Slide 1

Slide 1 text

HackͰ࡞Δ ϚΠΫϩϑϨʔϜϫʔΫ yuuki takezawa (ytake) PHPerKaigi 2018

Slide 2

Slide 2 text

Profile • ஛ᖒ ༗و / ytake • גࣜձࣾΞΠελΠϧ • PHP, Hack, Go, Scala • Apache Hadoop, Apache Spark, Apache Kafka
 • twitter https://twitter.com/ex_takezawa • facebook https://www.facebook.com/yuuki.takezawa • github https://github.com/ytake

Slide 3

Slide 3 text

author

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

͸ͳ͢͜ͱ • ։ൃ͢ΔͨΊͷ஌ࣝ 
 .hhconfigɺdecl / strictɺhhi • ڧྗͳϥΠϒϥϦ
 hhvm/hhvm-autoloadɺhhvm/hack-router • PHPϥΠϒϥϦͱͷޓ׵Λอͭ։ൃ

Slide 7

Slide 7 text

Slide 8

Slide 8 text

Slide 9

Slide 9 text

Slide 10

Slide 10 text

Slide 11

Slide 11 text

Slide 12

Slide 12 text

Slide 13

Slide 13 text

hhiϑΝΠϧΛ࡞੒(ޙड़) • __invokeͷѻ͍͕PHPͱҟͳΔͨΊܕΤϥʔൃੜ
 => strictͰ__invokeͰॲཧ͢Δϛυϧ΢ΣΞ(ඇPSR-15)
 ΛऔΓೖΕΔ৔߹͸஫ҙ
 • HackͰ࣮૷͢ΔͷͰ͋Ε͹StrictΛج४ʹɺ
 Ұ෦͚ͩPartial͕ϕετ

Slide 14

Slide 14 text

PHPͷ __invoke Λ࣮૷͢Δϛυϧ΢ΣΞܥΛ
 ͦͷ··strictͰҠ২͸Ͱ͖ͳ͍
 • inst_meth ͱ͍͏બ୒ࢶ

Slide 15

Slide 15 text

class Example { public function foo(T $t): T { return $t; } } inst_meth(new Example(), ‘foo’); Must be a constant string.

Slide 16

Slide 16 text

Slide 17

Slide 17 text

Slide 18

Slide 18 text

packagistʹ͋ΔhhiϑΝΠϧͨͪ • hack-psr/psr7-http-message-hhi • 91carriage/phpunit-hhi • ytake/psr-http-handlers-hhi • ytake/psr-container-hhi • libreworks/psr3-log-hhi
 • HackରԠϥΠϒϥϦ։ൃऀ͕࢖͏΋ͷఔ౓͔͋͠Γ· ͤΜ • nikic/fast-route ʹ΋ؚ·Ε͍ͯ·͢

Slide 19

Slide 19 text

PSR For Hack • https://github.com/facebookexperimental/hack-http- request-response-interfaces • This project aims to create standard request and response interfaces for Hack, using PSR-7 as a starting point.


Slide 20

Slide 20 text

Slide 21

Slide 21 text

Slide 22

Slide 22 text

Slide 23

Slide 23 text

Slide 24

Slide 24 text

Slide 25

Slide 25 text

Slide 26

Slide 26 text

Slide 27

Slide 27 text

Slide 28

Slide 28 text

Slide 29

Slide 29 text

Zend Expressive with HHVM/Hack

Slide 30

Slide 30 text

Slide 31

Slide 31 text

= 2.0 • zend-config-aggregator ͷҰ෦ͷίʔυ͕ಈ࡞ͤͣ
 __invoke, yield͕ಈ͔ͣ (Generator)
 • fast-routeͷhhi͕ϝϯς͞Ε͍ͯͳ͍ͨΊType Error
 PHPͱͷ෼཭͕ਐΉͨΊPRͤͣ
 • ಈ͔ͳ͍෦෼Λ
 Hack࣮૷ͷϥΠϒϥϦͱೖΕସ͑Δ͜ͱʹ

Slide 32

Slide 32 text

Slide 33

Slide 33 text

Slide 34

Slide 34 text

Slide 35

Slide 35 text

