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

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

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

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

Hisateru Tanaka

July 15, 2017
Tweet

More Decks by Hisateru Tanaka

Other Decks in Technology

Transcript

  1. 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
  2. 143

  3. <?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
  4. $logger = new SuperFlexibleLogger(); $logTarget = new FileLogTarget('/var/log/myapp.log'); $logTarget->setLevel(INFO |

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

    WARNING | ERROR); $logger->addTarget($logTarget); $logger->error('...'); ͱ͋ΔϩΪϯάϥΠϒϥϦΛ࢖͍͍ͨͱࢥͬͨ৔߹ ຖճίϐϖ΍Ίͯ
  6. 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.
  7. class Foo { protected $logger; public function setLogger(LoggerInterface $logger) {

    $this->logger = $logger; } public function bar() { $this->logger->error('...'); } } ʮऔΓʹߦ͔ͳ͍ʯ ʮ༩͑ͯ΋Β͏ʯ
  8. $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() } ੜ੒ ࢖༻ ෼཭ ˞͜Ε͸ઃఆϑΝΠϧ಺ͳͷͰػೳ࣮૷Ͱ͸͋Γ·ͤΜ
  9. class FooTest extends TestCase { protected $foo; protected function setUp()

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