Slide 1

Slide 1 text

HackͰ࡞Δݎ࣮ͳ ΞϓϦέʔγϣϯΞʔΩςΫνϟ yuuki takezawa PHP Conference Sendai 2019

Slide 2

Slide 2 text

Profile • ஛ᖒ ༗و / ytake • גࣜձࣾΞΠελΠϧ CTO • 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

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

HackΛ࢖ͬͨΒ۩ମతʹԿ͕Ͱ͖Δͷʁ ۩ମతͳ಺༰Λ஌Γ͍ͨʂ

Slide 6

Slide 6 text

Agenda • PHP Array / Hack Arrays • Shapes In Hack • Generics (With Dependency Injection) • Trait And Interface Requirements • Implementing DDD / Generic Repository

Slide 7

Slide 7 text

ࠓճ͸HHVM 3.27Ҏ߱ʹରԠ͢Δ࿩Ͱ͢ ݹ͍όʔδϣϯΛར༻͍ͯ͠Δ৔߹͸ ΞοϓσʔτΛ

Slide 8

Slide 8 text

HHVM 3.30(Latest)

Slide 9

Slide 9 text

Notice • PHPޓ׵͕αϙʔτ͞ΕΔ࠷ޙͷόʔδϣϯ • hhvm.enable_php=false 
 PHPίʔυΛແޮԽ • HackTest΍CLIɺIOपΓͷϥΠϒϥϦ͕ॆ࣮ • HHVMͰPHPΛಈ࡞ͤ͞ΔΑΓ΋Hackͱͯ͠

Slide 10

Slide 10 text

Notice • GitHubͷfacebook/hhvmʹهࡌ͞Ε͍ͯΔ
 Prebuilt Packages on Centos 7.x͸ݹ͍
 • Ubuntuɺ·ͨ͸macOSਪ঑

Slide 11

Slide 11 text

Hack͔PHP͔ • HHVM্ͰPHPΛಈ͔͢ϝϦοτ͸ແ͠
 Ҏલ͸ߴ଎ʹಈ͘؀ڥɺͱͯ͠ೝ஌͞Ε͍ͯͨ
 PHPࣗମ͕ߴ଎Խ͞ΕɺHHVMࣗମ΋Hackઐ༻ʹ • ଎౓Ͱ͸ͳ͘ɺ
 ੩తܕ෇ݴޠϥΠΫͳػೳ͕ཉ͍͔͠Ͳ͏͔
 ࣮༻తͳΞϓϦέʔγϣϯαʔό͕ཉ͍͔͠Ͳ͏͔
 async΍ɺxhpͳͲͷχʔζ͕͋Δ͔Ͳ͏͔

Slide 12

Slide 12 text

Hackपลπʔϧͷ࿩ • composer͸ɺhhvm/hh-autoloadͱҰॹʹ࢖͏͜ͱ • Hackͷ։ൃ؀ڥ͸ɺIDEαϙʔτ͕ڧྗ
 vscodeɺnuclide • hhast͕linterͱͯ͠ػೳ
 ίʔυͷมߋͳͲ΋΍ͬͯ͘ΕΔ • HHVM/Hackࣗମ͕։ൃαϙʔτػೳଟ͠

Slide 13

Slide 13 text

Ͱ΋PHPͷϥΠϒϥϦ ࢖͑ͳ͍ΜͰ͠ΐ͏ʁ

Slide 14

Slide 14 text

Hack ϥΠϒϥϦ • facebook/hack-router Hackઐ༻ϧʔλʔ • facebook/hh-clilib CLIϥΠϒϥϦ • usox/hackttp Hack HTTPΠϯλʔϑΣʔε࣮૷ • ytake/hungrr Hack HTTPΠϯλʔϑΣʔε࣮૷ • hhpack/service-locator αʔϏεϩέʔλʔɾDI • nazg/glue αʔϏεϩέʔλʔɾDI • nazg/heredity ϛυϧ΢ΣΞσΟεύονϟ • nazg/hcache Ωϟογϡ

Slide 15

Slide 15 text

PHP Array / Hack Arrays

Slide 16

Slide 16 text

PHPͱHack • PHPͰ͸഑ྻ͸arrayͷΈͰදݱ͞ΕΔ
 ഑ྻࣗମʹܕΛࢦఆ͢Δ͜ͱ͸Ͱ͖ͳ͍ • HackͰ഑ྻ͸arrayɺdarrayɺvarrayͷ3छ
 ͲΜͳ഑ྻͰ΋ܕΛࢦఆ͠ͳ͚Ε͹ͳΒͳ͍

Slide 17

Slide 17 text

collect[$key] = $value; } public function all(): array { return $this->collect; } }

Slide 18

Slide 18 text

collect[$key] = $value; } public function all(): array { return $this->collect; } } ෆ࣮֬ͳܕͷࠞೖΛ๷͙ͨΊ
 ೖྗΛ੍ݶ

Slide 19

Slide 19 text

collect[$key] = $value; } public function all(): array { return $this->collect; } } ੍ޚෆՄ

Slide 20

Slide 20 text

class ExtendArraySet extends ArraySet { public function all(): array { $this->collect[1] = 2; return $this->collect; } } ੍ޚෆՄ

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

$collect = []; public function add(string $key, string $value): void { $this->collect[$key] = $value; } public function all(): darray { return $this->collect; } }

Slide 23

Slide 23 text

$collect = []; public function add(string $key, string $value): void { $this->collect[$key] = $value; } public function all(): darray { return $this->collect; } } ෆ࣮֬ͳܕͷࠞೖΛ๷͙ͨΊ
 ೖྗΛ੍ݶ

Slide 24

Slide 24 text

$collect = []; public function add(string $key, string $value): void { $this->collect[$key] = $value; } public function all(): darray { return $this->collect; } } ܧঝ࣌Ͱ΋ͦͷ··੍ޚՄ

Slide 25

Slide 25 text

class ExtendArraySet extends ArraySet { public function all(): darray { $this->collect[1] = 2; return $this->collect; } } ૠೖෆՄ

Slide 26

Slide 26 text

class ExtendArraySet extends ArraySet { public function all(): darray { $this->collect[1] = 2; return $this->collect; } } ໭ΓͷܕΛมߋ͢Δ͜ͱ΋ෆՄ

