diff --git a/src/Di.php b/src/Di.php index 77c5db7e..d2d89642 100644 --- a/src/Di.php +++ b/src/Di.php @@ -8,7 +8,7 @@ class Di implements DependencyInjection * @var DefinitionList */ protected $definitions = null; - + /** * @var InstanceManager */ @@ -18,18 +18,18 @@ class Di implements DependencyInjection * @var string */ protected $instanceContext = array(); - + /** * All the class dependencies [source][dependency] - * - * @var array + * + * @var array */ protected $currentDependencies = array(); - + /** * All the class references [dependency][source] - * - * @var array + * + * @var array */ protected $references = array(); @@ -89,7 +89,7 @@ public function setInstanceManager(InstanceManager $instanceManager) } /** - * + * * @return InstanceManager */ public function instanceManager() @@ -101,10 +101,10 @@ public function instanceManager() /** * Lazy-load a class * - * Attempts to load the class (or service alias) provided. If it has been + * Attempts to load the class (or service alias) provided. If it has been * loaded before, the previous instance will be returned (unless the service * definition indicates shared instances should not be used). - * + * * @param string $name Class name or service alias * @param null|array $params Parameters to pass to the constructor * @return object|null @@ -112,11 +112,12 @@ public function instanceManager() public function get($name, array $params = array()) { array_push($this->instanceContext, array('GET', $name)); - + $im = $this->instanceManager; - + if ($params) { - if (($fastHash = $im->hasSharedInstanceWithParameters($name, $params, true))) { + $fastHash = $im->hasSharedInstanceWithParameters($name, $params, true); + if ($fastHash) { array_pop($this->instanceContext); return $im->getSharedInstanceWithParameters(null, array(), $fastHash); } @@ -157,14 +158,14 @@ public function newInstance($name, array $params = array(), $isShared = true) } array_push($this->instanceContext, array('NEW', $class, $alias)); - + if (!$definitions->hasClass($class)) { $aliasMsg = ($alias) ? '(specified by alias ' . $alias . ') ' : ''; throw new Exception\ClassNotFoundException( 'Class ' . $aliasMsg . $class . ' could not be located in provided definitions.' ); } - + $instantiator = $definitions->getInstantiator($class); $injectionMethods = $definitions->getMethods($class); @@ -190,7 +191,7 @@ public function newInstance($name, array $params = array(), $isShared = true) } else { $msg = 'Invalid instantiator'; } - throw new \RuntimeException($msg); + throw new \RuntimeException($msg); } if ($isShared) { @@ -243,7 +244,8 @@ public function newInstance($name, array $params = array(), $isShared = true) if ($objectsToInject) { foreach ($objectsToInject as $objectToInject) { foreach ($injectionMethods as $injectionMethod => $methodIsRequired) { - if ($methodParams = $definitions->getMethodParameters($class, $injectionMethod)) { + $methodParams = $definitions->getMethodParameters($class, $injectionMethod); + if ($methodParams) { foreach ($methodParams as $methodParam) { if (get_class($objectToInject) == $methodParam[1] || $this->isSubclassOf(get_class($objectToInject), $methodParam[1])) { @@ -270,15 +272,15 @@ public function newInstance($name, array $params = array(), $isShared = true) } } } - - + + array_pop($this->instanceContext); return $instance; } - + /** - * @todo + * @todo * @param unknown_type $object */ /* @@ -348,7 +350,7 @@ protected function createInstanceViaCallback($callback, $params, $alias) if (!is_callable($callback)) { throw new Exception\InvalidCallbackException('An invalid constructor callback was provided'); } - + if (is_array($callback)) { $class = (is_object($callback[0])) ? get_class($callback[0]) : $callback[0]; $method = $callback[1]; @@ -362,7 +364,7 @@ protected function createInstanceViaCallback($callback, $params, $alias) if ($this->definitions->hasMethod($class, $method)) { $callParameters = $this->resolveMethodParameters($class, $method, $params, true, $alias, true); } - return call_user_func_array($callback, $callParameters); + return call_user_func_array($callback, $callParameters); } /** @@ -401,7 +403,7 @@ protected function resolveMethodParameters($class, $method, array $callTimeUserP { // parameters for this method, in proper order, to be returned $resolvedParams = array(); - + // parameter requirements from the definition $injectionMethodParameters = $this->definitions->getMethodParameters($class, $method); @@ -411,21 +413,21 @@ protected function resolveMethodParameters($class, $method, array $callTimeUserP 'required' => array(), 'optional' => array() ); - + // retrieve instance configurations for all contexts $iConfig = array(); $aliases = $this->instanceManager->getAliases(); - + // for the alias in the dependency tree if ($alias && $this->instanceManager->hasConfiguration($alias)) { $iConfig['thisAlias'] = $this->instanceManager->getConfiguration($alias); } - + // for the current class in the dependency tree if ($this->instanceManager->hasConfiguration($class)) { $iConfig['thisClass'] = $this->instanceManager->getConfiguration($class); } - + // for the parent class, provided we are deeper than one node list($requestedClass, $requestedAlias) = ($this->instanceContext[0][0] == 'NEW') ? array($this->instanceContext[0][1], $this->instanceContext[0][2]) @@ -465,7 +467,7 @@ protected function resolveMethodParameters($class, $method, array $callTimeUserP $computedParams['required'][$fqParamPos] = array( $callTimeUserParams[$name], $this->instanceManager->getClassFromAlias($callTimeCurValue) - ); + ); } elseif ($this->definitions->hasClass($callTimeUserParams[$name])) { // was a known class provided? $computedParams['required'][$fqParamPos] = array( @@ -483,12 +485,12 @@ protected function resolveMethodParameters($class, $method, array $callTimeUserP unset($callTimeCurValue); continue; } - + // PRIORITY 2 -specific instance configuration (thisAlias) - this alias // PRIORITY 3 -THEN specific instance configuration (thisClass) - this class // PRIORITY 4 -THEN specific instance configuration (requestedAlias) - requested alias // PRIORITY 5 -THEN specific instance configuration (requestedClass) - requested class - + foreach (array('thisAlias', 'thisClass', 'requestedAlias', 'requestedClass') as $thisIndex) { // check the provided parameters config if (isset($iConfig[$thisIndex]['parameters'][$fqParamPos]) @@ -531,9 +533,9 @@ protected function resolveMethodParameters($class, $method, array $callTimeUserP } } - + // PRIORITY 6 - globally preferred implementations - + // next consult alias level preferred instances if ($alias && $this->instanceManager->hasTypePreferences($alias)) { $pInstances = $this->instanceManager->getTypePreferences($alias); @@ -575,7 +577,7 @@ protected function resolveMethodParameters($class, $method, array $callTimeUserP if ($type && $isRequired && $methodIsRequired) { $computedParams['required'][$fqParamPos] = array($type, $type); } - + } $index = 0; @@ -615,7 +617,7 @@ protected function resolveMethodParameters($class, $method, array $callTimeUserP } else { $resolvedParams[$index] = null; } - + $index++; } diff --git a/src/ServiceLocator/DependencyInjectorProxy.php b/src/ServiceLocator/DependencyInjectorProxy.php index 73bd4fd0..13dbfeb1 100644 --- a/src/ServiceLocator/DependencyInjectorProxy.php +++ b/src/ServiceLocator/DependencyInjectorProxy.php @@ -13,7 +13,7 @@ class DependencyInjectorProxy extends Di protected $di; /** - * @param DependencyInjector $di + * @param DependencyInjector $di * @return void */ public function __construct(Di $di) @@ -29,9 +29,9 @@ public function __construct(Di $di) /** * Override, as we want it to use the functionality defined in the proxy - * - * @param string $name - * @param array $params + * + * @param string $name + * @param array $params * @return GeneratorInstance */ public function get($name, array $params = array()) @@ -39,7 +39,8 @@ public function get($name, array $params = array()) $im = $this->instanceManager(); if ($params) { - if (($fastHash = $im->hasSharedInstanceWithParameters($name, $params, true))) { + $fastHash = $im->hasSharedInstanceWithParameters($name, $params, true); + if ($fastHash) { return $im->getSharedInstanceWithParameters(null, array(), $fastHash); } } else { @@ -54,9 +55,9 @@ public function get($name, array $params = array()) * Override createInstanceViaConstructor method from injector * * Returns code generation artifacts. - * - * @param string $class - * @param null|array $params + * + * @param string $class + * @param null|array $params * @param null|string $alias * @return GeneratorInstance */ @@ -75,9 +76,9 @@ public function createInstanceViaConstructor($class, $params, $alias = null) /** * Override instance creation via callback - * - * @param callback $callback - * @param null|array $params + * + * @param callback $callback + * @param null|array $params * @return GeneratorInstance */ public function createInstanceViaCallback($callback, $params, $alias = null) @@ -103,11 +104,11 @@ public function createInstanceViaCallback($callback, $params, $alias = null) /** * Retrieve metadata for injectible methods - * - * @param string $class - * @param string $method - * @param array $params - * @param string $alias + * + * @param string $class + * @param string $method + * @param array $params + * @param string $alias * @return array */ public function handleInjectionMethodForObject($class, $method, $params, $alias, $isRequired) @@ -121,10 +122,10 @@ public function handleInjectionMethodForObject($class, $method, $params, $alias, /** * Override new instance creation - * - * @param string $name - * @param array $params - * @param bool $isShared + * + * @param string $name + * @param array $params + * @param bool $isShared * @return GeneratorInstance */ public function newInstance($name, array $params = array(), $isShared = true)