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

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

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

8c1ecc738eee0254a41c2d4b33cda7b6?s=128

Hisateru Tanaka

July 15, 2017
Tweet

Transcript

  1. 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
  2. 8.
  3. 16.

    143

  4. 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
  5. 25.

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

    WARNING | ERROR); $logger->addTarget($logTarget); $logger->error('...'); ͱ͋ΔϩΪϯάϥΠϒϥϦΛ࢖͍͍ͨͱࢥͬͨ৔߹
  6. 26.

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

    WARNING | ERROR); $logger->addTarget($logTarget); $logger->error('...'); ͱ͋ΔϩΪϯάϥΠϒϥϦΛ࢖͍͍ͨͱࢥͬͨ৔߹ ຖճίϐϖ΍Ίͯ
  7. 31.
  8. 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.
  9. 39.

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

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

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

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