Slide 27

Slide 27 text

src/ArraySet.php:19:5,25: Invalid assignment (Typing[4110]) src/ArraySet.php:5:20,25: This is a string src/ArraySet.php:19:20,20: It is incompatible with an int src/ArraySet.php:20:12,25: Invalid return type (Typing[4110]) src/ArraySet.php:18:33,38: This is a string src/ArraySet.php:19:20,20: It is incompatible with an int

Slide 28

Slide 28 text

public function merge(): darray { $a = new ArraySet(); $a->add('PHP', 'Arrays'); $ar = $a->all(); $ar[1] = 11; return $ar; } औಘޙɺ ҟͳΔܕͷ஋Λࠞೖͤ͞Δ͜ͱ͸Մೳ *໭ΓͷܕΛఆٛ͢Δ͜ͱ

Slide 29

Slide 29 text

PHP Style Arrays • array
 ௨ৗͷ഑ྻ • varray
 ஋ͷΈͰߏ੒͞ΕΔ഑ྻ • darray
 σΟΫγϣφϦͷ഑ྻ

Slide 30

Slide 30 text

$varray = varray[ 'php', 'hack' ]; protected darray $darray = darray[ 'testing' => 'testing', 1 => 'testing' ]; public function failedVArray(): varray { return $this->varray; } public function getDArray(): darray { return $this->darray; } }

Slide 31

Slide 31 text

$varray = varray[ 'php', 'hack' ]; protected darray $darray = darray[ 'testing' => 'testing', 1 => 'testing' ]; public function failedVArray(): varray { return $this->varray; } public function getDArray(): darray { return $this->darray; } } ܕҧ͍

Slide 32

Slide 32 text

$varray = varray[ 'php', 'hack' ]; protected darray $darray = darray[ 'testing' => 'testing', 1 => 'testing' ]; public function failedVArray(): varray { return $this->varray; } public function getDArray(): darray { return $this->darray; } } ໭Γͷܕҧ͍

Slide 33

Slide 33 text

$varray = varray[ 'php', 'hack' ]; protected darray $darray = darray[ 'testing' => 'testing', 1 => 'testing' ]; public function failedVArray(): varray { return $this->varray; } public function getDArray(): darray { return $this->darray; } } ໭Γͷܕҧ͍

Slide 34

Slide 34 text

Hack Arrays • vec
 ७ਮ഑ྻΛѻ͏഑ྻ
 VectorɺImmVectorஔ͖׵͑Մ • dict
 keyͱvalueͰߏ੒͞ΕΔ഑ྻ
 MapɺImmMapஔ͖׵͑Մ

Slide 35

Slide 35 text

Hack Arrays • keyset
 ॏෳ͠ͳ͍஋Λ࣋ͭ७ਮ഑ྻ
 SetɺImmSetஔ͖׵͑Մ


Slide 36

Slide 36 text

$collect = dict[]; public function add(string $key, string $value): void { $this->collect[$key] = $value; } public function all(): dict { return $this->collect; } } dictࢦఆ

Slide 37

Slide 37 text

Hack Arrays • Hack Arrays͸
 isԋࢉࢠ(HackͷΈͷԋࢉࢠ)Λ༻͍ͯ൑ఆՄೳ
 $dict is dict<_, _> , etc • Hack Arrays͸ͦΕͧΕͷ഑ྻʹม׵Մೳ
 (Collection͔ΒHack Arrays΍ͦͷٯ΋)

Slide 38

Slide 38 text

$vec = vec[1,1,2,3,4,5,6,6,7,8]; var_dump($vec); var_dump(dict($vec)); var_dump(keyset($vec)); $dict = dict[1 => 2, 3 => 'testing']; var_dump(vec($dict), $dict is dict<_, _>);

Slide 39

Slide 39 text

abstract final class dict<+Tk as arraykey, +Tv> implements Indexish, XHPChild {} abstract final class keyset<+T as arraykey> implements Indexish, XHPChild {} abstract final class vec<+T> implements Indexish, XHPChild {}

Slide 40

Slide 40 text

Hack Collections • PHPϥΠΫͳ഑ྻ΍ɺHack ArraysΑΓ΋
 ݎ͍ίϨΫγϣϯ • ͲΕΛར༻͢Δ͔͸ઃܭɾ࣮૷࣍ୈ
 foreachΑΓ΋
 filter΍mapͱ͍ͬͨॲཧΛ͢ΔͷͰ͋Ε͹Collections
 • cloneޙʹ஋͕มߋ͞ΕΔ(ϝϞϦ)

Slide 41

Slide 41 text

class CollectionSet { protected Map $collect = Map{}; public function add(string $key, string $value): void { $this->collect[$key] = $value; } public function all(): Map { return $this->collect; } } Mapࢦఆ

Slide 42

Slide 42 text

class CollectionSet { protected Map $collect = Map{}; public function add(string $key, string $value): void { $this->collect[$key] = $value; } public function all(): Map { return $this->collect; } } ഑ྻͱಉ͡Α͏ʹૠೖ

Slide 43

Slide 43 text

ૠೖෆՄ $c = (new CollectionSet())->all(); $c->add(Pair{1, 2});

Slide 44

Slide 44 text

Notice: Hack Arrays / Collections • HackͰ͸issetɺunset͸ར༻Ͱ͖ͳ͍ • unset͸removeɺfilter(Collection)΍
 hhvm/hslΛར༻͢Δ͜ͱ • isset͸contains(Collection)΍array_key_existsɺ
 hhvm/hslར༻

Slide 45

Slide 45 text

class HackMap { protected Map $collect = Map{ 'a' => 'b', 'c' => 'd', }; public function filterRemove( string $keyName ): Map { return $this->collect ->filterWithKey(($key, $_) ==> $key !== $keyName); } public function remove( string $keyName ): Map { return $this->collect->remove($keyName); } } ࢦఆͨ͠ΩʔҎ֎ͷ΋ͷΛ MapͰฦ٫

Slide 46

Slide 46 text

