Upgrade to Pro — share decks privately, control downloads, hide ads and more …

DI Container Museum@PHP

DI Container Museum@PHP

YAP(achimon)C::Asia Hachioji 2016

046baac588d91fd78a85b189847a151d?s=128

Sota Sugiura

July 01, 2016
Tweet

Transcript

  1. %*$POUBJOFS.VTFVN !1)1 !TPUB :"1 BDIJNPO $"TJB)BDIJPKJ !඼઒

  2. ࣗݾ঺հ w ਿӜᰜଠ 4PUB4VHJVSB  w !TPUB w גࣜձࣾΞΠελΠϧ೥໨ w

    *1)1+BWB4DSJQU
  3. ຊ೔ͷ࿩

  4. %FQFOEFODZ*OKFDUJPO $POUBJOFS

  5. ͦͷલʹʜ ͦ΋ͦ΋%FQFOEFODZ*OKFDUJPOͬͯͳΜͧ

  6. %FQFOEFODZ *OKFDUJPO

  7. ίϯϙʔωϯτؒͷґଘΛ ؇࿨͢Δ

  8. 6TFSQIQ <?php class User { protected $db; public function __construct()

    { $this->db = new DB('mysql'); } public function getUsers() { return $this->db->table('users')->selectAll(); } }
  9. ΋͜͠ΜͳίʔυΛॻ͘ͱ w ͍ΖΜͳ͓͡͞Μʹࡴ͞ΕΔ w ૄ݁߹͓͡͞Μ w ςετ͓͡͞Μ w ΦϒδΣΫτࢦ޲͓͡͞Μ

  10. None
  11. Կ͕μϝͳͷ͔

  12. ϨϏϡʔλΠϜ w %#Ϋϥεͷ"1*͕มΘͬͨΒ6TFSQIQΛಡ· ͳ͖Ό͍͚ͳ͍ w σʔλϕʔε͕มߋ͞ΕͨΒ6TFSQIQΛมߋ͠ ͳ͖Ό͍͚ͳ͍ w ςετ࣌͸σʔλϕʔε઀ଓΛϞοΫ͍ͨ͠Μͩ ͚ͲͲ͏࣮ݱ͢Δͷʁ

  13. <?php class User { protected $db; public function __construct() {

    $this->db = new DB('mysql'); } public function getUsers() { return $this->db->table('users')->selectAll(); } } 6TFSQIQ %#Ϋϥεͷ"1*มߋͷӨڹΛड͚Δ
  14. <?php class User { protected $db; public function __construct() {

    $this->db = new DB('mysql'); } public function getUsers() { return $this->db->table('users')->selectAll(); } } 6TFSQIQ σʔλϕʔεมߋ࣌ʹ Ҿ਺Λॻ͖׵͑ͳ͚Ε͹͍͚ͳ͍
  15. <?php class User { protected $db; public function __construct() {

    $this->db = new DB('mysql'); } public function getUsers() { return $this->db->table('users')->selectAll(); } } 6TFSQIQ ͜ͷ஋ΛͲ͏΍ͬͯϞοΫ͢Δͷʁ
  16. ґଘؔ܎ w ͭ·Γɺ6TFSΫϥε͸%#ΫϥεͷมߋͷӨڹ Λड͚΍͍͢ w 6TFSΫϥε͸%#Ϋϥε΁ͷґଘ౓͕ߴ͍

  17. ꒪⌓꒪

  18. ղܾΛࢼΈΔ

  19. 6TFSQIQ <?php class User { protected $db; public function __construct()

    { $this->db = new DB('mysql'); } public function getUsers() { return $this->db->table('users')->selectAll(); } }
  20. 6TFSQIQ <?php class User { protected $db; public function __construct()

    { $this->db = new DB('mysql'); } public function getUsers() { return $this->db->table('users')->selectAll(); } } ͜ͷॲཧ͕ݪҼͬΆ͍
  21. .BJOQIQ <?php // $user = new User(); $user = new

    User(new DB('mysql'));
  22. 6TFSQIQ <?php class User { protected $db; public function __construct($db)

    { $this->db = $db; } public function getUsers() { return $this->db->table('users')->selectAll(); } }
  23. 6TFSQIQ <?php class User { protected $db; public function __construct(DB

    $db) { $this->db = $db; } public function getUsers() { return $this->db->table('users')->selectAll(); } } 5ZQF)JOU
  24. .BJOQIQ <?php // Success $user = new User(new DB('mysql')); //

    PHP Warning: Uncaught TypeError $badUser = new User('DB');
  25. 5FTUQIQ <?php use Mockery; $dbMock = Mockery::mock(DB::class); $user = new

    User($dbMock);
  26. ϨϏϡʔλΠϜ w σʔλϕʔε͕มߋ͞Εͯ΋6TFSΫϥε͸ॻ͖ ׵͑ͳͯ͘Α͍ w ςετ࣌ʹϞοΫΛίϯετϥΫλʹ౉͢͜ͱ ͕Ͱ͖ͦ͏ w %#ͷ"1*͕มΘΔͱ6TFSΫϥεΛॻ͖׵͑ͳ ͖Ό͍͚ͳ͍໰୊͸มΘΒͣ

  27. "1*ͷมߋʹڧ͍ΠέͯΔ࢓૊Έ͕ཉ͍͠  P ɻ 㵺㱿㵺 

  28. *OUFSGBDF

  29. 8JUIPVU*OUFSGBDF 6TFS %# *OKFDUJPO

  30. 8JUI*OUFSGBDF 6TFS %#*OUFSGBDF 5ZQF)JOU %# *NQMFNFOU *OKFDUJPO

  31. %#*OUFSGBDFQIQ <?php interface DBInterface { public function table($table); public function

    selectAll(); }
  32. 6TFSQIQ <?php class User { protected $db; public function __construct(DBInterface

    $db) { $this->db = $db; } public function getUsers() { return $this->db->table('users')->selectAll(); } } *OUFSGBDF
  33. .BJOQIQ <?php // Success $user = new User(new DB('mysql')); //

    PHP Warning: Uncaught TypeError $badUser = new User(new ViolateDB('mysql'));
  34. ϨϏϡʔλΠϜ w σʔλϕʔε͕มߋ͞Εͯ΋6TFSΫϥε͸ॻ͖ ׵͑ͳͯ͘Α͍ w ςετ࣌ʹϞοΫΛίϯετϥΫλʹ౉͢͜ͱ ͕Ͱ͖ͦ͏ w Πϯελϯεͷ"1*͕มߋ͞Εͯ΋*OUFSGBDF͞ ͑कΒΕ͍ͯΕ͹ॻ͖׵͑ͳͯ͘Α͍

  35. ґଘݮΒͤͨ٩ ˊᗜˋ و

  36. *U`T%FQFOEFODZ*OKFDUJPO w ͜Ε͕%*ύλʔϯͷجຊܕ w ΫϥεͷதͰͳ͘ɺ֎Ͱґଘ͢ΔΦϒδΣΫτ Λ࡞Γ֎͔Β஫ೖ͢Δ w ͜Ε͸ҰྫͰɺଞʹ΋༷ʑͳΞϓϩʔν͕͋Δ

  37. ,JOEPG%* w ίϯετϥΫλΠϯδΣΫγϣϯ w ηολʔΠϯδΣΫγϣϯ w ϝιουΠϯδΣΫγϣϯ w ϑΟʔϧυΠϯδΣΫγϣϯ

  38. ๻ʮͬͦ͘͞ΞϓϦॻ͜͏ʯ

  39. $POUSPMMFSQIQ <?php class Controller { public function getIndex() { $service

    = new UserService( new UserRepository( new DB('mysql'), new Cache('redis') ), new BoardRepository( new DB('mysql'), new Cache('memcached') ) ); return $service->getIndexPage(); } }
  40. $POUSPMMFSQIQ <?php class Controller { public function getIndex() { $service

    = new UserService( new UserRepository( new DB('mysql'), new Cache('redis') ), new BoardRepository( new DB('mysql'), new Cache('memcached') ) ); return $service->getIndexPage(); } } ᶃෳࡶ͗͢Δ
  41. $POUSPMMFSQIQ <?php class Controller { public function getIndex() { $service

    = new UserService( new UserRepository( new DB('mysql'), new Cache('redis') ), new BoardRepository( new DB('mysql'), new Cache('memcached') ) ); return $service->getIndexPage(); } } ᶃෳࡶ͗͢Δ ᶄ݁ہOFXͯ͠Δ
  42. δϨϯϚ w %*ύλʔϯʹΑΓۃݶ·Ͱґଘ౓ΛݮΒ͢͜ͱ ͸Մೳ w ͕ͩ݁ہͲ͔͜ͰΠϯελϯε͸࡞Βͳ͖Ό͍ ͚ͳ͍ w ΞϓϦέʔγϣϯ͕ڊେʹͳΕ͹ͳΔ΄ͲΠϯ ελϯεੜ੒͕ෳࡶʹͳ͍ͬͯ͘

  43. δϨϯϚ w *OUFSGBDFΛ࢖༻ͯ͠ૄ݁߹Խͯ͠Δ৔߹͸ந৅ Ϋϥεʹର͢Δ۩৅ΫϥεͷϚοϐϯά͕ඞཁ w ڊେʹͳΕ͹ͳΔ΄Ͳ؅ཧ͕൥ࡶʹ

  44. None
  45. ઃఆΛར༻͔Β෼཭͠Α͏

  46. ઃఆͱ͸ΦϒδΣΫτΛ࡞Δํ๏ ར༻ͱ͸ΞϓϦέʔγϣϯຊମ

  47. ղܾ͍ͨ͜͠ͱ w ΞϓϦͰ͸ٻΊΔ΋ͷ͕༩͑ΒΕΕ͹ಈ͘ίʔ υΛॻ͖͍ͨ w ͲͷΦϒδΣΫτʹԿ͕஫ೖ͞ΕΔ͔ͷ؅ཧ͸ ͳΔ΂ͨ͘͘͠ͳ͍

  48. ͓ɹ·ɹͨɹͤ ͔ͬ͜Βຊฤ

  49. %FQFOEFODZ*OKFDUJPO $POUBJOFS

  50. %*$POUBJOFS w ΦϒδΣΫτ΍ΦϒδΣΫτͷੜ੒ํ๏Λอ࣋ ͢Δശ w %*ίϯςφ͕Πϯελϯεͷੜ੒ํ๏Λ஌ͬͯ ͍Δ w ΞϓϦέʔγϣϯ͕%*ίϯςφ͔Βඞཁͳ΋ͷ ΛऔΓग़͚ͩ͢

  51. %*$POUBJOFS͸πʔϧ w %*͸σβΠϯύλʔϯ͕ͩ%*ίϯςφ͸σβΠ ϯύλʔϯͰ͸ͳ͍ w %*ύλʔϯΛ࠾༻࣮ͨ͠૷Λڧྗʹޙԡ͢͠Δ ͨΊͷπʔϧͱࢥͬͯྑ͍ w ͳͷͰ༷ʑͳΞϓϩʔν͕͋Δ

  52. ཁ͢Δʹ %*$POUBJOFS (FU6TFS4FSWJDF "QQMJDBUJPO

  53. ཁ͢Δʹ %*$POUBJOFS "QQMJDBUJPO ΰχϣͬͱ ΠϯελϯεΛ࡞Δ

  54. ཁ͢Δʹ %*$POUBJOFS "QQMJDBUJPO 6TFS4FSWJDF ׬੒͞Εͨ6TFS4FSWJDF Πϯελϯε͕ฦͬͯ͘Δ Πϯελϯεͷੜ੒ํ๏Λ ஌ΒͣʹखʹೖΕΔ

  55. γϯϓϧʹߟ͑Α͏ w %*ίϯςφͷ௒جຊػೳ͸ొ࿥ͱऔಘ w ޷͖ͳΦϒδΣΫτΛొ࿥͢Δ w ొ࿥ͨ͠ΦϒδΣΫτΛऔಘ͢Δ

  56. ͬͦ͘͞࢖͍͍ͨʂ ͲΜͳ΋ͷ͕͋ΔΜͰ͔͢ʁ Φεεϝ͸ʁ

  57. 1)1%*$POUBJOFST w QJNQMF w BVSBEJ w 1)1%* w %JDF w

    UIFQIQMFBHVF w TZNGPOZ w "VSZO w [FOETFSWJDFNBOBHFS w BOEFUD
  58. 1)1%*$POUBJOFST w QJNQMF w BVSBEJ w 1)1%* w %JDF w

    UIFQIQMFBHVF w TZNGPOZ w "VSZO w [FOETFSWJDFNBOBHFS w BOEFUD ଟ͍
  59. ϥΠϒϥϦ࣠Ͱͳ͘ ख๏࣠Ͱ͝Ҋ಺

  60. ϥΠϒϥϦ࣠Ͱͳ͘ ख๏࣠Ͱ͝Ҋ಺ 1JNQMF "VSB 4ZNGPOZ 1)1%* ̃⶿䘞☏匝Օ׋ֆָ׋׌ս֡֒Ւ媈䭚ՂթՄ

  61. ίϯςφ΁ͷొ࿥

  62. LFZWBMVFܗࣜͰͷొ࿥ w ೚ҙͷ஋ͰΦϒδΣΫτΛొ࿥ w αʔϏεϩέʔλͱͯ͠΋࢖͑Δ w ࣗ༝౓͕ߴ͍ w %*ίϯςφͷجຊͱݴͬͯ΋աݴͰͳ͍ 1JNQMF

    "VSB 4ZNGPOZ 1)1%* "VSZBO QIQMFBHVF ;FOE %JDF
  63. LFZWBMVF QJNQMF <?php use Pimple\Container; $container = new Container(); //

    Register instance. $container['session'] = function ($c) { return new App\Session('SESSION_ID'); };
  64. Ϋϥε໊Ͱͷొ࿥ w Ϋϥε໊ͰΦϒδΣΫτΛొ࿥ w LFZWBMVFΑΓগ੍͠໿͕ڧ͍ w %*ύλʔϯΛ࣮ݱ͠΍͍͢ 1JNQMF "VSB 4ZNGPOZ

    1)1%* "VSZBO QIQMFBHVF ;FOE %JDF
  65. $MBTTOBNF QIQMFBHVF <?php $container = new League\Container\Container; // Register $container->add('SessionInterface',

    'Session');
  66. -B[ZMPBE ஗Ԇొ࿥ w ొ࿥͢Δࡍʹ͸ΦϒδΣΫτΛ࡞Βͳ͍ w औಘ͞ΕΔλΠϛϯάͰΦϒδΣΫτΛੜ੒ w %*ίϯςφͷىಈ࣌ؒͷઅ໿ʹͳΔ "VSB 4ZNGPOZ

    1)1%* QIQMFBHVF ;FOE
  67. γϯάϧτϯ w ΦϒδΣΫτΛγϯάϧτϯͱͯ͠ొ࿥ w औಘ͞ΕΔΦϒδΣΫτ͸ৗʹಉ͡ w ࢖͍ա͗Δͱςετ͕ࠔ೉ʹɻΑ͘ߟ͑Α͏ʂ 1JNQMF "VSB 4ZNGPOZ

    1)1%* "VSZBO QIQMFBHVF ;FOE %JDF
  68. *OKFDU"SHVNFOUT

  69. Ҿ਺ࢦఆొ࿥ w ొ࿥͢ΔࡍʹҾ਺ʹԿΛೖΕΔ͔ࢦఆ͢Δ w ίϯετϥΫλ΍ηολʔɺ5SBJUͳͲʹࢦఆ w ந৅ʹରͯ͠ͷ۩৅Ϛοϐϯά͕Մೳ "VSB 4ZNGPOZ 1)1%*

    "VSZBO QIQMFBHVF ;FOE %JDF
  70. $POTUSVDUPS*OKFDUJPO BVSBEJ <?php $builder = new Aura\Di\ContainerBuilder; $di = $builder->newInstance();

    // Register $di->params['Example']['foo'] = new Foo();
  71. 4FUUFS*OKFDUJPO BVSBEJ <?php $builder = new Aura\Di\ContainerBuilder; $di = $builder->newInstance();

    // Register $di->setters['Example']['setFoo'] = new Foo;
  72. ϑΟʔϧυΠϯδΣΫγϣϯ w ϓϩύςΟΛηοτ͢Δ w Ξϊςʔγϣϯ΍ίϝϯτΛ%*ίϯςφ͕ղऍ ͠ɺඞཁͳ෺Λ൑ผ͢Δ w ϞϊʹΑͬͯ͸ίϯςφʹґଘ͢ΔͷͰ஫ҙ 4ZNGPOZ 1)1%*

  73. "OOPUBUJPO 1)1%* <?php class Yapc { /** * @Inject *

    @var Talk */ private $talk; /** * @Inject * @param Talk $talk */ public function __construct($talk) { } }
  74. 1SPQFSUZ 4ZNGPOZ <?php use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Reference; $container->setDefinition('talk', ...); $container->setDefinition('yapc',

    new Definition( 'Yapc' ))->setProperty('talk', new Reference('talk'));
  75. 'BDUPSZ w $MPVTFSΛొ࿥͢Δ w ෳࡶͳΠϯελϯεੜ੒ΛࢦఆͰ͖Δ w Πϯελϯεੜ੒ΛϑΝΫτϦʔʹ෼཭͢Δͷ ͕༰қʹ 1JNQMF "VSB

    4ZNGPOZ 1)1%* "VSZBO QIQMFBHVF ;FOE %JDF
  76. 4BNQMF ;FOE <?php use Interop\Container\ContainerInterface; use Zend\ServiceManager\Factory\InvokableFactory; use Zend\ServiceManager\ServiceManager; use

    stdClass; $serviceManager = new ServiceManager([ 'factories' => [ stdClass::class => InvokableFactory::class, MyObject::class => function (ContainerInterface $c, $name) { return new MyObject($c->get('obj')); }, ] ]);
  77. BVUPXJSJOH w %*͕ղऍͰ͖Δ৔߹ɺશͯࣗಈͰొ࿥͞ΕΔ w ٻΊΔΠϯελϯε͕ඞཁͳґଘΛղܾ͢Δ "VSB 4ZNGPOZ 1)1%* "VSZBO QIQMFBHVF

    ;FOE %JDF
  78. 4BNQMF BVSBEJ <?php $builder = new Aura\Di\ContainerBuilder; // Use auto

    wiring $di = $builder->newInstance($builder::AUTO_RESOLVE); // Get instance $userService = $di->newInstance('UserService');
  79. ίϯςφ͔Βͷऔಘ

  80. LFZWBMVFܗࣜͰͷऔಘ w ೚ҙͷ஋ͰΦϒδΣΫτΛऔಘ w ΞϓϦέʔγϣϯ಺Ͱ࢖͏ͱαʔϏεϩέʔλ ʹͳΔ 1JNQMF "VSB 4ZNGPOZ 1)1%*

    "VSZBO QIQMFBHVF ;FOE %JDF
  81. (FUCZLFZOBNF QJNQMF <?php use Pimple\Container; $container = new Container(); //

    Register instance. $container['session'] = function ($c) { return new App\Session('SESSION_ID'); }; // Get instance. $session = $container['session'];
  82. Ϋϥε໊Ͱͷऔಘ w Ϋϥε໊ͰΦϒδΣΫτΛऔಘ w BVUPXJSJOH͕͋Δ΋ͷ͸ొ࿥͞Εͯͳ͍΋ͷ ΋औಘͰ͖Δ 1JNQMF "VSB 4ZNGPOZ 1)1%*

    "VSZBO QIQMFBHVF ;FOE %JDF
  83. (FUCZDMBTTOBNF BVSBEJ <?php $builder = new Aura\Di\ContainerBuilder; $di = $builder->newInstance();

    // Register $di->params['Example']['foo'] = new Foo(); // Get instance $example = $di->newInstance('Example');
  84. (FUCZDMBTTOBNF "VSZBO <?php $injector = new Auryn\Injector; $userService = $injector->make('UserService');

  85. جຊ͸ొ࿥ͱऔಘ w ొ࿥ͱऔಘͷ֓೦͚ͩΘ͔Ε͹͍͍ͩͨҰॹ w ΞϓϦέʔγϣϯͷن໛΍࣮૷Ξϓϩʔνɺ޷ ΈͰ%*ίϯςφΛબͿ΂͠ w ύϑΥʔϚϯε΍"1*ͷॆ࣮౓΋ݟΔͱָ͍͠

  86. 143

  87. 143 w 143Ͱ%*$POUBJOFSͷΠϯλʔϑΣʔεΛܾΊ Α͏ͱ͍͏࿩͕͋Δ w ݱࡏͷϑΣʔζ͸%SBGU w 3FWJFXʹ͕͋Δؾ഑͸ແ͍ؾ͕͢Δɻɻɻ

  88. Կ͕ܾΊΒΕ͍ͯΔͷ͔

  89. ίϯςφ͔Βͷ औಘΠϯλʔϑΣʔε

  90. HFU  IBT

  91. HFU ΦϒδΣΫτΛऔಘ IBT ΦϒδΣΫτͷ༗ແΛௐ΂Δ

  92. DPOUBJOFSJOUFSPQ w ݱ࣌఺ͷ143Λ࣮૷ͨ͠ΠϯλʔϑΣʔε w ΦϒδΣΫτͷऔಘํ๏ͷΈఆٛ͞Ε͍ͯΔ w ޙ͸ྫ֎͕ͭ w ඇৗʹγϯϓϧ IUUQTHJUIVCDPNDPOUBJOFSJOUFSPQDPOUBJOFSJOUFSPQ

  93. BDDMJNBUFDPOUBJOFS w DPOUBJOFSJOUFSPQʹै͍ͬͯΔίϯςφΛ݁ ߹Ͱ͖Δ w ίϯςφͷࠩ͠ସ͑ͷϋʔυϧ͕௿͘ͳΔ IUUQTHJUIVCDPNKFSFNFBNJBBDDMJNBUFDPOUBJOFS

  94. 143͸͍ܾͭ·Δʁ w ਖ਼௚Θ͔ΒΜ ɾТɾʆ  w ͦ΋ͦ΋ඞཁ͋Δͷ͔ w ࠓ͸DPOUBJOFSJOUFSPQΛಡΊ͹ॆ෼ w

    ෼ͰಡΊ·͢
  95. ·ͱΊ

  96. %*$POUBJOFSͷັྗ

  97. ઃఆΛར༻͔Β׬શʹ ෼཭Ͱ͖Δ

  98. ొ࿥ͱऔಘͷ֓೦Λ ֮͑ͯ

  99. ܅͚ͩͷ࠷ڧͷ ΞϓϦέʔγϣϯΛखʹೖΕΖ ٩ ˊᗜˋ و

  100. ͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