æå°ã®DIã³ã³ããã使ã£ã¦ççµåã«ããä¾
前回のエントリã®ç¶ãã
ãã®è¨äºã§ã¯ååã®ã¨ã³ããªã§æ¸ããDIã³ã³ããã使ã£ã¦ã¯ã©ã¹éãççµåã«ããä¾ãæ¸ãã
ã¯ã©ã¹éãççµåã«ããã¨è¨ã£ã¦ããã¯ã©ã¹ã®å
é¨ã§ä»ã®ã¯ã©ã¹ã«ä¾åãã¦ããã®ã§ã¯DIã³ã³ãããã©ã使ããã¨ççµåã«ã¯ãªããªãã
以ä¸ã®ã³ã¼ãããããLoggerã¯ã©ã¹å
é¨ã«LogWriterã¯ã©ã¹ã¸ã®ä¾åãæ±ãã¦ããã
<?php // ããããåç´ãªãã¬ã¼ class Logger { protected $writer; function __construct() { // LogWriterã¯ã©ã¹ããã¼ãã³ã¼ãã£ã³ã°ããã¦ããï¼ $this->writer = new LogWriter; } function debug($msg) { $this->writer->write('[debug]' . date('c') . ' '. $msg . PHP_EOL); } function info($msg) { $this->writer->write('[info]' . date('c') . ' '. $msg . PHP_EOL); } } // å®éã«ãã°ãåãåºãã¯ã©ã¹ class LogWriter { protected $path; function __construct($path) { $this->path = $path; } function write($msg) { // 渡ãããæååããã¡ã¤ã«ã«è¿½è¨ãã file_put_contents($this->path, $msg, FILE_APPEND); } }
ã¦ã¼ã¶ã¼ã¯Loggerã¯ã©ã¹ãç¨ãã¦ãã°ãåãããã ããå
é¨çã«ã¯ãã°ã®åé¢ã®æ´å½¢ãLoggerã¯ã©ã¹ãæ
å½ãããã°ãå®éã«ã©ããã«åãä»äºãLogWriterã¯ã©ã¹ãè¡ã£ã¦ããã
Loggerã¯ã©ã¹å
é¨ã§LogWriterã¯ã©ã¹ã«ä¾åãã¦ãã以ä¸ããã®ã¾ã¾ã ã¨ä½ããã£ã¦ãççµåã«ã¯ã§ããªãã®ã§ä»¥ä¸ã®ããã«æ¸ãæããã
<?php interface ILogwWriter { function write($msg); } // ããããåç´ãªãã¬ã¼ class Logger { protected $writer; function __construct(ILogWriter $writer) { // å¤é¨ããä¾åãããªãã¸ã§ã¯ããåãåãå½¢ã«ãªã£ã¦ãã $this->writer = $writer; } function debug($msg) { $this->writer->write('[debug]' . date('c') . ' '. $msg . PHP_EOL); } function info($msg) { $this->writer->write('[info]' . date('c') . ' '. $msg . PHP_EOL); } } // ãã¡ã¤ã«ã«ãã°ãåãåºãã¯ã©ã¹ class FileLogWriter implements ILogWriter { protected $path; function __construct($path) { $this->path = $path; } function write($msg) { // 渡ãããæååããã¡ã¤ã«ã«è¿½è¨ãã file_put_contents($this->path, $msg, FILE_APPEND); } }
ããã§Loggerã¯ã©ã¹ã¯FileLogWriterã¯ã©ã¹ã¨ããå
·è±¡ã¯ã©ã¹ã«ä¾åããªããªããILogWriterã¤ã³ã¿ã¼ãã§ã¤ã¹ã«ä¾åããããã«ãªã£ãã®ããããã
ãã°ãã©ãåãåºããã¨ããå®è£
ãããã§å
¥ãæ¿ãããã¨ãã§ããã
ãããã«ILogWriterã¤ã³ã¿ã¼ãã§ã¤ã¹ã®å¥ã®å®è£
ãä½ã£ã¦ã¿ãã
<?php class HTMLLogWriter implements ILogWriter { function write($msg) { echo '<pre>' . htmlspecialchars($msg, ENT_QUOTES, 'utf-8') . '</pre>'; } }
ãã§ä¸ã®ããã«ä½¿ããããã«ãªãã
<?php // ãªãã¸ã§ã¯ãã®è¨å® $logger2 = new Logger(new HTMLLogWriter); // ãªãã¸ã§ã¯ãã®å©ç¨ $logger2->info('hoge-'); $logger2->debug('fuga-');
ãã°ãåãåºããã¸ãã¯ã交æããã¦ããã®ããããã¨æãã
ããã¯å
¸åçãªStrategyãã¿ã¼ã³ã§ããã
ãã§ãåã®ã¨ã³ããªã§æ¸ããDIã³ã³ããã使ã£ã¦ãªãã¸ã§ã¯ãã®è¨å®ã管çããã
<?php class MyComponenntFactory extends ComponentFactory { function buildLogWriter() { return new FileLogWriter('./log.txt'); } function buildLogger() { return new Logger($this->container->get('logWriter')); } } $container = new DIContainer(new MyComponentFactory); // ã³ã³ãããããªãã¸ã§ã¯ããåãåºã $logger = $container->get('logger'); // ãªãã¸ã§ã¯ããå©ç¨ãã $logger->info('hoge-');
ããã§ããªãã¸ã§ã¯ãéã®ä¾åãDIã³ã³ããã«ãã£ã¦ç®¡çãããççµåã¨ãªã£ãã
ã¨ã¯ãã£ã¦ããããã ãã ã¨ããã¾ãå®ç¨çã§ã¯ãªãããã¾ã ä½ãä½ã ãããããªãã¨æãã®ã§ããå°ãä¾ãçºå±ãããã
ä¾ãã°ãLoggerã¤ã³ã¹ã¿ã³ã¹ãå
é¨ã§ä½¿ãFooAppã¯ã©ã¹ãããã¨ããã
<?php class FooApp { protected $logger; function setLogger(Logger $obj) { $this->logger = $obj; } function execute() { /* * ãªãããã®å¦ç ã * $this->loggerã使ãããã */ } }
Loggerã¤ã³ã¹ã¿ã³ã¹ãå¤é¨ããåãåãå½¢ã«ãªã£ã¦ããã®ããããã¨æãã
ãã§ãããã³ã³ããã§ç®¡çããã¨ãããªãã
<?php class MyComponentFactory extends ComponentFactory { function buildLogWriter() { return new FileLogWriter('./log.txt'); } function buildLogger() { return new Logger($this->container->get('logWriter')); } function buildFooApp() { $obj = new FooApp; $obj->setLogger($this->container->get('logger')); return $obj; } } $container = new DIContainer(new MyComponentFactory); // ã³ã³ãããããªãã¸ã§ã¯ããåãåºã $fooapp = $container->get('fooApp'); $fooapp->execute();
ã³ã³ãããå
¨ã¦ã®ãªãã¸ã§ã¯ãéã®ä¾åã解決ãã¦ããã¦ããã®ããããã
ããã¦ç®¡çããããªãã¸ã§ã¯ãã¯ã³ã³ããã«å
¨ãä¾åãã¦ããªãã
ããããä¾ãã°ãã°ãåãåºããã¸ãã¯ã交æããããªã£ããããã®ããã«ããã°è¯ãã
<?php class FugaComponentFactory extends MyComponentFactory { function buildLogWriter() { return new HTMLLogWriter; } } $container = new DIContainer(new FugaComponentFactory); $fooapp = $container->get('fooApp'); $fooapp->execute();
ãªãã¸ã§ã¯ãã®è¨å®ã¨å©ç¨ããã¡ãã¨åé¢ããã¦ããã®ã§ãDIã³ã³ããå´ããããã ãã§ãã¸ãã¯ã®äº¤æãã§ãããã¨ã