class HackMap { protected Map $collect = Map{ 'a' => 'b', 'c' => 'd', }; public function filterRemove( string $keyName ): Map { return $this->collect ->filterWithKey(($key, $_) ==> $key !== $keyName); } public function remove( string $keyName ): Map { return $this->collect->remove($keyName); } } ࢦఆͨ͠ΩʔΛ࡟আ͠ɺ MapͰฦ٫

Slide 47

Slide 47 text

Hack Arrays / Collections • array͚ͩͰ͸੍ݶͰ͖ͳ͍΋ͷΛ੍ݶ • PHP͔ΒҠߦ͢Δ৔߹͸·͔ͣ͜͜Β
 Ұ൪ޮՌ͕େ͖͍ • arrayૢ࡞ͱ΄΅ಉ͡ͰɺͦΕΒͷPHPؔ਺͕ར༻Մ

Slide 48

Slide 48 text

Shapes In Hack

Slide 49

Slide 49 text

Shapes? • ഑ྻͷϑΟʔϧυʹରͯ͠ܕΛఆٛ͢Δ΋ͷ
 • TypecheckerͰϑΟʔϧυΛ൑ఆͰ͖ΔͨΊɺ
 ෆཁͳϑΟʔϧυͷ௥Ճ΍ɺෆ࣮֬ͳ஋ΛഉআͰ͖Δ • ഑ྻͦͷ΋ͷͰ͸ͳ͍ͨΊɺforeach΍ [] ΦϖϨʔλ͸
 ར༻ෆՄ

Slide 50

Slide 50 text

'qwerty', 'w' => 'wertyu', ]; } }

Slide 51

Slide 51 text

'qwerty', 'w' => 'wertyu', ]; } } ഑ྻͷϑΟʔϧυʹରͯ͠
 ੍ݶෆՄ

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

string, 'w' => string); class GenerateArrayField { public function getArray(): SampleShape { return shape( 'q' => 'qwerty', 'w' => 'wertyu', ); } }

Slide 54

Slide 54 text

string, 'w' => string); class GenerateArrayField { public function getArray(): SampleShape { return shape( 'q' => 'qwerty', 'w' => 'wertyu', ); } } ഑ྻͷϑΟʔϧυʹܕఆٛ

Slide 55

Slide 55 text

string, 'w' => string); class GenerateArrayField { public function getArray(): SampleShape { return shape( 'q' => 'qwerty', 'w' => 'wertyu', ); } } shapeΛ࢖ͬͯهड़

Slide 56

Slide 56 text

public function getArrays(): vec { return vec[ shape('q' => 'qwerty', 'w' => 'wertyu',), shape('q' => 'PHP', 'w' => 'Hack',), shape('q' => 'Java', 'w' => 'Scala',), ]; } vec഑ྻʹshapesΛ
 ૊Έ߹ΘͤͯΑΓ࣮֬ͳ഑ྻ΁

Slide 57

Slide 57 text

namespace Acme\Shapes; type SampleShape = shape( 'a' => int, 'b' => int ); type NestShape = shape( 'sample' => SampleShape, 'vec' => vec ); class NestedShape { public function nest(): NestShape { return shape( 'sample' => shape( 'a' => 0, 'b' => 1 ), 'vec' => vec[ 'shapes' ], ); } } Shapesಉ࢜Λ૊Έ߹Θͤͯ
 ఆٛՄ

Slide 58

Slide 58 text

The field 'z' is not defined in this shape type, and this shape type does not allow unknown fields. The field 'z' is set in the shape. ఆٛ͞Ε͍ͯͳ͍ϑΟʔϧυ ར༻࣌ʹΤϥʔ

Slide 59

Slide 59 text

Shapes? • ଞݴޠͷStructͷΑ͏ʹϑΟʔϧυͷܕએݴ • APIͳͲͷόϦσʔγϣϯ΍ɺ
 ϝοηʔδϒϩʔΧʔ΁ͷ௨৴ͳͲʹ • PHPؔ਺ͷ໭Γ஋ͱ૊Έ߹Θͤͯݫ֨ʹ • PHPʹಉ౳ػೳ͸ແ͠

Slide 60

Slide 60 text

class HalShape { const type embeddedLinks = shape( 'name' => string, '_links' => shape( 'self' => shape( 'href' => string ) ) ); public function index(): void { $url = [ 'name' => 'hack', '_links' => [ 'self' => [ 'href' =>'uri' ] ] ]; \var_dump($url as this::embeddedLinks); } }

Slide 61

Slide 61 text

class HalShape { const type embeddedLinks = shape( 'name' => string, '_links' => shape( 'self' => shape( 'href' => string ) ) ); public function index(): void { $url = [ 'name' => 'hack', '_links' => [ 'self' => [ 'href' =>'uri' ] ] ]; \var_dump($url as this::embeddedLinks); } } type constantsΛ࢖ͬͯ
 shapeΛهड़

Slide 62

Slide 62 text

class HalShape { const type embeddedLinks = shape( 'name' => string, '_links' => shape( 'self' => shape( 'href' => string ) ) ); public function index(): void { $url = [ 'name' => 'hack', '_links' => [ 'self' => [ 'href' =>'uri' ] ] ]; \var_dump($url as this::embeddedLinks); } } ഑ྻͳͲͰ஋Λهड़

Slide 63

Slide 63 text

class HalShape { const type embeddedLinks = shape( 'name' => string, '_links' => shape( 'self' => shape( 'href' => string ) ) ); public function index(): void { $url = [ 'name' => 'hack', '_links' => [ 'self' => [ 'href' =>'uri' ] ] ]; \var_dump($url as this::embeddedLinks); } } shapeͷఆٛΛݩʹ഑ྻΛݕࠪ

Slide 64

Slide 64 text

Shapes? • ϑΟʔϧυࣗମ͕nullableͷ৔߹͸ɺϑΟʔϧυ໊ʹ? • ϑΟʔϧυͷ஋͕nullableͷ৔߹͸ɺܕʹ? • ShapeͷϑΟʔϧυΞΫηε࣌ͷσϑΥϧτ஋͸
 Shapes::idx($shape, $fieldName, 'default') • ഑ྻͳͲͷม׵͸toArray΍toDictͳͲ

Slide 65

Slide 65 text

Hack Arrays + Shapes • ͜ΕΒ͸શͯTypechekcerʹΑΔܕνΣοΫͰ൑ఆՄ • ܕʹؔ͢Δίʔυ͕େ͖͘ݮΔ • ܕʹؔ͢ΔίʔυϨϏϡʔ࣌ؒ࡟ݮ • جຊతʹ͸strictͰهड़͢Δ͜ͱ

Slide 66

Slide 66 text

Hack Application Architecture

Slide 67

Slide 67 text

Dependency Injection

Slide 68

Slide 68 text

DI Container • PHP༻ͷϥΠϒϥϦΛར༻Ͱ͖Δ͕ɺ
 4ܥ͔Βར༻Ͱ͖ͳ͍ͨΊɺϝϦοτແ͠ • PSR-11Λͦͷ··ར༻͢Δ
 ΋͘͠͸ܕʹΑΔ੍໿Λݫͨ͘͠͠΋ͷΛ։ൃ

Slide 69

Slide 69 text

DI Container • ίϯςφ͔ΒΠϯελϯεΛ࣮֬ʹऔಘ͢ΔͨΊʹ
 ొ࿥͸Ϋϥε໊ͷΈ
 • mixedΛϦλʔϯ͢Δ৔߹͸ɺ
 ར༻࣌ʹܕνΣοΫΛߦΘͳ͚Ε͹ͳΒͳ͍(Typecheker) • औಘ࣌͸ΦϒδΣΫτͷΈฦ٫ͱ͢Δ


Slide 70

Slide 70 text

Ϋϥε໊Λදݱ • PHPͰ͸Ϋϥε໊͸stringͰ͔͠ͳ͍ͨΊɺ
 ଘࡏ͢ΔΫϥε͔Ͳ͏͔ɺݫ֨ͳࢦఆ͸Ͱ͖ͳ͍ • HackͰ͸ stringͰ͸ͳ͘ classname Λར༻
 ::class ར༻͢Δͱ͜ͷܕʹ • GenericsΛซ༻

Slide 71

Slide 71 text

$container = new Container(); $container->set( \stdClass::class, ($container) ==> new \stdClass() ); $stdClass = $container->getInstance(\stdClass::class); ೖྗΛΫϥε໊ͷΈͱ੍ͯ͠ݶ

Slide 72

Slide 72 text

$container = new Container(); $container->set( \stdClass::class, ($container) ==> new \stdClass() ); $stdClass = $container->getInstance(\stdClass::class); Typechecker͕൑ఆͰ͖ͳ͍৔߹͸ɺ isͳͲΛར༻ͯ͠อূ͢Δඞཁ͕͋Δ

Slide 73

Slide 73 text

