diff --git a/src/Definition/RuntimeDefinition.php b/src/Definition/RuntimeDefinition.php index f9f9e90f..fc47f3eb 100644 --- a/src/Definition/RuntimeDefinition.php +++ b/src/Definition/RuntimeDefinition.php @@ -6,22 +6,44 @@ class RuntimeDefinition implements Definition { + /**@#+ + * @const string Lookup Type + */ const LOOKUP_TYPE_IMPLICIT = 'implicit'; const LOOKUP_TYPE_EXPLICIT = 'explicit'; + /** + * @var string Initial lookup type + */ protected $lookupType = self::LOOKUP_TYPE_IMPLICIT; - + + /** + * @var IntrospectionRuleset + */ protected $introspectionRuleset = null; - + + /** + * @var array + */ protected $classes = array(); - + + /** + * @var array + */ protected $injectionMethodCache = array(); - + + /** + * @param string $lookupType + */ public function __construct($lookupType = self::LOOKUP_TYPE_IMPLICIT) { $this->lookupType = $lookupType; } - + + /** + * @param IntrospectionRuleset $introspectionRuleset + * @return void + */ public function setIntrospectionRuleset(IntrospectionRuleset $introspectionRuleset) { $this->introspectionRuleset = $introspectionRuleset; @@ -60,7 +82,7 @@ public function setLookupType($lookupType) /** * Track classes when using EXPLICIT lookups - * @param unknown_type $class + * @param string $class */ public function addClass($class) { @@ -69,7 +91,8 @@ public function addClass($class) /** * Return whether the class exists - * + * + * @param string $class * @return bool */ public function hasClass($class) @@ -79,7 +102,8 @@ public function hasClass($class) /** * Return the supertypes for this class - * + * + * @param string $class * @return array of types */ public function getClassSupertypes($class) @@ -89,6 +113,8 @@ public function getClassSupertypes($class) /** * Get the instiatiator + * + * @param string $class * @return string|callable */ public function getInstantiator($class) @@ -102,7 +128,8 @@ public function getInstantiator($class) /** * Return if there are injection methods - * + * + * @param string $class * @return bool */ public function hasInjectionMethods($class) @@ -113,7 +140,9 @@ public function hasInjectionMethods($class) /** * Return injection methods - * + * + * @param string $class + * @param string $method * @return bool */ public function hasInjectionMethod($class, $method) @@ -124,7 +153,8 @@ public function hasInjectionMethod($class, $method) /** * Return an array of the injection methods - * + * + * @param string $class * @return array */ public function getInjectionMethods($class) @@ -177,7 +207,7 @@ public function getInjectionMethods($class) continue; } - // explicity NOT in excluded classes + // explicitly NOT in excluded classes if ($sRules['excludedClasses'] && (in_array($className, $sRules['excludedClasses']) || in_array($declaringClassName, $sRules['excludedClasses']))) { @@ -228,13 +258,15 @@ public function getInjectionMethods($class) /** * Return the parameters for a method - * + * * 3 item array: * #1 - Class name, string if it exists, else null * #2 - Optional?, boolean * #3 - Instantiable, boolean if class exists, otherwise null - * - * @return array + * + * @param string $class + * @param string $method + * @return array */ public function getInjectionMethodParameters($class, $method) { @@ -264,7 +296,7 @@ public function getInjectionMethodParameters($class, $method) $params[$paramName] = array(); - // set the clas name, if it exists + // set the class name, if it exists $params[$paramName][] = ($pc !== null) ? $pc->getName() : null; // optional? diff --git a/src/DependencyInjector.php b/src/DependencyInjector.php index 7b67a77a..704d8e7c 100755 --- a/src/DependencyInjector.php +++ b/src/DependencyInjector.php @@ -32,9 +32,10 @@ class DependencyInjector implements DependencyInjection * @var array */ protected $references = array(); - + /** - * @param Zend\DI\Configuration $config + * @param null|Configuration $config + * @return \Zend\Di\DependencyInjector */ public function __construct(Configuration $config = null) { @@ -42,22 +43,33 @@ public function __construct(Configuration $config = null) $this->configure($config); } } - + + /** + * Provide a configuration object to configure this instance + * + * @param Configuration $config + * @return void + */ public function configure(Configuration $config) { $config->configure($this); } - + + /** + * @param Definition $definition + * @return DependencyInjector + */ public function setDefinition(Definition $definition) { $this->definition = $definition; return $this; } - + /** * Definition Factory - * + * * @param string $class + * @return Definition */ public function createDefinition($class) { @@ -70,12 +82,22 @@ public function createDefinition($class) } return $definition; } - + + /** + * If this DependencyInjector has a definition currently attached + * + * @return bool + */ public function hasDefinition() { return ($this->definition !== null); } - + + /** + * Return a definition, if one doesn't already exist, a Definition\RuntimeDefinition is created + * + * @return Definition + */ public function getDefinition() { if ($this->definition == null) { @@ -91,39 +113,27 @@ public function hasInstanceManager() { return ($this->instanceManager !== null); } - - public function setInstanceManager(InstanceCollection $instanceManager) - { - $this->instanceManager = $instanceManager; - return $this; - } - + /** - * InstanceManager factory - * - * @param string $class - * @return Zend\Di\InstanceManager + * Set the instance manager + * + * @param InstanceManager $instanceManager + * @return DependencyInjector */ - public function createInstanceManager($class) + public function setInstanceManager(InstanceManager $instanceManager) { - $instanceManager = new $class(); - if (!$instanceManager instanceof InstanceManager) { - throw new Exception\InvalidArgumentException( - 'The class provided to the InstanceManager factory ' . $class - . ' does not implement the InstanceCollection interface' - ); - } - return $instanceManager; + $this->instanceManager = $instanceManager; + return $this; } - + /** * - * @return Zend\Di\InstanceManager + * @return InstanceManager */ public function getInstanceManager() { if ($this->instanceManager == null) { - $this->instanceManager = $this->createInstanceManager('Zend\Di\InstanceManager'); + $this->instanceManager = new InstanceManager(); } return $this->instanceManager; } @@ -160,15 +170,16 @@ public function get($name, array $params = array()) array_pop($this->instanceContext); return $instance; } - + /** * Retrieve a new instance of a class * * Forces retrieval of a discrete instance of the given class, using the * constructor parameters provided. - * - * @param mixed $name Class name or service alias - * @param array $params Parameters to pass to the constructor + * + * @param mixed $name Class name or service alias + * @param array $params Parameters to pass to the constructor + * @param bool $isShared * @return object|null */ public function newInstance($name, array $params = array(), $isShared = true) @@ -203,7 +214,7 @@ public function newInstance($name, array $params = array(), $isShared = true) unset($injectionMethods[array_search('__construct', $injectionMethods)]); } } elseif (is_callable($instantiator)) { - $object = $this->createInstanceViaCallback($instantiator, $params); + $object = $this->createInstanceViaCallback($instantiator, $params, $alias); // @todo make sure we can create via a real object factory throw new \Exception('incomplete implementation'); } else { @@ -249,16 +260,17 @@ public function newInstance($name, array $params = array(), $isShared = true) // { // // } - + /** * Retrieve a class instance based on class name * - * Any parameters provided will be used as constructor arguments. If any + * Any parameters provided will be used as constructor arguments. If any * given parameter is a DependencyReference object, it will be fetched * from the container so that the instance may be injected. - * - * @param string $class - * @param array $params + * + * @param string $class + * @param array $params + * @param string|null $alias * @return object */ protected function createInstanceViaConstructor($class, $params, $alias = null) @@ -277,23 +289,23 @@ protected function createInstanceViaConstructor($class, $params, $alias = null) case 2: return new $class($callParameters[0], $callParameters[1]); case 3: - return new $class($callParameters[0], $callParameters[1], $callParameters[3]); + return new $class($callParameters[0], $callParameters[1], $callParameters[2]); default: $r = new \ReflectionClass($class); return $r->newInstanceArgs($callParameters); } } - - + /** * Get an object instance from the defined callback - * - * @param callback $callback - * @param array $params + * + * @param callback $callback + * @param array $params + * @param string $alias * @return object * @throws Exception\InvalidCallbackException */ - protected function createInstanceViaCallback($callback, $params) + protected function createInstanceViaCallback($callback, $params, $alias) { if (!is_callable($callback)) { throw new Exception\InvalidCallbackException('An invalid constructor callback was provided'); @@ -306,18 +318,19 @@ protected function createInstanceViaCallback($callback, $params) $callParameters = array(); if ($this->definition->hasInjectionMethod($class, $method)) { - $callParameters = $this->resolveMethodParameters($class, $method, $params, true); + $callParameters = $this->resolveMethodParameters($class, $method, $params, true, $alias); } return call_user_func_array($callback, $callParameters); } - + /** * This parameter will handle any injection methods and resolution of - * dependencies for such methods - * + * dependencies for such methods + * * @param object $object * @param string $method * @param array $params + * @param string $alias */ protected function handleInjectionMethodForObject($object, $method, $params, $alias) { @@ -327,17 +340,22 @@ protected function handleInjectionMethodForObject($object, $method, $params, $al call_user_func_array(array($object, $method), $callParameters); } } - + /** * Resolve parameters referencing other services - * - * @param array $params + * + * @param string $class + * @param string $method + * @param array $callTimeUserParams + * @param bool $isInstantiator + * @param string $alias * @return array */ protected function resolveMethodParameters($class, $method, array $callTimeUserParams, $isInstantiator, $alias) { + /* @var $isSubclassFunc Closure */ static $isSubclassFunc = null; - static $isSubclassFuncCache = null; + static $isSubclassFuncCache = null; // null as unset, array when set $isSubclassFunc = function($class, $type) use (&$isSubclassFuncCache) { /* @see https://bugs.php.net/bug.php?id=53727 */ diff --git a/test/TestAsset/SetterInjection/Y.php b/test/TestAsset/SetterInjection/Y.php index bbc6b992..e28885a1 100644 --- a/test/TestAsset/SetterInjection/Y.php +++ b/test/TestAsset/SetterInjection/Y.php @@ -9,4 +9,4 @@ public function setX(X $x) { $this->x = $x; } -} \ No newline at end of file +}