Created
October 31, 2013 18:21
-
-
Save yukikaoru/7254442 to your computer and use it in GitHub Desktop.
Test of binding definitions are duplicate on Ray.Di
This file contains 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 | |
use Ray\Di\AbstractModule; | |
use Ray\Di\Di\Inject; | |
use Ray\Di\Injector; | |
$loader = require __DIR__ . '/vendor/autoload.php'; | |
\Doctrine\Common\Annotations\AnnotationRegistry::registerLoader([$loader, 'loadClass']); | |
class Base | |
{ | |
public $name; | |
public function __construct($name) | |
{ | |
$this->name = $name; | |
} | |
} | |
class Foo extends Base {} | |
class Bar extends Base {} | |
class Baz extends Base {} | |
class FooModule extends AbstractModule | |
{ | |
protected function configure() | |
{ | |
$this->bind('Foo')->toInstance(new Foo('foo')); | |
} | |
} | |
class HogeModule extends AbstractModule | |
{ | |
protected function configure() | |
{ | |
$this->bind('Foo')->toInstance(new Foo('hoge')); | |
} | |
} | |
class BarModule extends AbstractModule | |
{ | |
protected function configure() | |
{ | |
$this->bind('Bar')->toInstance(new Bar('bar')); | |
} | |
} | |
class FugaModule extends AbstractModule | |
{ | |
protected function configure() | |
{ | |
$this->bind('Bar')->toInstance(new Bar('fuga')); | |
} | |
} | |
class BazModule extends AbstractModule | |
{ | |
protected function configure() | |
{ | |
$this->bind('Baz')->toInstance(new Baz('baz')); | |
} | |
} | |
class FooBarService | |
{ | |
/** | |
* @var Foo | |
*/ | |
private $foo; | |
/** | |
* @var Bar | |
*/ | |
private $bar; | |
/** | |
* @Inject | |
* @param Foo $foo | |
* @param Bar $bar | |
*/ | |
public function __construct(Foo $foo, Bar $bar) | |
{ | |
$this->foo = $foo; | |
$this->bar = $bar; | |
} | |
public function dump() | |
{ | |
var_dump($this->foo->name, $this->bar->name); | |
} | |
} | |
class HogeFugaBazService | |
{ | |
/** | |
* @var Foo | |
*/ | |
private $foo; | |
/** | |
* @var Bar | |
*/ | |
private $bar; | |
/** | |
* @var Baz | |
*/ | |
private $baz; | |
/** | |
* @Inject | |
* @param Foo $foo | |
* @param Bar $bar | |
* @param Baz $baz | |
*/ | |
public function __construct(Foo $foo, Bar $bar, Baz $baz) | |
{ | |
$this->foo = $foo; | |
$this->bar = $bar; | |
$this->baz = $baz; | |
} | |
public function dump() | |
{ | |
var_dump($this->foo->name, $this->bar->name, $this->baz->name); | |
} | |
} | |
class FooBarServiceModule extends AbstractModule | |
{ | |
protected function configure() | |
{ | |
$this->install(new FooModule()); | |
$this->install(new BarModule($this)); | |
} | |
} | |
class HogeFugaBazServiceModule extends AbstractModule | |
{ | |
protected function configure() | |
{ | |
$this->install(new HogeModule()); | |
$this->install(new FugaModule()); | |
$this->install(new BazModule($this)); | |
} | |
} | |
$injector = Injector::create( | |
[ | |
new FooBarServiceModule(), | |
new HogeFugaBazServiceModule(), | |
] | |
); | |
$injector->getInstance('HogeFugaBazService')->dump(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment