public function configureInstance(DependencyInjector $di, $instanceData) { $im = $di->getInstanceManager(); foreach ($instanceData as $target => $data) { switch (strtolower($target)) { case 'aliases': case 'alias': foreach ($data as $aliasName => $className) { $im->addAlias($aliasName, $className); } break; case 'properties': case 'property': foreach ($data as $classOrAlias => $properties) { foreach ($properties as $propName => $propValue) { $im->setProperty($classOrAlias, $propName, $propValue); } } break; case 'preferences': case 'preferredinstances': case 'preferredinstance': foreach ($data as $classOrAlias => $preferredValueOrValues) { if (is_array($preferredValueOrValues)) { foreach ($preferredValueOrValues as $preferredValue) { $im->addPreferredInstance($classOrAlias, $preferredValue); } } else { $im->addPreferredInstance($classOrAlias, $preferredValueOrValues); } } } } }
public function testConfigurationCanConfigureBuilderDefinitionFromIni() { $ini = new \Zend\Config\Ini(__DIR__ . '/_files/sample.ini', 'section-b'); $config = new Configuration($ini->di); $di = new DependencyInjector($config); $definition = $di->getDefinition(); $this->assertTrue($definition->hasClass('My\DbAdapter')); $this->assertEquals('__construct', $definition->getInstantiator('My\DbAdapter')); $this->assertEquals( array('username' => null, 'password' => null), $definition->getInjectionMethodParameters('My\DbAdapter', '__construct') ); $this->assertTrue($definition->hasClass('My\Mapper')); $this->assertEquals('__construct', $definition->getInstantiator('My\Mapper')); $this->assertEquals( array('dbAdapter' => 'My\DbAdapter'), $definition->getInjectionMethodParameters('My\Mapper', '__construct') ); $this->assertTrue($definition->hasClass('My\Repository')); $this->assertEquals('__construct', $definition->getInstantiator('My\Repository')); $this->assertEquals( array('mapper' => 'My\Mapper'), $definition->getInjectionMethodParameters('My\Repository', '__construct') ); }
/** * Test for Circular Dependencies (case 2) * * C->D, D->E, E->C * @group CircurlarDependencyCheck */ public function testNewInstanceThrowsExceptionWhenEnteringInMiddleOfCircularDependency() { $di = new DependencyInjector(); $this->setExpectedException( 'Zend\Di\Exception\CircularDependencyException', 'Circular dependency detected: ZendTest\Di\TestAsset\CircularClasses\C depends on ZendTest\Di\TestAsset\CircularClasses\D and viceversa' ); $di->newInstance('ZendTest\Di\TestAsset\CircularClasses\D'); }
/** * @param DependencyInjector $di * @return void */ public function __construct(DependencyInjector $di) { $this->di = $di; $this->definition = $di->getDefinition(); $this->instanceManager = $di->getInstanceManager(); }
public function testNewInstanceWillRunArbitraryMethodsAccordingToConfiguration() { $di = new DependencyInjector(); $im = $di->getInstanceManager(); $im->setMethods('ZendTest\Di\TestAsset\ConfigParameter\A', array( 'setSomeInt' => array('value' => 5), 'injectM' => array('m' => 10) )); $b = $di->newInstance('ZendTest\Di\TestAsset\ConfigParameter\B'); $this->assertEquals(5, $b->a->someInt); $this->assertEquals(10, $b->a->m); }
public function configureInstance(DependencyInjector $di, $instanceData) { $im = $di->getInstanceManager(); foreach ($instanceData as $target => $data) { switch (strtolower($target)) { case 'aliases': case 'alias': foreach ($data as $n => $v) { $im->addAlias($n, $v); } break; case 'preferences': case 'preference': foreach ($data as $n => $v) { if (is_array($v)) { foreach ($v as $v2) { $im->addTypePreference($n, $v2); } } else { $im->addTypePreference($n, $v); } } break; default: foreach ($data as $n => $v) { switch ($n) { case 'parameters': case 'parameter': $im->setParameters($target, $v); break; case 'methods': case 'method': $im->setMethods($target, $v); break; } } } } }