diff --git a/src/ServiceLocator/DependencyInjectorProxy.php b/src/ServiceLocator/DependencyInjectorProxy.php index 50abb60c..9748ee99 100644 --- a/src/ServiceLocator/DependencyInjectorProxy.php +++ b/src/ServiceLocator/DependencyInjectorProxy.php @@ -36,18 +36,24 @@ public function __construct(DependencyInjector $di) */ public function get($name, array $params = array()) { + array_push($this->instanceContext, array('GET', $name)); + $im = $this->getInstanceManager(); if ($params) { if (($fastHash = $im->hasSharedInstanceWithParameters($name, $params, true))) { + array_pop($this->instanceContext); return $im->getSharedInstanceWithParameters(null, array(), $fastHash); } } else { if ($im->hasSharedInstance($name, $params)) { + array_pop($this->instanceContext); return $im->getSharedInstance($name, $params); } } - return $this->newInstance($name, $params); + $instance = $this->newInstance($name, $params); + array_pop($this->instanceContext); + return $instance; } /** @@ -127,6 +133,7 @@ public function handleInjectionMethodForObject($class, $method, $params, $alias) */ public function newInstance($name, array $params = array(), $isShared = true) { + // localize dependencies (this also will serve as poka-yoke) $definition = $this->getDefinition(); $instanceManager = $this->getInstanceManager(); @@ -138,9 +145,13 @@ public function newInstance($name, array $params = array(), $isShared = true) $alias = null; } + array_push($this->instanceContext, array('NEW', $class, $alias)); + if (!$definition->hasClass($class)) { $aliasMsg = ($alias) ? '(specified by alias ' . $alias . ') ' : ''; - throw new Exception\ClassNotFoundException('Class ' . $aliasMsg . $class . ' could not be located in provided definition.'); + throw new Exception\ClassNotFoundException( + 'Class ' . $aliasMsg . $class . ' could not be located in provided definition.' + ); } $instantiator = $definition->getInstantiator($class); @@ -159,21 +170,33 @@ public function newInstance($name, array $params = array(), $isShared = true) } if ($injectionMethods) { - $methodMetadata = array(); foreach ($injectionMethods as $injectionMethod) { - $methodMetadata[] = $this->handleInjectionMethodForObject($class, $injectionMethod, $params, $alias); + $this->handleInjectionMethodForObject($object, $injectionMethod, $params, $alias); } - $object->setMethods($methodMetadata); } + // Methods for which we have configuration + $iConfig = ($instanceManager->hasAlias($alias) && $instanceManager->hasConfiguration($alias)) + ? $instanceManager->getConfiguration($alias) + : $instanceManager->getConfiguration(get_class($object)); + + if ($iConfig['methods']) { + foreach ($iConfig['methods'] as $iConfigMethod => $iConfigMethodParams) { + // skip methods processed by handleInjectionMethodForObject + if (in_array($iConfigMethod, $injectionMethods) && $iConfigMethod !== '__construct') continue; + call_user_func_array(array($object, $iConfigMethod), array_values($iConfigMethodParams)); + } + } + if ($isShared) { if ($params) { - $instanceManager->addSharedInstanceWithParameters($object, $name, $params); + $this->getInstanceManager()->addSharedInstanceWithParameters($object, $name, $params); } else { - $instanceManager->addSharedInstance($object, $name); + $this->getInstanceManager()->addSharedInstance($object, $name); } } - + + array_pop($this->instanceContext); return $object; } } diff --git a/test/ServiceLocator/GeneratorTest.php b/test/ServiceLocator/GeneratorTest.php index 3ca96839..b058183c 100644 --- a/test/ServiceLocator/GeneratorTest.php +++ b/test/ServiceLocator/GeneratorTest.php @@ -8,7 +8,7 @@ Zend\Di\Definition\Builder, PHPUnit_Framework_TestCase as TestCase; -class ContainerBuilderTest extends TestCase +class GeneratorTest extends TestCase { public $tmpFile = false; @@ -70,14 +70,12 @@ public function createDefinitions() 'inspect' => array('inspect'), 'struct' => array('struct'), ), - 'properties' => array( - 'ZendTest\Di\TestAsset\InspectedClass' => array( - 'baz' => 'BAZ', - ), - 'ZendTest\Di\TestAsset\Struct' => array( - 'param1' => 'foo', - ), - ), + 'ZendTest\Di\TestAsset\InspectedClass' => array( 'parameters' => array( + 'baz' => 'BAZ', + )), + 'ZendTest\Di\TestAsset\Struct' => array( 'parameters' => array( + 'param1' => 'foo', + )), ), ); $configuration = new Configuration($data); @@ -93,6 +91,9 @@ public function buildContainerClass($name = 'Application') $this->assertFileExists($this->tmpFile); } + /** + * @group one + */ public function testCreatesContainerClassFromConfiguredDependencyInjector() { $this->buildContainerClass();