{ private dict< string, (Scope, (function(\Acme\Container): T)) > $map = dict[]; public function set( classname $id, (function(\Acme\Container): T) $callback, Scope $scope = Scope::PROTOTYPE, ): void { $this->map[$id] = tuple($scope, $callback); }

Slide 74

Slide 74 text

{ private dict< string, (Scope, (function(\Acme\Container): T)) > $map = dict[]; public function set( classname $id, (function(\Acme\Container): T) $callback, Scope $scope = Scope::PROTOTYPE, ): void { $this->map[$id] = tuple($scope, $callback); } Genericsར༻

Slide 75

Slide 75 text

{ private dict< string, (Scope, (function(\Acme\Container): T)) > $map = dict[]; public function set( classname $id, (function(\Acme\Container): T) $callback, Scope $scope = Scope::PROTOTYPE, ): void { $this->map[$id] = tuple($scope, $callback); } dictΛར༻͠ɺ ίʔϧόοΫͰTΛฦ٫͢Δͱఆٛ

Slide 76

Slide 76 text

{ private dict< string, (Scope, (function(\Acme\Container): T)) > $map = dict[]; public function set( classname $id, (function(\Acme\Container): T) $callback, Scope $scope = Scope::PROTOTYPE, ): void { $this->map[$id] = tuple($scope, $callback); } classname 
 ͭ·Γଘࡏ͢ΔΫϥε໊ͷΈڐՄͱ͢Δ

Slide 77

Slide 77 text

{ private dict< string, (Scope, (function(\Acme\Container): T)) > $map = dict[]; public function set( classname $id, (function(\Acme\Container): T) $callback, Scope $scope = Scope::PROTOTYPE, ): void { $this->map[$id] = tuple($scope, $callback); } tupleͰΠϯελϯεੜ੒ํ๏ͱɺ ίʔϧόοΫΛอ࣋

Slide 78

Slide 78 text

dictૢ࡞ • dictʹొ࿥ࡁΈ͔Ͳ͏͔͸array_key_existsɺ
 ·ͨ͸ϥούʔؔ਺Λར༻ • Πϯελϯεੜ੒ํ๏ʹΑͬͯϝϞԽ͢Δ͔Ͳ͏͔

Slide 79

Slide 79 text

<<__Rx>> public function getInstance(classname $t): T { return $this->resolve($t); } <<__Memoize>> protected function shared(classname $id): T { list($_, $callable) = $this->map[$id]; return $callable($this); } <<__Rx>> public function has(string $id): bool { return C\contains_key($this->map, $id); }

Slide 80

Slide 80 text

<<__Rx>> public function getInstance(classname $t): T { return $this->resolve($t); } <<__Memoize>> protected function shared(classname $id): T { list($_, $callable) = $this->map[$id]; return $callable($this); } <<__Rx>> public function has(string $id): bool { return C\contains_key($this->map, $id); } hhvm/hsl
 array_key_existsϥούʔؔ਺

Slide 81

Slide 81 text

<<__Rx>> public function getInstance(classname $t): T { return $this->resolve($t); } <<__Memoize>> protected function shared(classname $id): T { list($_, $callable) = $this->map[$id]; return $callable($this); } <<__Rx>> public function has(string $id): bool { return C\contains_key($this->map, $id); } ϝϞԽࢦఆ Ҿ਺Λར༻ͯ͠Ωϟογϡ͞ΕΔ γϯάϧτϯ

Slide 82

Slide 82 text

<<__Rx>> public function getInstance(classname $t): T { return $this->resolve($t); } <<__Memoize>> protected function shared(classname $id): T { list($_, $callable) = $this->map[$id]; return $callable($this); } <<__Rx>> public function has(string $id): bool { return C\contains_key($this->map, $id); } reactiveʹ

Slide 83

Slide 83 text

<<__Rx>> protected function resolve(classname $id): T { if ($this->has($id)) { list($scope, $callable) = $this->map[$id]; if ($callable is nonnull) { if ($scope === Scope::SINGLETON) { return $this->shared($id); } return $callable($this); } } throw new Exception\NotFoundException( Str\format('Identifier "%s" is not binding.', $id), ); }

Slide 84

Slide 84 text

<<__Rx>> protected function resolve(classname $id): T { if ($this->has($id)) { list($scope, $callable) = $this->map[$id]; if ($callable is nonnull) { if ($scope === Scope::SINGLETON) { return $this->shared($id); } return $callable($this); } } throw new Exception\NotFoundException( Str\format('Identifier "%s" is not binding.', $id), ); } nullͰͳ͍͔Ͳ͏͔

Slide 85

Slide 85 text

<<__Rx>> protected function resolve(classname $id): T { if ($this->has($id)) { list($scope, $callable) = $this->map[$id]; if ($callable is nonnull) { if ($scope === Scope::SINGLETON) { return $this->shared($id); } return $callable($this); } } throw new Exception\NotFoundException( Str\format('Identifier "%s" is not binding.', $id), ); } γϯάϧτϯͷ৔߹͸ɺ ϝϞԽ

Slide 86

Slide 86 text

ݎ࣮ͳΠϯελϯεऔಘ • ෆ࣮֬ͳཁૉ͕ೖΓࠐ·ͳ͍Α͏ͳ੍໿ • Ϋϥεऔಘͱొ࿥Ͱଘࡏ͠ͳ͍஋Λར༻ͤ͞ͳ͍ • ΦϒδΣΫτҎ֎Λѻ͏৔߹͸ɺ
 ͦΕ༻ͷొ࿥ɾऔಘϝιουΛ༻ҙ͢Δͷ͕࣮֬ • PHPͰ͸੍ݶͰ͖ͳ͍͜ͱ͕PHPϥΠΫͳίʔυͰՄ

Slide 87

Slide 87 text

HackͰ࡞ΔΞϓϦέʔγϣϯઃܭͬͯʁ • ϨΠϠʔυ΍ɺΫϦʔϯΞʔΩςΫνϟɺ
 ϔΩαΰφϧΞʔΩςΫνϟͳͲ
 ΦϒδΣΫτࢦ޲Ͱ༻͍ΒΕΔ΋ͷ͸౰વՄೳ • ෆ࣮֬ͳ஋΍ૢ࡞͕ೖΓࠐ·ͳ͍Α͏ʹ͢Δ͜ͱʹ
 ప͢Δ • ͍ΘΏΔ൚༻ੑ΍ݟ΍͢͞ɺΑΓ΋খ͘͞ɺ
 ͦͯ͠TypecheckerʹܕΛڭ͑Δίʔυʹ͢Δ

Slide 88

Slide 88 text

Trait And Interface Requirements

Slide 89

Slide 89 text

Traitͷ՝୊ • PHPͷTrait͸੍໿Λ͔͚ΒΕͳ͍ͨΊɺ
 ෳࡶͳॲཧ͕͏·Ε΍͍͢ • ϑϨʔϜϫʔΫͳͲʹ΋͜͏ͨ͠Trait͸ଟ͍

Slide 90

Slide 90 text

protected function validateEmail(Request $request) { $this->validate( $request, ['email' => 'required|email'] ); }

Slide 91

Slide 91 text

Laravelͷྫ: Illuminate\Foundation\Auth\SendsPasswordResetEmails • TraitʹύεϫʔυϦηοτʹؔ͢ΔৼΔ෣͍͕͋Δ • ྫʹڍ͛ͨvalidateϝιου͸ɺ
 ͜ͷτϨΠτʹ͸ͳ͍ϝιουͰɺ
 ଞͷΫϥεɾτϨΠτͷϝιουʹґଘ • Traitࣗମʹґଘؔ܎͕ͳ͍ͨΊɺ
 ಺༰Λ஌Βͳ͍ͱར༻Ͱ͖ͳ͍ྫ

Slide 92

Slide 92 text

Traitʹ஫ҙ • ܧঝͰ͸ͳ͍ͨΊɺ؆୯ʹ࠶ར༻͕Ͱ͖Δ
 ͕ɺಛఆͷΫϥεͷϓϩύςΟ͕͋Δʁʂ • ϓϩύςΟ͕ଘࡏ͢Δ͔Ͳ͏͔
 νΣοΫ͍ͯ͠Δॲཧ͕͋Δʁʂ • άϩʔόϧΞΫηε͢ΔΑ͏ͳϔϧύʔؔ਺͕͋Δʁʂ

Slide 93

Slide 93 text

/** * Fire an event when a lockout occurs. * * @param \Illuminate\Http\Request $request * @return void */ protected function fireLockoutEvent(Request $request) { event(new Lockout($request)); } /** * Get the rate limiter instance. * * @return \Illuminate\Cache\RateLimiter */ protected function limiter() { return app(RateLimiter::class); }

Slide 94

Slide 94 text

ෳࡶͳґଘؔ܎ • Laravelͷ app ϔϧύʔؔ਺͸
 αʔϏείϯςφ(DIίϯςφ)ʹΞΫηε͕Ͱ͖Δ΋ͷ • τϨΠτͱͷґଘؔ܎͕͋ΔԿ͔ͷॲཧͷதͰɺ
 Πϯελϯεੜ੒ʹ͍ͭͯͷอূ͕Ͱ͖ͳ͍ͨΊɺ
 DIίϯςφʹάϩʔόϧΞΫηεΛ
 ߦ͍ΠϯελϯεΛऔಘ͢Δɺ
 ͱ͍͏αʔϏεϩέʔλґଘΛӅṭ

Slide 95

Slide 95 text

HackͷTrait • τϨΠτΛར༻͢ΔΫϥεͷൣғΛࢦఆͰ͖Δ • ͋ΔΫϥεΛܧঝͨ͠΋ͷͷΈར༻ͤ͞Δ͔ɺ
 ͋ΔΠϯλʔϑΣʔεΛ࣮૷ͨ͠΋ͷͷΈ͔ • ෳࡶʹͳΓ΍͍͢ґଘؔ܎Λੜ·ͳ͍Α͏ʹ
 ڧ੍తʹ੍໿Λ͔͚Δ͜ͱ͕Մ

Slide 96

Slide 96 text

Slide 97

Slide 97 text

Slide 98

Slide 98 text

> $headers = dict[]; }

Slide 99

Slide 99 text

> $headers = dict[]; } ࢦఆͨ͠ΠϯλʔϑΣʔεΛ࣮૷ ͨ͠ΫϥεͷΈʹར༻Λ੍ݶ

Slide 100

Slide 100 text

src/Request.php:6:7,18: Failure to satisfy requirement: Acme\RequestInterface (Typing[4111]) src/MessageTrait.php:6:22,37: Required here ΠϯλʔϑΣʔεΛ࣮૷͍ͯ͠ͳ͍ Ϋϥεʹهड़ͨ͠৔߹͸ɺ ΤϥʔͱͳΓ·͢

Slide 101

Slide 101 text

HackͷTrait • ར༻͢ΔΫϥεͷϝιουΛݺͿ͜ͱ͸Ͱ͖ͳ͍ • method_exists͸ܧঝݩͷ΋ͷ͔ɺ
 ·ͨ͸ಉҰͷτϨΠτ಺ͷΈҎ֎Ͱ͸
 TypececkerͰΤϥʔ

Slide 102

Slide 102 text

Slide 103

Slide 103 text

trait MessageTrait { require implements RequestInterface; private dict> $headers = dict[]; public function castIntVersion(): int { return (int) $this->getVersion(); } }

Slide 104

Slide 104 text

trait MessageTrait { require implements RequestInterface; private dict> $headers = dict[]; public function castIntVersion(): int { return (int) $this->getVersion(); } } ϦΫΤετΫϥεͷϝιουΛ ར༻͍ͨ͠

Slide 105

Slide 105 text

src/MessageTrait.php:10:25,34: Could not find method getVersion in an object of type Acme\MessageTrait (Typing[4053]) src/MessageTrait.php:10:18,22: This is why I think it is an object of type Acme\MessageTrait src/MessageTrait.php:5:7,18: Declaration of Acme\MessageTrait is here ΠϯλʔϑΣʔεʹఆٛ͞Ε͍ͯͳ͍ ϝιου͸ίʔϧෆՄ

Slide 106

Slide 106 text

HackͷTrait • ಛఆͷΫϥε͕ར༻͢Δ
 ΦϒδΣΫτͷϓϩύςΟ͕͋Δ৔߹͸ɺ
 Πϯελϯε͕ਖ਼͍͠ΦϒδΣΫτͰ͋Δ͔Ͳ͏͔Λ
 هड़͠ͳ͚Ε͹ͳΒͳ͍

Slide 107

Slide 107 text

> $headers = dict[]; protected \stdClass $class; public function getStdClass(): \stdClass { return $this->class; } }

Slide 108

Slide 108 text

> $headers = dict[]; protected \stdClass $class; public function getStdClass(): \stdClass { return $this->class; } } stdClassΛϓϩύςΟͱͯ͠ѻ͏৔߹

Slide 109

Slide 109 text

class); } } Traitʹهड़͞ΕͨϓϩύςΟΛͦͷ·· ར༻͢ΔͱΤϥʔ

