DIã¨ã¯ï¼DIã³ã³ããã¨ã¯ï¼è©¦ãã¦ã¿ã(å¾ç·¨)[PHP][Pimple][DI]
DIとは?DIコンテナとは?試してみた(前編)[PHP][DI] - あざらし備忘録。 ã®å¾ç·¨ã§ãã
ä»åã¯DIã³ã³ããã«ã¤ãã¦ã
DIã³ã³ããã¨ã¯
DIãããã«ããã£ã¦ã使ç¨ãã度ã«æ¯åä¾åæ§ã®ãããã®ãçæãã¦æ³¨å ¥ãã¦ã...ã¨ããã®ãé¢åãããã®ã§ãã®ä¾åæ§ãå®ç¾©ãã¦ã³ã³ããã¨ããå½¢ã§ä¿ç®¡ãã¦ãããã¨ã§ãã®ã³ã³ãããåãåºãã ãã§ä¾åé¢ä¿ã®æ´ã£ããã®ãæã«å ¥ãããã¨ãã§ãã代ç©ã§ãã
ååã®ãããã
ååãDIã®ä¾ã¨ãã¦æ¸ããã³ã¼ãã¯ä»¥ä¸ã®æ§ã§ããã
<?php interface SongInterface { public function getTitle(); // ...ãªã©æ²ã«é¢ãã諸ã } class Song implements SongInterface { public function __construct($title=null) { $this->title = $title; } public function getTitle() { return $this->title; } // ... } interface MusicPlayerInterface { public function play(); } class MusicPlayer implements MusicPlayerInterface { /** * @var SongInterface */ private $song; public function __construct(SongInterface $song) { $this->song = $song; } public function play() { echo '"'.$this->song->getTitle().'"ãåçããã'; } } interface TwitterClientInterface { public function post(); } class TwitterClient implements TwitterClientInterface { public function post() { // æ稿å¦ç echo 'é³ã²ã¼ããã¬ã¤ãããã¨ã¤ã¶ããã'; } } class Otoge { /** * @var SongInterface */ private $song; /** * @var MusicPlayerInterface */ private $music_player; /** * @var TwitterClientInterface */ private $twitter_client; public function __construct(SongInterface $song, MusicPlayerInterface $music_player, TwitterClientInterface $twitter_client) { $this->song = $song; $this->music_player = $music_player; $this->twitter_client = $twitter_client; } public function play() { echo '"'.$this->song->getTitle().'"ã§é³ã²ã¼ãéå§ããã'; $this->music_player->play(); } public function tweet() { $this->twitter_client->post(); } }
å®è¡ä¾ã¯ããããªæãã§ããã
<?php $song = new Song('test'); $music_player = new MusicPlayer($song); $twitter_client = new TwitterClient(); $otoge = new Otoge($song, $music_player, $twitter_client); $otoge->play(); $otoge->tweet();
songãmusic_playerãtwitter_clientã...æ¯åã¤ã³ã¹ã¿ã³ã¹ãçæãã¦DIãã¦ã...é¢åããããã§ããã
DIã³ã³ããã使ã£ã¦ã¿ã
ä»åã¯ãPHPã®ä¸ã§æãæåãªDIã³ã³ããã®ä¸ã¤ã§ããPimpleã使ã£ã¦ãµã³ãã«ãä½ã£ã¦ã¿ã¾ãã
Pimple - A simple PHP Dependency Injection Container
æ©éã§ãããã¡ãã
ã¯ã©ã¹/ã¤ã³ã¿ã¼ãã§ã¼ã¹å®ç¾©ã«ã¤ãã¦ã¯å¤æ´ã¯ããã¾ãããå®è¡ä¾ã¯æ¬¡ã®ããã«ãªãã¾ãã
<?php require_once './config_container.php'; $container['song.title'] = 'hoge'; $otoge = $container['otoge']; $otoge->play(); $otoge->tweet();
å ã»ã©ã®DIã®ä¾ã§ã¯ãã£ãsongãmusic_playerãtwitter_clientã¨ãã£ãotogeã«ä¾åé¢ä¿ã®ãã£ãé¢åãããã¤ã³ã¹ã¿ã³ã¹ã®çæãã±ã£ã¨è¦ãªããªã£ã¦ãã¾ãã ã¾ããcontainerã¨ããè¦æ £ããªããã®è¦ãã¾ããã
ãã®containerã¨ãããã¤ããããªã«ããæãã«ãã¦ããã¦ããããã§ãã
ã¾ãããã¡ãã£ã¨è¨ãã¨ååã¨æèããDIã³ã³ããã§ããï½
åçã¯ããããªãã§ããå¼ã³åºãå´ã¨ãã¦ã¯æ²æ å ±(ä»åã¯titleã ãã ãã©)ãã»ããããã ãã§ãã®æ²æ å ±ãæã£ãç¶æ ã®$otogeãä½ããã¨ãã§ããã¾ãã表ã§ã¯ç¹ã«ä½ããã¦ããªãã®ã«tweetæ©è½ãplayæ©è½ãæ±ãäºãã§ããããã«ãªããé常ã«ä½¿ãããããªãã¾ããã
åçãè¦ã¦ã¿ã
requireãã¦ããconfig_container.phpãè¦ã¦ã¿ã¾ãããã
<?php use Pimple\Container; $container = new Container(); $container['song.title'] = ''; $container['song'] = $container->factory(function ($c) { return new Song($c['song.title']); }); $container['music_player'] = $container->factory(function ($c) { return new MusicPlayer($c['song']); }); $container['twitter_client'] = function ($c) { return new TwitterClient(); }; $container['otoge'] = $container->factory(function ($c) { return new Otoge($c['song'], $c['music_player'], $c['twitter_client']); });
ãããDIã³ã³ããè¨å®ã®ä¸èº«ã§ãã
ããã§ä¾åé¢ä¿ãå®ç¾©ãã¦ãã¾ãã
ä»çµã¿ã¨ãã¦ã¯ã$container['otoge']ãå®è¡æã«å¼ã°ãããã¨ã§ç¡åé¢æ°ãå¼ã°ãã¦ãã®éã«ã¤ã³ã¹ã¿ã³ã¹ãçæãããã¨ãã寸æ³ã§ãã
Pimpleã«ã¤ãã¦ã¯ä¸DIã³ã³ããä¾ã¨ãã¦ç¨ããã ããªã®ã§è©³ãã説æã¯å²æãã¾ããã
factory()ãç¨ãããã¨ã§ç¡åé¢æ°ãå¼ã°ãã度ã«æ°ããªã¤ã³ã¹ã¿ã³ã¹ãçæãã¾ãã
ç¨ããªãå ´åã¯ç¡åé¢æ°ãä¸åº¦å¼ã°ããæã«ãã£ãã·ã¥ãã以å¾åä¸ã¤ã³ã¹ã¿ã³ã¹ãè¿ãããããã·ã³ã°ã«ãã³ãªæ¯ãèãããã¾ãã
ã¡ã¼ã«é ä¿¡ããã¤ãã¿ã¼éä¿¡ãªã©ããµã¼ãã¹ã¬ã¤ã¤ã¼ãªãã®ã«ã¤ãã¦ã¯factory()ãªãã§å¼ã¶ãã¨ã§ç¡é§ã«ã¤ã³ã¹ã¿ã³ã¹çæãããã¨ãªãæ±ããDIã³ã³ããã¨ãã¦ã¯ãã®ãããªæ¯ãèããæå¾ ãã¦ç¨ãããã¨ãå¤ããã¨æãã¾ã(ä¾ã§ã¯factory()ãå¤ãã«ãªã£ã¦ãã¾ã£ã¦ã¢ã¬ã§ããããã)
ã¾ãã$container['song.title']ã®ããã«æ®éã«æååçãæ±ããã¨ã¯ã§ãã¾ãã
ãã®ããã«ãDIã³ã³ãããä¸æã使ããã¨ã§å¼ã³åºãå´ã¨ãã¦ã¯é常ã«ã¹ãããªã¨ããå¼ã³åºãæ¹ããããã¨ãã§ããæå³ã®ããããããã³ã¼ããæ¸ããããã«ãªãã¾ãã
å¼ã³åºãå´ããã£ããããã®ã¯é常ã«è¯ãï¼
DIã³ã³ããã使ã£ã¦ããããã¨ã§å¼ã³åºãå´ãé常ã«è¦éããè¯ããªãä½ããããã®ããæ確ã«ãªãã¨ããæ©æµãå¾ãããã®ã¯é常ã«å¤§ãããªãã¨æãã¾ããã
ä¸æã«ä»ãåã£ã¦ãããã代ç©ã§ããï¼
ä»åç¨ãããµã³ãã«ã³ã¼ãã¯ä»¥ä¸ã«ç½®ãã¦ããã¾ãã®ã§ãããããã°ã