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

実践DIコンテナ

 実践DIコンテナ

DIコンテナを使って役に立った場面をご紹介
PHP BLT#4 (http://phpblt.connpass.com/event/27655/) 発表資料

046baac588d91fd78a85b189847a151d?s=128

Sota Sugiura

April 20, 2016
Tweet

Transcript

  1. ࣮ફ%*ίϯςφ PO-BSBWFM !TPUB 1)1#-5

  2. ࣗݾ঺հ w ਿӜᰜଠ 4PUB4VHJVSB  w !TPUB w ΃ͪͺʔྺ೥ w

    גࣜձࣾΞΠελΠϧ೥໨ ʘ'PMMPXNFʗ
  3. ΈΜͳʮ%*ศརͩͧʂʯ

  4. ๻ʮͲ͏͍͏෩ʹʜʁʯ

  5. ͯ͜ͱͰ

  6. %*ίϯςφͷ࿩Λ͠·͢ ஌ͬͯΔͧͬͯํ͸πΠολʔͯ͠Ͳ͏ͧ Ňŏ﹏ŏ

  7. %*ͱ͸ w %*%FQFOEFODZ*OKFDUJPO w ʮґଘੑͷ஫ೖʯͱݴ͏ͱ͔ݴΘͳ͍ͱ͔ w ৄ͍͠આ໌͸࣌ؒͷ౎߹্Χοτ ứʆ

  8. %*ίϯςφͱ͸ w %*ύλʔϯͰґଘղܾ͞ΕͨΠϯελϯεΛฦ ͯ͘͠ΕΔγϩϞϊ w ༷ʑͳ࣮૷͕͋Δ͕ɺ͜͜Ͱ͸-BSBWFMΛྫʹ

  9. -BSBWFMʹ͓͚Δ %*ίϯςφ

  10. %*$POUBJOFSPO-BSBWFM w ίϯετϥΫλͰλΠϓώϯτ͓͍ͯͨ͠Ϋϥε Λ%*ίϯςφ͕஫ೖͯ͘͠ΕΔ w ඞཁͳΫϥεʹ͞ΒʹඞཁͳΠϯελϯε͕͋ Ε͹࠶ؼతʹΠϯελϯεΛ஫ೖ͢Δ

  11. %*$POUBJOFSPO-BSBWFM #SFBE

  12. %*$POUBJOFSPO-BSBWFM #SFBE *OFFE #BDPO -FUUVDFBOE5PNBUP public function __construct( Bacon $bacon,

    Lettuce $lettuce, Tomato $tomato ) { // }
  13. %*$POUBJOFSPO-BSBWFM %*$POUBJOFS (JWFZPV #SFBE *OFFE #BDPO -FUUVDFBOE5PNBUP #BDPO -FUUVDF 5PNBUP

  14. %*$POUBJOFSPO-BSBWFM %*$POUBJOFS *OFFE4BMU #SFBE #BDPO -FUUVDF 5PNBUP public function __construct(Salt

    $salt) { // }
  15. %*$POUBJOFSPO-BSBWFM %*$POUBJOFS (JWFZPV *OFFE4BMU #SFBE #BDPO -FUUVDF 5PNBUP 4BMU

  16. %*$POUBJOFSPO-BSBWFM %*$POUBJOFS /FFE /FFE /FFE #SFBE #BDPO -FUUVDF 5PNBUP 4BMU

    /FFE
  17. $MBTT#JOEJOH w %*ίϯςφʹ͋Β͔͡ΊΠϯελϯεΛಥͬࠐ ΊΔ

  18. $MBTT#JOEJOH <?php /* ུ */ /** * */ public function

    boot() { $this->app->bind( // ίϯετϥΫλͰλΠϓώϯτ͞ΕΔΫϥε໊ \App\Dinners\PotatoTips::class, function ($app) { // ࣮ࡍʹฦ͞ΕΔΠϯελϯε return new \App\Dinners\Blt; } ); }
  19. $MBTT#JOEJOHT "QQMJDBUJPO *OFFE 1PUBUP5JQT %*$POUBJOFS

  20. $MBTT#JOEJOHT "QQMJDBUJPO *OFFE 1PUBUP5JQT %*$POUBJOFS $this->app->bind( \App\Dinners\PotatoTips::class, function ($app) {

    return new \App\Dinners\Blt; } );
  21. $MBTT#JOEJOHT "QQMJDBUJPO *OFFE 1PUBUP5JQT %*$POUBJOFS (JWFZPV #MU

  22. $MBTT#JOEJOHT "QQMJDBUJPO *OFFE 1PUBUP5JQT %*$POUBJOFS (JWFZPV #MU ˞௨ৗͷλΠϓώϯτͱಉ༷ɺ ܧঝؔ܎ʹͳ͍ͱΤϥʔʹͳΓ·͢

  23. ͜͜·Ͱલ࠲

  24. ຊ୊ ͜ͷ%*ίϯςφ͕໾ʹཱͬͨ৔໘Λ͝঺հʂʂʂ

  25. ͦͷ ࣮૷ͷࠩ͠ସ͑

  26. Ғ͍ਓʮந৅ʹґଘͤΑʯ w Α͋͘Δ%*ίϯςφͷ࢖͍ํͱͯ͠ந৅Ϋϥε ʹ۩৅ΫϥεΛ$MBTT#JOEJOH͢Δ

  27. *OUFSGBDFBOE$MBTT interface Burger { public function addTopping($toppingName); } class Blt

    implements Burger { public function addTopping($toppingName) { // } }
  28. #JOE*OUFSGBDF /* in DI Container */ public function boot() {

    // ΠϯλʔϑΣʔεʹ۩৅ΫϥεΛbind // BurgerΠϯλʔϑΣʔεΛλΠϓώϯτ͢Δͱ // BltΫϥε͕஫ೖ͞ΕΔ $this->app->bind(Burger::class, Blt::class); }
  29. ಥવͷϦΫΤετ ʮԶ͸#-5͡Όͳͯ͘ςϦϠΩόʔΨʔ͕৯΂͍ͨʯ

  30. ๻ʮطଘίʔυΛॻ͖׵͑ͣʹ ཁ๬ʹ౴͍͑ͨʯ

  31. ͦ͏ͩʂந৅ͩʂ w ͜ͷ࣌ɺٻΊΒΕͯΔͷ͸#VSHFSΠϯλʔ ϑΣʔεΛ࣮૷ͨ͠৽ͨͳόʔΨʔ w ͭ·Γ಺෦తͳ࣮૷͕Ͳ͏มΘΖ͏ͱͦͷந৅ Λ࣮૷͢Ε͹ࠩ͠ସ͕͑Մೳ

  32. /FX$MBTT interface Burger { public function addTopping($toppingName); } class Blt

    implements Burger { public function addTopping($toppingName) { // } } class Teriyaki implements Burger { public function addTopping($toppingName) { // } }
  33. $IBOHF$MBTT#JOEJOH /* in DI Container */ public function boot() {

    // ݩʑͷΫϥε // $this->app->bind(Burger::class, Blt::class); // ৽ࠩ͘͠͠ସ͑ΔΫϥεΛBinding $this->app->bind(Burger::class, Teriyaki::class); }
  34. .JTTJPO$PNQMFUF 㱼ʆ w ந৅ͱ%*ίϯςφΛ͏·͘ѻͬͯطଘίʔυʹ खΛೖΕͣʹ৽͍͠ίʔυʹࠩ͠ସ͑Δ͜ͱ͕ Մೳ w %*ίϯςφ͚ͩͷ࿩Ͱ͸ͳ͍ΜͰ͕͢ศརͩΑ ͱ͍͏ʜͶʜ

  35. ͦͷ ςετ࣮૷

  36. ྫ͑͹͜ΜͳςετΛॻ͖͍ͨ

  37. 501ϖʔδʹΞΫηεͨ͠Β ͪΌΜͱϖʔδ͕දࣔ͞ΕΔ͔

  38. "QQMJDBUJPO5FTUJOH w %#ʹ໰͍߹Θͤͯฦ͖ͬͯͨ΋ͷ͕501ϖʔδ ʹग़Δ͸ͣʜ w ͖ͪΜͱɺ͔ͭ%#͔ΒͷϨεϙϯε͕ࡌͬ ͯΕ͹0,

  39. "QQMJDBUJPO5FTUJOH /** * TOPϖʔδʹΞΫηεͨ͠Β’username'ͷจࣈྻ͕͋Γɺ * HTTPεςʔλείʔυ͕200Ͱ͋Δ͜ͱ */ public function testAccessTopPage()

    { $this->visit('/top') ->see(‘username') ->assertResponseOk(); }
  40. ೰Έ w ςετέʔε࣮ߦ͞ΕΔ౓ʹ%#ΞΫηε͢Δͷ πϥΠ w ੒ޭ༻ࣦഊ༻ͷσʔλΛέʔε͝ͱʹ༻ҙ͢Δ ͷπϥΠ w ςετલޙͰ%#ͷ͓૟আͱ͔πϥΠ

  41. 6TF%*$POUBJOFS ͦΕɺ%*ίϯςφͰղܾͰ͖·͢Α

  42. $MBTT#JOEJOH w σʔλʹΞΫηεͯ͠ΔΫϥε΋%*ίϯςφ͔ Β஫ೖ͞Ε͍ͯΔ w ςετத͚ͩσʔλΞΫηεΫϥεʹߜͬͯϞο ΫΫϥεʹ#JOEJOHͯ͠͠·͑͹͓̺

  43. .BLJOH.PDL$MBTT // σʔλΞΫηεΫϥεͷϞοΫ࡞੒ $userMock = \Mockery::mock(\User::class); $userMock->shouldReceive(‘getUsers')->andReturn('username');

  44. #JOEJOH.PDL$MBTT // ϞοΫΫϥεΛBinding $this->app->bind(\User::class, function () use ($userMock) { return

    $userMock; });
  45. 5FTUJOH public function testAccessTopPage() { // σʔλΞΫηεΫϥεͷϞοΫ࡞੒ $userMock = \Mockery::mock(\User::class);

    $userMock->shouldReceive('getUsers')->andReturn('username'); // ϞοΫΫϥεΛBinding $this->app->bind(\User::class, function () use ($userMock) { return $userMock; }); // ςετ࣮ߦ $this->visit('/top') ->see('username') ->assertResponseOk(); }
  46. .JTTJPO$PNQMFUF 㶅㱿㶅 w ष͍΋ͷʹ֖ςετͷ໨తΛՌͨͨ͢ΊʹΞϓ ϦέʔγϣϯͷҰ෦෼͚ͩϞοΫͰࠩ͠ସ͑Δ ͜ͱ͕Ͱ͖ͨʂ w ΞΫηεςετͰͳͯ͘΋͍Ζ͍Ζͳͱ͜ΖͰ Ԡ༻Մೳ w

    "1*ΞΫηεͷϞοΫͱ͔
  47. ·ͱΊ w %*ίϯςφ͸Ϋϥεͷґଘ͕ղܾ͞ΕͨΠϯε λϯεΛฦͯ͘͠ΕΔຐ๏ͷശ w ͏·͘པΕ͹ςετ΍࣮૷ͷࠩ͠ସ͕͑ͱͯ΋ ؆୯@ ᵇʮЏ @ w

    ΠέͯΔ%*ίϯςφϥΠϒϥϦ͸Ұഋ͋ΔͷͰ ֤ࣗ(PPHMFઌੜʂ
  48. Ҏ্ʂ