type TResponder = ImmVector>; final class Router extends BaseRouter { 
 <<__Override>> protected function getRoutes( ): ImmMap> { $i = $this->routeMap->getIterator(); $map = []; while ($i->valid()) { $map[$this->convertHttpMethod($i->key())] = $i->current(); $i->next(); } return new ImmMap($map); } ࢦఆͨ͠ΫϥεɺIFΛܧঝɺ࣮૷ͨ͠Ϋϥε໊એݴ Routerʹ֨ೲ͠ɺϚον࣌ʹTResponderΛฦ٫

Slide 36

Slide 36 text

Slide 37

Slide 37 text

>
 • Πϯελϯεղܾํ๏ఆٛ͸ࣗ༝

Slide 38

Slide 38 text

public function set( string $id, (function(FactoryContainer): mixed) $callback, Scope $scope = Scope::PROTOTYPE, ): void { if (!$this->locked) { $this->bindings->add(Pair {$id, $callback}); $this->scopes->add(Pair {$id, $scope}); } } Closure Type enums Map

Slide 39

Slide 39 text

use Ytake\HHContainer\FactoryContainer; $container = new FactoryContainer(); $container->set( MessageClass::class, $container ==> new MessageClass(‘testing') );
 $container->parameters( MessageClient::class, 'message', $container ==> $container->get(‘message.class’) ); $instance = $container->get(MessageClient::class); Lambda PSR-11࣮૷

Slide 40

Slide 40 text

final class TestingInvokable { public function __invoke(FactoryContainer $container): int { return 1; } } $container = new \Ytake\HHContainer\FactoryContainer(); $container->set(TestingInvokable::class, $container ==> $container->callable( new \Ytake\HHContainer\Invokable( new TestingInvokable(), '__invoke', $container ) ) ); Zend ServiceManager Factory෩ Πϯελϯεੜ੒࣌ʹ࣮ߦ͢Δ

Slide 41

Slide 41 text

Slide 42

Slide 42 text

໽հͳmixed PSR-11ͷྫ(strict)

Slide 43

Slide 43 text

Slide 44

Slide 44 text

$container = require 'config/services.php'; $app = $container->get('Zend\Expressive\Application'); mixedͳͨΊɺԿ͕ฦ٫͞ΕΔ͔Θ͔Βͳ͍

Slide 45

Slide 45 text

$config = $container->get('config_array'); if (is_array($config)) { if (array_key_exists('config_array', $config)) { return $config['config_array']; } } arrayͰ͋Δ͜ͱΛࣔ͢

Slide 46

Slide 46 text

$logger = $container->get(‘LoggerInterface'); invariant( $logger instanceof LoggerInterface, "Interface '\Psr\Log\LoggerInterface' is not implemented by this class", ); invariant͸HackͰ༻ҙ͞Ε͍ͯΔؔ਺ ୈҰҾ਺ͷ৚͕݅falseͷ৔߹ʹType Error

Slide 47

Slide 47 text

class TypeAssert { const type Tk = LoggerInterface; public static function assert(Tk $t): this::Tk { invariant( $logger instanceof LoggerInterface, "Interface '\Psr\Log\LoggerInterface' is not implemented by this class", ); return $t; } }

Slide 48

Slide 48 text

Slide 49

Slide 49 text

Forget PHP! Facebook’s HHVM engine switches to Hack instead

Slide 50

Slide 50 text

Slide 51

Slide 51 text

use namespace Facebook\TypeAssert; class Foo { const type TAPIResponse = shape( 'id' => int, 'user' => string, 'data' => shape( /* ... */ ), ); public static function getAPIResponse(): self::TAPIResponse { $json_string = file_get_contents('https://api.example.com'); $array = json_decode($json_string, true); return TypeAssert\matches_type_structure( type_structure(self::class, 'TAPIResponse'), $array, ); } } ShapeͰϑΟʔϧυࢦఆ ͍͋·͍ͳܕฦ٫APIΛݕࠪ

Slide 52

Slide 52 text

RouterͱContainerɺ ؆୯ͳValidation͕͋Ε͹ ࠷௿ݶͷಈ࡞͕Մೳʹ

Slide 53

Slide 53 text

Dependency / Container Build Application Routing/Dispatcher Middleware

Slide 54

Slide 54 text

type TResponder = ImmVector>; hack routerͷTResponder ϦΫΤετʹରԠ͢Δ΋ͷ͕ొ࿥͞ΕͯೖΕ͹ɺ TResponderͰࢦఆͨ͠ܕΛฦ٫͢Δ

Slide 55

Slide 55 text

\Nazg\Http\HttpMethod::GET => ImmMap { '/' => ImmVector {App\Action\IndexAction::class}, }, GETʹରԠͤ͞ΔRouteΛهड़ ‘/’ ʹΞΫηε࣌ʹىಈ͢Δϛυϧ΢ΣΞΛࢦఆ ImmVector> هड़ͨ͠௨Γʹ࣮ߦ͞ΕΔ
 ActionΫϥε΋ಉΠϯλʔϑΣʔε࣮૷ͷͨΊ۠ผͳ͠

Slide 56

Slide 56 text

public function run(ServerRequestInterface $serverRequest): void { $container = $this->getContainer(); $this->bootstrap($container); $router = $container->get(BaseRouter::class); invariant( $router instanceof BaseRouter, "%s class must extend %s", get_class($router), BaseRouter::class, ); list($middleware, $attributes) = $router->routePsr7Request($serverRequest); hack-routerܧঝΫϥεΛऔΓग़͠ from PSR-7

Slide 57

Slide 57 text

public function __construct( Traversable> $queue, ?Resolvable $resolver = null, ) { $this->queue = new Vector($queue); $this->resolver = (is_null($resolver)) ? new InstanceResolver() : $resolver; } public function shift(): MiddlewareInterface { $this->queue->reverse(); $current = $this->queue->pop(); return $this->resolver->resolve($current); } public function cancel(int $index): Vector> { return $this->queue->removeKey($index); } Collection (Vector) Vector ૢ࡞ ҰͭͣͭऔΓग़࣮ͯ͠ߦ Vector ૢ࡞ ࢦఆͨ͠ΠϯσοΫεͷΞΠςϜΛ࡟আ

Slide 58

Slide 58 text

https://github.com/ytake/nazg-skeleton https://github.com/nazg-hack/framework

Slide 59

Slide 59 text

Slide 60

Slide 60 text

Slide 61

Slide 61 text

Slide 62

Slide 62 text

Go(Goa, Echo)͸ଟ͘ͷAPIͰಋೖࡁΈ
 େ͖͘ݴޠΛม͑ͣʹɺ
 PHPͷ։ൃऀ͕࢖͑Δ΋ͷΛ૿΍͔ͨͬͨ͠
 • segmentation faultͭΒ͘ͳ͍Ͱ͔͢ʁ
 hhvm-dbg + straceͰͳΜͱ͔ͳΓ·͢