Slide 110

Slide 110 text

class); } } ࣮֬ʹΠϯελϯεΛ౉͢͜ͱ

Slide 111

Slide 111 text

HackͷTrait • ൚༻ੑΛ࣋ͭTraitͱͨ͠৔߹ɺ
 ͋ΔΫϥεͰ͸࢖Θͳ͍ϓϩύςΟ͕
 ଘࡏ͢Δ৔߹͸Type Error͕ൃੜ͢Δ
 ෆ࣮֬ͳࢦఆ͸Ͱ͖ͳ͍ • ͜ͷϓϩύςΟͲ͜Ͱ࢖ΘΕ͍ͯΔͷʁ
 ͱ͍͏࣮૷͕ੜ·Εʹ͘͘ɺ໾ׂ͕໌֬

Slide 112

Slide 112 text

Implementing DDD Generic Repository *ଟগΦʔόʔͳྫ

Slide 113

Slide 113 text

https://github.com/ytake/hack-ddd-repository-sample *αϯϓϧίʔυ͸ͪ͜ΒΛ͝ཡ͍ͩ͘͞

Slide 114

Slide 114 text

Generic Repository • GenericsͱRepository PatternΛ૊Έ߹Θͤͯɺ
 ୯Ұͷू໿ʹؔ࿈͚ͮΔ • SpecificationύλʔϯΛ૊Έ߹ΘͤΔ͜ͱͰখ͘͞͠
 نଇΛద༻͢Δ • ଞݴޠͰ͸Α͘ར༻͞ΕΔύλʔϯ
 HackͰ΋ಉ͜͡ͱ͕Ͱ͖Δʂ

Slide 115

Slide 115 text

ࢦఆͨ͠೔෇ΑΓ ৽͍͠هࣄ͕ಡΈ͍ͨ

Slide 116

Slide 116 text

هࣄΦϒδΣΫτ *DDDࣗମͷ࿩Λ͢ΔΘ͚Ͱ͸ແ͍ͷͰৄࡉ͸ׂѪ

Slide 117

Slide 117 text

{ public function __construct( private T $id ) {} <<__Rx>> public function id(): T { return $this->id; } <<__Rx>> public function equals(Identifier $id): bool { return $this->id === $id->id(); } }

Slide 118

Slide 118 text

{ public function __construct( private T $id ) {} <<__Rx>> public function id(): T { return $this->id; } <<__Rx>> public function equals(Identifier $id): bool { return $this->id === $id->id(); } } Ҿ਺΍໭ΓͰར༻͢ΔܕΛҰͭʹ

Slide 119

Slide 119 text

{ public function __construct( private T $id ) {} <<__Rx>> public function id(): T { return $this->id; } <<__Rx>> public function equals(Identifier $id): bool { return $this->id === $id->id(); } } int int int int

