Created
February 26, 2014 23:47
-
-
Save hirak/9241254 to your computer and use it in GitHub Desktop.
Dependency Injectionを特定のDIコンテナに頼らず実現する ref: http://qiita.com/Hiraku/items/48fbdfca4b63c74494e9
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$container = new \Pimple(); | |
// オブジェクトコンストラクションのコンフィギュレーション | |
$container['infrastructure.mailer'] = function($container) { | |
$mailer = new \SendmailMailer(); | |
return $mailer; | |
}; | |
$container['domain.transfer.newsletter'] = function($container) { | |
$newsletterTransfer = new \NewsletterTransfer($container['infrastructure.mailer']); | |
return $newsletterTransfer; | |
}; | |
return $container; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$container = new FactoryDefault; | |
// オブジェクトの利用 | |
$newsletterTransfer = $container->createDomainTransferNewsletter(); | |
$newsletterTransfer->send('ニュースレター本文'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class FactoryDefault | |
{ | |
function createInfrastructureMailer() | |
{ | |
return new SendmailMailer; | |
} | |
function createDomainTransferNewsletter() | |
{ | |
return new NewsletterTransfer($this->createInfrastructureMailer()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class FactoryTest extends FactoryDefault | |
{ | |
function createInfrastructureMailer() | |
{ | |
return new SendmailMailerMock; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createInfrastructureMailer() | |
{ | |
static $mailer; | |
return $mailer ?: ($mailer = new SendmailMailer); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//... | |
function createInfrastructureMailer() | |
{ | |
$mailer = parent::createInfrastructureMailer(); | |
$mailer->setOption('hogehoge'); | |
return $mailer; | |
} | |
//... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$container = new FactoryTest; //こっちに差し替える | |
//... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment