PSRとDIとフレームワーク #phpkansai

PSRとDIとフレームワーク #phpkansai

PHPカンファレンス関西2017 パネルディスカッションのスライドです

8c1ecc738eee0254a41c2d4b33cda7b6?s=128

Hisateru Tanaka

July 15, 2017
Tweet

Transcript

  1. ϑϨʔϜϫʔΫ 143ͱ %*ͱ

  2. 1)1JT

  3. 8FCϑϨʔϜϫʔΫ ଟ͗͢ݴޠ

  4. '"2 ʮͲΕ͕ྑ͍Ͱ͔͢ ʯ

  5. ओͳϑϧελοΫ'8 ػೳൺֱ

  6. 4ZNGPOZ $BLF1)1 -BSBWFM :JJ $PEF*HOJUFS 3PVUJOH P P P P

    P %BUBCBTF P P P P P 03. P P P P $BDIF P P P P P 7JFXIFMQFST P P P P P 'PSNWBMJEBUJPO P P P P P -PHHJOH P P P P P $43'QSPUFDUJPO P P P P P 5FTUTVQQPSU P P P P P %FCVH6* P P P P *:3rd party
  7. ͍͍͓ͩͨΜͳ͡΍Μ ·͢·͢Θ͔ΒΜ

  8. ྺ࢙

  9. 1)1 1)1 1)1 ;FOE'SBNFXPSL 4ZNGPOZ $BLF1)1 $PEF*HOJUFS -BSBWFM 4QSJOH 3VCZPO3BJMT

    ʙ      :JJ 
  10. 4QSJOH΍3BJMT͔Β ΍΍஗Εͨఔ౓ +BWB3VCZͷӨڹΛड͚ͯͷ஀ੜͳͷͰ

  11. ͜Ε͚ͩͷ௕ظؒ ࢥ૝͕౫ଡ͞Εͣ ࢖͍ଓ͚ΒΕ͍ͯΔ ͱ͍͏͜ͱ͸

  12. 1)1ͷ'8ɺຊ࣭తʹ ͦΕͧΕҧ͏໾໨Λ ࣋ͭͷͰ͸

  13. 8FCϑϨʔϜϫʔΫ ଟ͗͢ݴޠJO ͳͷͰจԽత ֶͼͷଟ͍

  14. ͍·ɺҎདྷͷ 1)1ϑϨʔϜϫʔΫ ܹಈͷ࣌୅͔ 143ొ৔

  15. ύωϥʔ঺հ

  16. 143

  17. 1)1 4UBOEBSET 3FDPNNFOEBUJPO 1)1ඪ४ͷਪ঑ࣄ߲

  18. 1)1'SBNFXPSL *OUFSPQFSBCJMJUZ(SPVQ 1)1ϑϨʔϜϫʔΫ૬ޓӡ༻άϧʔϓ 1)1'*( ֤ϑϨʔϜϫʔΫؒͰڞ௨ͷ͖·ΓΛઃ͚Α͏ ಛఆϑϨʔϜϫʔΫઐ༻ͷϥΠϒϥϦ͸࢖͍ʹ͍͘ ࢖༻தͷϑϨʔϜϫʔΫ͕ϥΠϒϥϦͷઃܭͱ߹Θͳ͍ ͦ͏͍͏ͷ΍ΊΑ͏

  19. ίʔσΟϯάن໿143 w 143143Ϋϥεͷ໋໊ͱ഑ஔ w 143143143 ESBGU ίʔσΟϯάελΠϧ w 143 ESBGU

    EPDίϝϯτ
  20. ΠϯλʔϑΣʔε143 w 143ϩΨʔΠϯλʔϑΣʔε w 143143ΩϟογϯάΠϯλʔϑΣʔε w 143)551ϝοηʔδΠϯλʔϑΣʔε w 143ϋΠύʔϦϯΫΠϯλʔϑΣʔε w

    ͦͷଞESBGUෳ਺
  21. <?php namespace Psr\Log; interface LoggerInterface { public function emergency($message, array

    $context = array()); public function alert($message, array $context = array()); public function critical($message, array $context = array()); public function error($message, array $context = array()); public function warning($message, array $context = array()); public function notice($message, array $context = array()); public function info($message, array $context = array()); public function debug($message, array $context = array()); public function log($level, $message, array $context = array()); } ྫ 143-PHHFS*OUFSGBDF
  22. /PX*O3FWJFX

  23. 143 $POUBJOFS*OUFSGBDF ίϯςφɾΠϯλʔϑΣʔε

  24. %# -PH 4FDVSJUZ αʔϏε ϑϨʔϜϫʔΫͷΠϝʔδ ࣮૷

  25. $logger = new SuperFlexibleLogger(); $logTarget = new FileLogTarget('/var/log/myapp.log'); $logTarget->setLevel(INFO |

    WARNING | ERROR); $logger->addTarget($logTarget); $logger->error('...'); ͱ͋ΔϩΪϯάϥΠϒϥϦΛ࢖͍͍ͨͱࢥͬͨ৔߹
  26. $logger = new SuperFlexibleLogger(); $logTarget = new FileLogTarget('/var/log/myapp.log'); $logTarget->setLevel(INFO |

    WARNING | ERROR); $logger->addTarget($logTarget); $logger->error('...'); ͱ͋ΔϩΪϯάϥΠϒϥϦΛ࢖͍͍ͨͱࢥͬͨ৔߹ ຖճίϐϖ΍Ίͯ
  27. $logger = $container->get('logger'); $logger->error('...'); ઃఆࡁΈͷϩΨʔΛԿ͔ʹอ࣋ͤ͞ αʔϏεͱͯ͠࠶ར༻ Log::error('...'); ͨͿΜ͜Ε΋தͰಉ͜͡ͱͯ͠·͢Ͷ

  28. function doSomething(LoggerInterface $logger) { // ... $logger->error('...'); } ͞Βʹɺ࠾༻ͨ͠ϥΠϒϥϦ͕ 143ʹ४ڌ͍ͯ͠Ε͹

    ࣮૷ϥΠϒϥϦΛ໰Θͣ࢖͍ํ͕ಉ͡ ͜ͷΠϯλʔϑΣʔεͷൣғͰ࢖͏͔͗Γ
  29. $container->get('service'); $container->has('service'); 143 ͦ͏ͩɺαʔϏεࣗମ͚ͩͰͳ͘ ͦͷೖΕ෺ʹ΋ඪ४ΠϯλʔϑΣʔεΛ

  30. ϑϨʔϜϫʔΫ࡞ऀ ϥΠϒϥϦ࡞ऀ ΞϓϦέʔγϣϯ։ൃऀ PSRs with PSR-11

  31. ͱ͜Ζ͕

  32. 1.3 Recommended usage Users SHOULD NOT pass a container into

    an object so that the object can retrieve its own dependencies. This means the container is used as a Service Locator which is a pattern that is generally discouraged.
  33. ίϯςφࣗମΛର৅ΦϒδΣΫτʹ౉ͯ͠ɺࣗྗ Ͱίϯςφ͔ΒґଘΛऔΓग़ͤ͞ΔΑ͏ͳ࢖͍ํ ͸4)06-%/05ͩͥɻ ίϯςφΛ͋ͷʮͩΊͩ͜ΓΌʯ͋Δ͋Δͳʮαʔ Ϗεϩέʔλʔʯͱͯ͠࢖ͬͪΌ͏͔Βͳɻ ҙ༁

  34. $logger = $this->container->get('logger'); $logger->error('...'); 4)06-%/05 function setContainer( ContainerInterface $container )

  35. 4)06-%/05 app()->get('logger'); TheApp::$app->get('logger'); Log::error('...'); ʮ͓લ΋Ͳ͏ͤ4)06-%/05ͳԿ͔Ͱ࡞ΒΕͱΔ΍Ζʯ ʮάϩʔόϧͳΒͬͯ༨ܭ͔͋ΜΘʯ

  36. %# -PH 4FDVSJUZ ίϯςφ ͲΕͰ΋޷͖ͳͷ࢖͍์୊ͩͥ ΍ͬͨશ෦΋Βͬͨ ʮͩΊͩ͜ΓΌʯͰ༗໊ͳ αʔϏεϩέʔλʔ͞Μͱ͸

  37. ·͞ʹάϩʔόϧؔ਺ ϑϦʔμϜϫʔϧυ ͦ͜Ͱ

  38. ґଘੑ஫ೖ %FQFOEFODZ *OKFDUJPO %*

  39. class Foo { protected $logger; public function setLogger(LoggerInterface $logger) {

    $this->logger = $logger; } public function bar() { $this->logger->error('...'); } } ʮऔΓʹߦ͔ͳ͍ʯ ʮ༩͑ͯ΋Β͏ʯ
  40. $container->set('foo', function () use ($container) { $object = new Foo();

    $object->setLogger($container->get('logger')); return $object; }); class Foo { public function setLogger(LoggerInterface $logger) public function bar() } ੜ੒ ࢖༻ ෼཭ ˞͜Ε͸ઃఆϑΝΠϧ಺ͳͷͰػೳ࣮૷Ͱ͸͋Γ·ͤΜ
  41. $response = $container->get('applicationRoot') ->handle($request); JOEFYQIQ ґଘੑ஫ೖͷνΣʔϯͰػೳΛߏ੒͢Δͱ ڀۃɺαʔϏεऔಘͱ࢖༻͕ ಉ࣌ʹى͖Δͷ͕ՕॴʹͳΔ

  42. %# -PH 4FDVSJUZ ίϯςφ %*ίϯςφͷੈք -PHͱ4FDVSJUZΛࠩ͠ࠐΊ͹ ಈ͘෦඼ͷઃܭਤͩΑ DMBTT DMBTT JOEFYQIQ

  43. ༩͑ͨ΋ͷ͔͠ઈରʹ࢖Θͳ͍ ୭͕͍ͭԿΛ࢖͏͔Θ͔Βͳ͍ ҆৺ͳͷͰ

  44. class FooTest extends TestCase { protected $foo; protected function setUp()

    { $this->foo = new Foo(); $this->foo->setLogger(new NullLogger()); } } ʮ७ਮʯͳ୯ମςετՄೳ ґଘʹμϛʔΛೖΕ͓͚ͯ͹0, ςετର৅ΦϒδΣΫτ͸ ίϯςφʹԿ΋औΓʹߦ͔ͳ͍ ʮ୯ମʯςετʹʮίϯςφηοτΞοϓʯ͕ඞཁʹͳΔ͋ͷҧ࿨ײͷղফ
  45. ७ਮͳ୯ମςετ ˠςετϑΝʔετ͕؆୯ʹ ίϯςφ ? 1)16OJU

  46. ૄ݁߹ສࡀ ςετۦಈ։ൃສࡀ Ͱ͔͢

  47. None
  48. ͋ͳ͕ͨཉ͍͠ͷ͸ɺ ػೳϑϨʔϜϫʔΫͷઃܭϑϨʔϜϫʔΫͰ͔͢  ͦΕͱ΋ɺ͍·͙͢࢖͑ΔઃܭࡁΈͷࠎ૊ΈͰ͔͢

  49. TDB⒎PME DPOpHVSBUJPO ػೳϑϨʔϜϫʔΫͷ ઃܭϑϨʔϜϫʔΫ ͍·͙͢࢖͑Δ ઃܭࡁΈͷࠎ૊Έ

  50. 1)1JTΦϒδΣΫτ ࢦ޲ݴޠ

  51. 1)1JTBMTPކݴޠ

  52. ΞϓϦέʔγϣϯ ςετࡁΈ044 ϢʔβʔϥΠϒϥϦ ކΛΘ͖·͑ͨബ͞Ͱ ΞϓϦέʔγϣϯ͕ॻ͚ΔͳΒ ͜Ε͚ͩςετͰ͖ͯΕ͹ ͜ͷྔͰີ݁߹ͷԿ͕ѱ͍ UFTUFE

  53. ߏ੒PSද૚ ຊ࣭తͳ࢖༻໨తͷ͕͍ͪ ͋ΒΏΔධՁ࣠Ͱɺ͜ͷΑ͏ͳ ରൺ͕੒ΓཱͭͷͰ͸

  54. '"2 ʮͲΕ͕ྑ͍Ͱ͔͢ ʯ

  55. "ຊ࣭ΛݟۃΊ ৗʹରൺ͢΂͠

  56. None
  57. ͻͱͭͷݴޠɺଟ༷ͳࢥ૝ 1)1JT ֶͼͷଟ͍ݴޠ