Slide 120

Slide 120 text

extends Model\Identifier { }

Slide 121

Slide 121 text

extends Model\Identifier { } ྫ͑͹ɺIdͱ͍ͬͯ΋
 intͱ͸ݶΒͳ͍

Slide 122

Slide 122 text

{ public function getID(): T; }

Slide 123

Slide 123 text

namespace Acme\Domain\Model\Article\Entity; use type DateTime; use type Acme\Domain\Model\EntityInterface; use type Acme\Domain\Model\Article\ArticleId; use type Acme\Domain\Model\Article\Body; class Article implements EntityInterface { const int EXPIRE_EDIT_TIME = 120; public function __construct( private ArticleId $id, private Body $body, private DateTime $createdAt = new DateTime() ) {} <<__Rx>> public function getID(): T { return $this->id->id(); } // লུ }

Slide 124

Slide 124 text

namespace Acme\Domain\Model\Article\Entity; use type DateTime; use type Acme\Domain\Model\EntityInterface; use type Acme\Domain\Model\Article\ArticleId; use type Acme\Domain\Model\Article\Body; class Article implements EntityInterface { const int EXPIRE_EDIT_TIME = 120; public function __construct( private ArticleId $id, private Body $body, private DateTime $createdAt = new DateTime() ) {} <<__Rx>> public function getID(): T { return $this->id->id(); } // লུ } ୯ҰͷܕͷΈ ޡͬͨܕ͕հೖͮ͠Β͍
 (TypecheckerͰݕ஌)

Slide 125

Slide 125 text

ϦϙδτϦ *DDDࣗମͷ࿩Λ͢ΔΘ͚Ͱ͸ແ͍ͷͰৄࡉ͸ׂѪ

Slide 126

Slide 126 text

T? • EntityInterfaceΛܕએݴͰར༻͢Δ͜ͱ΋OK • ͨͩ͠EntityInterfaceΛ࣮૷͍ͯ͠Δ΋ͷͰ͋Ε͹
 ͳΜͰ΋Α͍͜ͱʹͳͬͯ͠·͏
 • ΑΓڧ੍͍໿Λ͔͚͍ͨ

Slide 127

Slide 127 text

interface ArticleRepositoryInterface { public function add(T $entity): void; public function remove(T $entity): void; public function findById(TId $id): T; public function latestArticles(DateTime $date): Map $specification ): Map; public function size(): int; }

Slide 128

Slide 128 text

interface ArticleRepositoryInterface { public function add(T $entity): void; public function remove(T $entity): void; public function findById(TId $id): T; public function latestArticles(DateTime $date): Map $specification ): Map; public function size(): int; } TId : Identifier͸intಛఆͷܕ͔Ͳ͏͔͸ɻɻʁ T: EntityͰ͸͋Δ͕ɺ͜ͷϦϙδτϦͰѻ͏΋ͷ͸શͯಉ͡ܕ

Slide 129

Slide 129 text

ϦϙδτϦ ࣮૷ *DDDࣗମͷ࿩Λ͢ΔΘ͚Ͱ͸ແ͍ͷͰৄࡉ͸ׂѪ

Slide 130

Slide 130 text

abstract class BaseRepository> implements ArticleRepositoryInterface { protected Map $collect = Map{}; public function add(T $article): void { $this->collect->add(Pair{$article->getID(), $article}); } // লུ <<__Rx>> public function findById(TId $id): T { if($this->collect->contains($id)) { return $this->collect->at($id); } throw new \RuntimeException('Not Found.'); } }

Slide 131

Slide 131 text

abstract class BaseRepository> implements ArticleRepositoryInterface { protected Map $collect = Map{}; public function add(T $article): void { $this->collect->add(Pair{$article->getID(), $article}); } // লུ <<__Rx>> public function findById(TId $id): T { if($this->collect->contains($id)) { return $this->collect->at($id); } throw new \RuntimeException('Not Found.'); } } TId : Identifier͸intಛఆͷܕ͔Ͳ͏͔͸ɻɻʁ T: EntityInterfaceΛ࣮૷ͯ͠Δ͕ɺTIdࢦఆ͞Εͨ΋ͷ

Slide 132

Slide 132 text

abstract class BaseRepository> implements ArticleRepositoryInterface { protected Map $collect = Map{}; public function add(T $article): void { $this->collect->add(Pair{$article->getID(), $article}); } // লུ <<__Rx>> public function findById(TId $id): T { if($this->collect->contains($id)) { return $this->collect->at($id); } throw new \RuntimeException('Not Found.'); } } ίϨΫγϣϯ͸ɺ ͷϚοϓʹ Ұ؏ੑ͕͋Γɺ͜ΕҎ֎ͷ΋ͷ͸ར༻͞Εͳ͍

Slide 133

Slide 133 text

> { <<__Rx>> public function latestArticles( DateTime $date ): Map> { return $this->collect->filter( $v ==> $v->createdAt() > $date ); } }

Slide 134

Slide 134 text

> { <<__Rx>> public function latestArticles( DateTime $date ): Map> { return $this->collect->filter( $v ==> $v->createdAt() > $date ); } } ͲΜͳܕΛར༻͢Δͷ͔ɺ۩৅ΫϥεͰ໌ه

Slide 135

Slide 135 text

> { <<__Rx>> public function latestArticles( DateTime $date ): Map> { return $this->collect->filter( $v ==> $v->createdAt() > $date ); } } ಛఆͷू໿ݶఆͷॲཧͳͲ͕͋Ε͹ɺ
 ΠϯλʔϑΣʔε௥Ճ΍ɺઐ༻ͷॲཧͷΈهड़

Slide 136

Slide 136 text

> { } ྫ͑͹ར༻͢Δܕ͚ͩΛม͑Δ ू໿ͷૢ࡞ͱͯ͠͸͜Ε͚ͩͰ׬݁
 ৄࡉͳॲཧ͸࢓༷ύλʔϯͳͲʹ

Slide 137

Slide 137 text

࢓༷ *DDDࣗମͷ࿩Λ͢ΔΘ͚Ͱ͸ແ͍ͷͰৄࡉ͸ׂѪ

Slide 138

Slide 138 text

࢓༷ύλʔϯ • ΞϓϦέʔγϣϯαʔϏεͳͲ͔Β
 ϦϙδτϦͱڞʹར༻͞ΕΔ • DBΞΫηε΍Կ͔͠Βͷ֎෦ϦιʔεΛ࢖͏΋ͷ΋
 ࢓༷ͱͯ͠੾Γग़͢

Slide 139

Slide 139 text

{ <<__Rx>> public function isSatisfiedBy(T $entity): bool; } ࢓༷Λຬͨ͢ͷ͔Ͳ͏͔

Slide 140

Slide 140 text

࢓༷ ࣮૷ *DDDࣗମͷ࿩Λ͢ΔΘ͚Ͱ͸ແ͍ͷͰৄࡉ͸ׂѪ

Slide 141

Slide 141 text

class LatestPostSpecification implements SpecificationInterface> { public function __construct( private DateTime $since ) {} <<__Rx>> public function isSatisfiedBy( Article $article ): bool { return $article->createdAt() > $this->since; } } ͜Ε·Ͱͷྫͱಉ༷ʹ۩৅Ϋϥεʹ
 ࣮ࡍʹద༻ͤ͞ΔܕΛهड़

Slide 142

Slide 142 text

class LatestPostSpecification implements SpecificationInterface> { public function __construct( private DateTime $since ) {} <<__Rx>> public function isSatisfiedBy( Article $article ): bool { return $article->createdAt() > $this->since; } } ࢦఆ͞Εͨ೔࣌ΑΓ΋৽͍͠هࣄͳΒ
 ࢓༷͕ຬͨ͞Ε·͢Α

Slide 143

Slide 143 text

<<__Rx>> public function query( SpecificationInterface $specification ): Map { return $this->collect->filter( $v ==> $specification->isSatisfiedBy($v) ); } ࢓༷ɾϦϙδτϦͱ૊Έ߹ΘͤΔ

Slide 144

Slide 144 text

ΞϓϦέʔγϣϯαʔϏε

Slide 145

Slide 145 text

use type Vendor\Path\ArticleRepositoryInterface as Repository; use type Vendor\Path\\ArticleSpecificationFactoryInterface as SpecificationFactory; final class LatestArticleFeed { public function __construct( private Repository> $repository, private SpecificationFactory> $specification ) { } ࢦఆͨ͠ܕΛར༻͢ΔΑ͏ʹద༻͞Εͨ ΠϯλʔϑΣʔεΛ࣮૷ͨ͠΋ͷΛར༻͍ͯͩ͘͠͞

Slide 146

Slide 146 text

public function execute( FeedRequestTransfer $request ): vec int, 'content' => string, 'created_at' => \DateTime)> { $result = $this->repository->query( $this->specificationFactory ->createLatestPosts($request->getDateTime() )); if(C\count($result)) { return Vec\map($result, ($v) ==> { return shape( 'id' => $v->getID(), 'content' => $v->body()->content(), 'created_at' => $v->createdAt() ); }); } return vec[]; } ࢓༷Λຬͨ͢هࣄΦϒδΣΫτΛϦϙδτϦ͔Βऔಘ

Slide 147

Slide 147 text

public function execute( FeedRequestTransfer $request ): vec int, 'content' => string, 'created_at' => \DateTime)> { $result = $this->repository->query( $this->specificationFactory ->createLatestPosts($request->getDateTime() )); if(C\count($result)) { return Vec\map($result, ($v) ==> { return shape( 'id' => $v->getID(), 'content' => $v->body()->content(), 'created_at' => $v->createdAt() ); }); } return vec[]; } Collectionૢ࡞
 ϦϙδτϦ͔Βऔಘ͞ΕͨΦϒδΣΫτΛҰׅૢ࡞ *͜͜Ͱ͸shapeͷΈͷvec഑ྻʹ

Slide 148

Slide 148 text

ΫϥΠΞϯτ

Slide 149

Slide 149 text

ܕએݴ͞ΕͨΠϯλʔϑΣʔεΛຬͨ͢
 ۩৅ΫϥεͷΠϯελϯε $service = new Application\Service\LatestArticleFeed( new Map\ArticleRepository(), new Map\ArticleSpecificationFactory() ); $result = $service->execute( new Application\FeedRequestTransfer([ 'datetime' => new DateTime('-4 hours'), ]) );

Slide 150

Slide 150 text

$service = new Application\Service\LatestArticleFeed( new Map\ArticleRepository(), new Map\ArticleSpecificationFactory() ); $result = $service->execute( new Application\FeedRequestTransfer([ 'datetime' => new DateTime('-4 hours'), ]) ); ϑΥʔϜ΍APIͳͲ͔Β஋Λૹ৴

Slide 151

Slide 151 text

final class FeedRequestTransfer { const type FeedRequest = shape( 'datetime' => DateTime ); public function __construct( private array $request ) {} public function getDateTime(): DateTime { $request = $this->request as this::FeedRequest; return $request['datetime']; } } Ұൠతͳ഑ྻΛड͚औΔ

Slide 152

Slide 152 text

final class FeedRequestTransfer { const type FeedRequest = shape( 'datetime' => DateTime ); public function __construct( private array $request ) {} public function getDateTime(): DateTime { $request = $this->request as this::FeedRequest; return $request['datetime']; } } ShapeͰ഑ྻʹରͯ͠ ظ଴͢ΔϑΟʔϧυͱܕఆٛ

Slide 153

Slide 153 text

final class FeedRequestTransfer { const type FeedRequest = shape( 'datetime' => DateTime ); public function __construct( private array $request ) {} public function getDateTime(): DateTime { $request = $this->request as this::FeedRequest; return $request['datetime']; } } ϑΟʔϧυͱܕݕࠪ OKͳΒͦͷ··഑ྻ NGͳΒType Error͕εϩʔ

Slide 154

Slide 154 text

·ͱΊ • Hack͸े෼঎༻ΞϓϦέʔγϣϯͰ׆༻ • ݎ͘࡞ΔɺܕΛҙࣝ͢Δ • Կࣄ΋΍Γ͗͢ʹ஫ҙ • ͦͷPHPΞϓϦέʔγϣϯɺ
 HackͰॻ͍ͯΈ·ͤΜ͔ʁ