diff --git a/src/Configuration.php b/src/Configuration.php index b788c3b0..a7457b3b 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -12,18 +12,26 @@ use Traversable; use Zend\Stdlib\ArrayUtils; +use Zend\Di\Definition\ArrayDefinition; +use Zend\Di\Definition\RuntimeDefinition; +/** + * Configures Di instances + * + * @category Zend + * @package Zend_Di + */ class Configuration { /** * @var array */ protected $data = array(); - + /** * Constructor * - * @param array|Traversable $options + * @param array|Traversable $options * @throws Exception\InvalidArgumentException */ public function __construct($options) @@ -43,7 +51,7 @@ public function __construct($options) /** * Configure * - * @param Di $di + * @param Di $di * @return void */ public function configure(Di $di) @@ -55,9 +63,13 @@ public function configure(Di $di) if (isset($this->data['instance'])) { $this->configureInstance($di, $this->data['instance']); } - + } + /** + * @param Di $di + * @param array $definition + */ public function configureDefinition(Di $di, $definition) { foreach ($definition as $definitionType => $definitionData) { @@ -65,7 +77,7 @@ public function configureDefinition(Di $di, $definition) case 'compiler': foreach ($definitionData as $filename) { if (is_readable($filename)) { - $di->definitions()->addDefinition(new \Zend\Di\Definition\ArrayDefinition(include $filename), false); + $di->definitions()->addDefinition(new ArrayDefinition(include $filename), false); } } break; @@ -74,16 +86,18 @@ public function configureDefinition(Di $di, $definition) // Remove runtime from definition list if not enabled $definitions = array(); foreach ($di->definitions() as $definition) { - if (!$definition instanceof \Zend\Di\Definition\RuntimeDefinition) { + if (!$definition instanceof RuntimeDefinition) { $definitions[] = $definition; } } $definitions = new DefinitionList($definitions); $di->setDefinitionList($definitions); } elseif (isset($definitionData['use_annotations']) && $definitionData['use_annotations']) { - $di->definitions()->getDefinitionByType('\Zend\Di\Definition\RuntimeDefinition') - ->getIntrospectionStrategy() - ->setUseAnnotations(true); + /* @var $runtimeDefinition Definition\RuntimeDefinition */ + $runtimeDefinition = $di + ->definitions() + ->getDefinitionByType('\Zend\Di\Definition\RuntimeDefinition'); + $runtimeDefinition->getIntrospectionStrategy()->setUseAnnotations(true); } break; case 'class': @@ -136,11 +150,17 @@ public function configureDefinition(Di $di, $definition) } } - + + /** + * Configures a given Di instance + * + * @param Di $di + * @param $instanceData + */ public function configureInstance(Di $di, $instanceData) { $im = $di->instanceManager(); - + foreach ($instanceData as $target => $data) { switch (strtolower($target)) { case 'aliases': @@ -183,5 +203,4 @@ public function configureInstance(Di $di, $instanceData) } - } diff --git a/src/Definition/Annotation/Inject.php b/src/Definition/Annotation/Inject.php index 78c399e0..46a0131e 100644 --- a/src/Definition/Annotation/Inject.php +++ b/src/Definition/Annotation/Inject.php @@ -12,11 +12,22 @@ use Zend\Code\Annotation\AnnotationInterface; +/** + * Annotation for injection endpoints for dependencies + * + * @category Zend + * @package Zend_Di + */ class Inject implements AnnotationInterface { - + /** + * @var mixed + */ protected $content = null; + /** + * {@inheritDoc} + */ public function initialize($content) { $this->content = $content; diff --git a/src/Definition/Annotation/Instantiator.php b/src/Definition/Annotation/Instantiator.php index c679d8ce..2de9f205 100644 --- a/src/Definition/Annotation/Instantiator.php +++ b/src/Definition/Annotation/Instantiator.php @@ -12,11 +12,22 @@ use Zend\Code\Annotation\AnnotationInterface; +/** + * Annotation for instantiator + * + * @category Zend + * @package Zend_Di + */ class Instantiator implements AnnotationInterface { - + /** + * @var mixed + */ protected $content = null; + /** + * {@inheritDoc} + */ public function initialize($content) { $this->content = $content; diff --git a/src/Definition/ArrayDefinition.php b/src/Definition/ArrayDefinition.php index 3f3c8bae..48a53c59 100644 --- a/src/Definition/ArrayDefinition.php +++ b/src/Definition/ArrayDefinition.php @@ -10,12 +10,23 @@ namespace Zend\Di\Definition; +/** + * Class definitions based on a given array + * + * @category Zend + * @package Zend_Di + */ class ArrayDefinition implements DefinitionInterface { - + /** + * @var array + */ protected $dataArray = array(); - - public function __construct(Array $dataArray) + + /** + * @param array $dataArray + */ + public function __construct(array $dataArray) { foreach ($dataArray as $class => $value) { // force lower names @@ -23,109 +34,134 @@ public function __construct(Array $dataArray) } $this->dataArray = $dataArray; } - + + /** + * {@inheritDoc} + */ public function getClasses() { return array_keys($this->dataArray); } - + + /** + * {@inheritDoc} + */ public function hasClass($class) { return array_key_exists($class, $this->dataArray); } - + + /** + * {@inheritDoc} + */ public function getClassSupertypes($class) { if (!isset($this->dataArray[$class])) { return array(); } - + if (!isset($this->dataArray[$class]['supertypes'])) { return array(); } - + return $this->dataArray[$class]['supertypes']; } - + + /** + * {@inheritDoc} + */ public function getInstantiator($class) { if (!isset($this->dataArray[$class])) { return null; } - + if (!isset($this->dataArray[$class]['instantiator'])) { return '__construct'; } - + return $this->dataArray[$class]['instantiator']; } - + + /** + * {@inheritDoc} + */ public function hasMethods($class) { if (!isset($this->dataArray[$class])) { return array(); } - + if (!isset($this->dataArray[$class]['methods'])) { return array(); } - + return (count($this->dataArray[$class]['methods']) > 0); } - + + /** + * {@inheritDoc} + */ public function hasMethod($class, $method) { if (!isset($this->dataArray[$class])) { return false; } - + if (!isset($this->dataArray[$class]['methods'])) { return false; } - + return array_key_exists($method, $this->dataArray[$class]['methods']); } - + + /** + * {@inheritDoc} + */ public function getMethods($class) { if (!isset($this->dataArray[$class])) { return array(); } - + if (!isset($this->dataArray[$class]['methods'])) { return array(); } - + return $this->dataArray[$class]['methods']; } /** - * @param $class - * @param $method - * @return bool + * {@inheritDoc} */ public function hasMethodParameters($class, $method) { return isset($this->dataArray[$class]['parameters'][$method]); } + /** + * {@inheritDoc} + */ public function getMethodParameters($class, $method) { if (!isset($this->dataArray[$class])) { return array(); } - + if (!isset($this->dataArray[$class]['parameters'])) { return array(); } - + if (!isset($this->dataArray[$class]['parameters'][$method])) { return array(); } - + return $this->dataArray[$class]['parameters'][$method]; } - + + /** + * @return array + */ public function toArray() { return $this->dataArray; diff --git a/src/Definition/Builder/InjectionMethod.php b/src/Definition/Builder/InjectionMethod.php index c8525175..e52f9e99 100644 --- a/src/Definition/Builder/InjectionMethod.php +++ b/src/Definition/Builder/InjectionMethod.php @@ -10,24 +10,49 @@ namespace Zend\Di\Definition\Builder; +/** + * Definitions for an injection endpoint method + * + * @category Zend + * @package Zend_Di + */ class InjectionMethod { - const PARAMETER_POSTION_NEXT = 'next'; - + /** + * @var string|null + */ protected $name = null; + + /** + * @var array + */ protected $parameters = array(); - + + /** + * @param string|null $name + * @return self + */ public function setName($name) { $this->name = $name; + return $this; } - + + /** + * @return null|string + */ public function getName() { return $this->name; } - + + /** + * @param string $name + * @param string|null $class + * @param mixed|null $isRequired + * @return InjectionMethod + */ public function addParameter($name, $class = null, $isRequired = null) { $this->parameters[] = array( @@ -35,12 +60,16 @@ public function addParameter($name, $class = null, $isRequired = null) $class, ($isRequired == null) ? true : false ); + return $this; } - + + /** + * @return array + */ public function getParameters() { return $this->parameters; } - + } diff --git a/src/Definition/Builder/PhpClass.php b/src/Definition/Builder/PhpClass.php index f8ea9f8c..f9f1b865 100644 --- a/src/Definition/Builder/PhpClass.php +++ b/src/Definition/Builder/PhpClass.php @@ -10,23 +10,49 @@ namespace Zend\Di\Definition\Builder; +/** + * Object containing definitions for a single class + * + * @category Zend + * @package Zend_Di + */ class PhpClass { + /** + * @var string + */ protected $defaultMethodBuilder = 'Zend\Di\Definition\Builder\InjectionMethod'; + + /** + * @var null|string + */ protected $name = null; + + /** + * @var string|\Callable|array + */ protected $instantiator = '__construct'; + + /** + * @var InjectionMethod[] + */ protected $injectionMethods = array(); + + /** + * @var array + */ protected $superTypes = array(); /** * Set name * - * @param string $name + * @param string $name * @return PhpClass */ public function setName($name) { $this->name = $name; + return $this; } @@ -39,21 +65,34 @@ public function getName() { return $this->name; } - + + /** + * @param string|\Callable|array $instantiator + * @return PhpClass + */ public function setInstantiator($instantiator) { $this->instantiator = $instantiator; + return $this; } - + + /** + * @return array|\Callable|string + */ public function getInstantiator() { return $this->instantiator; } + /** + * @param string $superType + * @return PhpClass + */ public function addSuperType($superType) { $this->superTypes[] = $superType; + return $this; } @@ -70,12 +109,13 @@ public function getSuperTypes() /** * Add injection method * - * @param InjectionMethod $injectionMethod + * @param InjectionMethod $injectionMethod * @return PhpClass */ public function addInjectionMethod(InjectionMethod $injectionMethod) { $this->injectionMethods[] = $injectionMethod; + return $this; } @@ -84,33 +124,36 @@ public function addInjectionMethod(InjectionMethod $injectionMethod) * * Optionally takes the method name. * - * This method may be used in lieu of addInjectionMethod() in + * This method may be used in lieu of addInjectionMethod() in * order to provide a more fluent interface for building classes with * injection methods. - * - * @param null|string $name + * + * @param null|string $name * @return InjectionMethod */ public function createInjectionMethod($name = null) { $builder = $this->defaultMethodBuilder; + /* @var $method InjectionMethod */ $method = new $builder(); if (null !== $name) { $method->setName($name); } $this->addInjectionMethod($method); + return $method; } /** * Override which class will be used by {@link createInjectionMethod()} - * - * @param string $class + * + * @param string $class * @return PhpClass */ public function setMethodBuilder($class) { $this->defaultMethodBuilder = $class; + return $this; } @@ -118,14 +161,17 @@ public function setMethodBuilder($class) * Determine what class will be used by {@link createInjectionMethod()} * * Mainly to provide the ability to temporarily override the class used. - * + * * @return string */ public function getMethodBuilder() { return $this->defaultMethodBuilder; } - + + /** + * @return InjectionMethod[] + */ public function getInjectionMethods() { return $this->injectionMethods; diff --git a/src/Definition/BuilderDefinition.php b/src/Definition/BuilderDefinition.php index a1b6c076..fb9ee2d4 100644 --- a/src/Definition/BuilderDefinition.php +++ b/src/Definition/BuilderDefinition.php @@ -12,6 +12,12 @@ use Zend\Di\Exception; +/** + * Class definitions based on a configuration array + * + * @category Zend + * @package Zend_Di + */ class BuilderDefinition implements DefinitionInterface { /** @@ -20,14 +26,14 @@ class BuilderDefinition implements DefinitionInterface protected $defaultClassBuilder = 'Zend\Di\Definition\Builder\PhpClass'; /** - * @var array + * @var Builder\PhpClass[] */ protected $classes = array(); /** * Create classes from array * - * @param array $builderData + * @param array $builderData * @return void */ public function createClassesFromArray(array $builderData) @@ -57,7 +63,7 @@ public function createClassesFromArray(array $builderData) $class->addInjectionMethod($injectionMethod); } break; - + } } $this->addClass($class); @@ -67,12 +73,13 @@ public function createClassesFromArray(array $builderData) /** * Add class * - * @param Builder\PhpClass $phpClass + * @param Builder\PhpClass $phpClass * @return BuilderDefinition */ public function addClass(Builder\PhpClass $phpClass) { $this->classes[] = $phpClass; + return $this; } @@ -80,40 +87,44 @@ public function addClass(Builder\PhpClass $phpClass) * Create a class builder object using default class builder class * * This method is a factory that can be used in place of addClass(). - * - * @param null|string $name Optional name of class to assign + * + * @param null|string $name Optional name of class to assign * @return Builder\PhpClass */ public function createClass($name = null) { $builderClass = $this->defaultClassBuilder; + /* @var $class Builder\PhpClass */ $class = new $builderClass(); + if (null !== $name) { $class->setName($name); } $this->addClass($class); + return $class; } /** * Set the class to use with {@link createClass()} - * - * @param string $class + * + * @param string $class * @return BuilderDefinition */ public function setClassBuilder($class) { $this->defaultClassBuilder = $class; + return $this; } /** * Get the class used for {@link createClass()} * - * This is primarily to allow developers to temporarily override + * This is primarily to allow developers to temporarily override * the builder strategy. - * + * * @return string */ public function getClassBuilder() @@ -122,20 +133,22 @@ public function getClassBuilder() } /** - * @return array + * {@inheritDoc} */ public function getClasses() { $classNames = array(); + + /* @var $class Builder\PhpClass */ foreach ($this->classes as $class) { $classNames[] = $class->getName(); } + return $classNames; } /** - * @param string $class - * @return bool + * {@inheritDoc} */ public function hasClass($class) { @@ -144,12 +157,13 @@ public function hasClass($class) return true; } } + return false; } /** - * @param string $name - * @return bool + * @param string $name + * @return bool|Builder\PhpClass */ protected function getClass($name) { @@ -158,27 +172,28 @@ protected function getClass($name) return $classObj; } } + return false; } /** - * @param string $class - * @return array - * @throws Exception\RuntimeException + * {@inheritDoc} + * @throws \Zend\Di\Exception\RuntimeException */ public function getClassSupertypes($class) { $class = $this->getClass($class); + if ($class === false) { throw new Exception\RuntimeException('Cannot find class object in this builder definition.'); } + return $class->getSuperTypes(); } /** - * @param string $class - * @return array|string - * @throws Exception\RuntimeException + * {@inheritDoc} + * @throws \Zend\Di\Exception\RuntimeException */ public function getInstantiator($class) { @@ -186,13 +201,13 @@ public function getInstantiator($class) if ($class === false) { throw new Exception\RuntimeException('Cannot find class object in this builder definition.'); } + return $class->getInstantiator(); } /** - * @param string $class - * @return bool - * @throws Exception\RuntimeException + * {@inheritDoc} + * @throws \Zend\Di\Exception\RuntimeException */ public function hasMethods($class) { @@ -201,13 +216,13 @@ public function hasMethods($class) if ($class === false) { throw new Exception\RuntimeException('Cannot find class object in this builder definition.'); } + return (count($class->getInjectionMethods()) > 0); } /** - * @param string $class - * @return array - * @throws Exception\RuntimeException + * {@inheritDoc} + * @throws \Zend\Di\Exception\RuntimeException */ public function getMethods($class) { @@ -217,17 +232,18 @@ public function getMethods($class) } $methods = $class->getInjectionMethods(); $methodNames = array(); + + /* @var $methodObj Builder\InjectionMethod */ foreach ($methods as $methodObj) { $methodNames[] = $methodObj->getName(); } + return $methodNames; } /** - * @param string $class - * @param string $method - * @return bool - * @throws Exception\RuntimeException + * {@inheritDoc} + * @throws \Zend\Di\Exception\RuntimeException */ public function hasMethod($class, $method) { @@ -236,18 +252,19 @@ public function hasMethod($class, $method) throw new Exception\RuntimeException('Cannot find class object in this builder definition.'); } $methods = $class->getInjectionMethods(); + + /* @var $methodObj Builder\InjectionMethod */ foreach ($methods as $methodObj) { if ($methodObj->getName() === $method) { return true; } } + return false; } /** - * @param string $class - * @param string $method - * @return bool + * {@inheritDoc} */ public function hasMethodParameters($class, $method) { @@ -256,6 +273,7 @@ public function hasMethodParameters($class, $method) return false; } $methods = $class->getInjectionMethods(); + /* @var $methodObj Builder\InjectionMethod */ foreach ($methods as $methodObj) { if ($methodObj->getName() === $method) { $method = $methodObj; @@ -264,14 +282,15 @@ public function hasMethodParameters($class, $method) if (!$method instanceof Builder\InjectionMethod) { return false; } + + /* @var $method Builder\InjectionMethod */ + return (count($method->getParameters()) > 0); } /** - * @param string $class - * @param string $method - * @return array - * @throws Exception\RuntimeException + * {@inheritDoc} + * @throws \Zend\Di\Exception\RuntimeException */ public function getMethodParameters($class, $method) { @@ -280,6 +299,7 @@ public function getMethodParameters($class, $method) throw new Exception\RuntimeException('Cannot find class object in this builder definition.'); } $methods = $class->getInjectionMethods(); + /* @var $methodObj Builder\InjectionMethod */ foreach ($methods as $methodObj) { if ($methodObj->getName() === $method) { $method = $methodObj; @@ -289,11 +309,12 @@ public function getMethodParameters($class, $method) throw new Exception\RuntimeException('Cannot find method object for method ' . $method . ' in this builder definition.'); } $methodParameters = array(); + /* @var $method Builder\InjectionMethod */ foreach ($method->getParameters() as $name => $info) { $methodParameters[$class->getName() . '::' . $method->getName() . ':' . $name] = $info; } + return $methodParameters; } - } diff --git a/src/Definition/ClassDefinition.php b/src/Definition/ClassDefinition.php index 498edea9..9249d931 100644 --- a/src/Definition/ClassDefinition.php +++ b/src/Definition/ClassDefinition.php @@ -10,37 +10,73 @@ namespace Zend\Di\Definition; +/** + * Class definitions for a single class + * + * @category Zend + * @package Zend_Di + */ class ClassDefinition implements DefinitionInterface, PartialMarker { - + /** + * @var null|string + */ protected $class = null; + + /** + * @var string[] + */ protected $supertypes = array(); + + /** + * @var null|\Callable|array|string + */ protected $instantiator = null; + + /** + * @var bool[] + */ protected $methods = array(); - protected $methodParameters = array(); + /** + * @var array + */ + protected $methodParameters = array(); + /** + * @param string $class + */ public function __construct($class) { $this->class = $class; } + /** + * @param null|\Callable|array|string $instantiator + * @return self + */ public function setInstantiator($instantiator) { $this->instantiator = $instantiator; + return $this; } + /** + * @param string[] $supertypes + * @return self + */ public function setSupertypes(array $supertypes) { $this->supertypes = $supertypes; + return $this; } /** - * @param string $method - * @param bool|null $isRequired - * @return ClassDefinition + * @param string $method + * @param bool|null $isRequired + * @return self */ public function addMethod($method, $isRequired = null) { @@ -55,7 +91,7 @@ public function addMethod($method, $isRequired = null) /** * @param $method * @param $parameterName - * @param array $parameterInfo (keys: required, type) + * @param array $parameterInfo (keys: required, type) * @return ClassDefinition */ public function addMethodParameter($method, $parameterName, array $parameterInfo) @@ -75,12 +111,12 @@ public function addMethodParameter($method, $parameterName, array $parameterInfo $this->methodParameters[$method][$fqName] = array( $parameterName, $type, $required ); - + return $this; } /** - * @return string[] + * {@inheritDoc} */ public function getClasses() { @@ -88,8 +124,7 @@ public function getClasses() } /** - * @param string $class - * @return bool + * {@inheritDoc} */ public function hasClass($class) { @@ -97,8 +132,7 @@ public function hasClass($class) } /** - * @param string $class - * @return string[] + * {@inheritDoc} */ public function getClassSupertypes($class) { @@ -106,8 +140,7 @@ public function getClassSupertypes($class) } /** - * @param string $class - * @return string|array + * {@inheritDoc} */ public function getInstantiator($class) { @@ -115,8 +148,7 @@ public function getInstantiator($class) } /** - * @param string $class - * @return bool + * {@inheritDoc} */ public function hasMethods($class) { @@ -124,8 +156,7 @@ public function hasMethods($class) } /** - * @param string $class - * @return string[] + * {@inheritDoc} */ public function getMethods($class) { @@ -133,9 +164,7 @@ public function getMethods($class) } /** - * @param string $class - * @param string $method - * @return bool + * {@inheritDoc} */ public function hasMethod($class, $method) { @@ -147,9 +176,7 @@ public function hasMethod($class, $method) } /** - * @param string $class - * @param string $method - * @return bool + * {@inheritDoc} */ public function hasMethodParameters($class, $method) { @@ -157,27 +184,14 @@ public function hasMethodParameters($class, $method) } /** - * getMethodParameters() return information about a methods parameters. - * - * Should return an ordered named array of parameters for a given method. - * Each value should be an array, of length 4 with the following information: - * - * array( - * 0, // string|null: Type Name (if it exists) - * 1, // bool: whether this param is required - * 2, // string: fully qualified path to this parameter - * ); - * - * - * @param $class - * @param $method - * @return array[] + * {@inheritDoc} */ public function getMethodParameters($class, $method) { if (array_key_exists($method, $this->methodParameters)) { return $this->methodParameters[$method]; } + return null; } } diff --git a/src/Definition/CompilerDefinition.php b/src/Definition/CompilerDefinition.php index 07b38711..be08fb8c 100644 --- a/src/Definition/CompilerDefinition.php +++ b/src/Definition/CompilerDefinition.php @@ -11,7 +11,6 @@ namespace Zend\Di\Definition; use Zend\Code\Annotation\AnnotationCollection; -use Zend\Code\Annotation\AnnotationManager; use Zend\Code\Reflection; use Zend\Code\Scanner\AggregateDirectoryScanner; use Zend\Code\Scanner\DerivedClassScanner; @@ -19,6 +18,12 @@ use Zend\Code\Scanner\FileScanner; use Zend\Di\Definition\Annotation; +/** + * Class definitions based on a set of directories to be scanned + * + * @category Zend + * @package Zend_Di + */ class CompilerDefinition implements DefinitionInterface { protected $isCompiled = false; @@ -55,6 +60,9 @@ public function setIntrospectionStrategy(IntrospectionStrategy $introspectionStr $this->introspectionStrategy = $introspectionStrategy; } + /** + * @param bool $allowReflectionExceptions + */ public function setAllowReflectionExceptions($allowReflectionExceptions = true) { $this->allowReflectionExceptions = (bool) $allowReflectionExceptions; @@ -111,14 +119,15 @@ public function addCodeScannerFile(FileScanner $fileScanner) */ public function compile() { - /* - * @var $classScanner \Zend\Code\Scanner\DerivedClassScanner - */ + /* @var $classScanner \Zend\Code\Scanner\DerivedClassScanner */ foreach ($this->directoryScanner->getClassNames() as $class) { $this->processClass($class); } } + /** + * @return ArrayDefinition + */ public function toArrayDefinition() { return new ArrayDefinition( @@ -126,6 +135,10 @@ public function toArrayDefinition() ); } + /** + * @param string $class + * @throws \ReflectionException + */ protected function processClass($class) { $strategy = $this->introspectionStrategy; // localize for readability @@ -136,6 +149,7 @@ protected function processClass($class) if (!$this->allowReflectionExceptions) { throw $e; } + return; } $className = $rClass->getName(); @@ -158,10 +172,11 @@ protected function processClass($class) if (($annotations instanceof AnnotationCollection) && $annotations->hasAnnotation('Zend\Di\Definition\Annotation\Instantiator') ) { - // @todo Instnatiator support in annotations + // @todo Instantiator support in annotations } } + /* @var $rTarget \Zend\Code\Reflection\ClassReflection */ $rTarget = $rClass; $supertypes = array(); do { @@ -189,11 +204,11 @@ protected function processClass($class) if (!$this->allowReflectionExceptions) { throw $e; } + return; } } - foreach ($rClass->getMethods(Reflection\MethodReflection::IS_PUBLIC) as $rMethod) { $methodName = $rMethod->getName(); @@ -227,7 +242,6 @@ protected function processClass($class) } } - // method // by annotation // by setter pattern, @@ -244,7 +258,8 @@ protected function processClass($class) preg_match($interfaceInjectorPattern, $rIface->getName(), $matches); if ($matches) { foreach ($rIface->getMethods() as $rMethod) { - if ($rMethod->getName() === '__construct') { // ctor not allowed in ifaces + if ($rMethod->getName() === '__construct') { + // constructor not allowed in interfaces continue; } $def['methods'][$rMethod->getName()] = true; @@ -256,6 +271,11 @@ protected function processClass($class) } } + /** + * @param array $def + * @param \Zend\Code\Reflection\ClassReflection $rClass + * @param \Zend\Code\Reflection\MethodReflection $rMethod + */ protected function processParams(&$def, Reflection\ClassReflection $rClass, Reflection\MethodReflection $rMethod) { if (count($rMethod->getParameters()) === 0) { @@ -374,7 +394,8 @@ protected function processParams(&$def, Reflection\ClassReflection $rClass, Refl // preg_match($interfaceInjectorPattern, $sInterface->getName(), $matches); // if ($matches) { // foreach ($sInterface->getMethods(true) as $sMethod) { -// if ($sMethod->getName() === '__construct') { // ctor not allowed in ifaces +// if ($sMethod->getName() === '__construct') { + // constructor not allowed in interfaces // continue; // } // $def['methods'][$sMethod->getName()] = true; @@ -440,9 +461,7 @@ protected function processParams(&$def, Reflection\ClassReflection $rClass, Refl // } /** - * Return nothing - * - * @return array + * {@inheritDoc} */ public function getClasses() { @@ -450,10 +469,7 @@ public function getClasses() } /** - * Return whether the class exists - * - * @param string $class - * @return bool + * {@inheritDoc} */ public function hasClass($class) { @@ -461,101 +477,86 @@ public function hasClass($class) } /** - * Return the supertypes for this class - * - * @param string $class - * @return array of types + * {@inheritDoc} */ public function getClassSupertypes($class) { if (!array_key_exists($class, $this->classes)) { $this->processClass($class); } + return $this->classes[$class]['supertypes']; } /** - * Get the instantiator - * - * @param string $class - * @return string|callable + * {@inheritDoc} */ public function getInstantiator($class) { if (!array_key_exists($class, $this->classes)) { $this->processClass($class); } + return $this->classes[$class]['instantiator']; } /** - * Return if there are injection methods - * - * @param string $class - * @return bool + * {@inheritDoc} */ public function hasMethods($class) { if (!array_key_exists($class, $this->classes)) { $this->processClass($class); } + return (count($this->classes[$class]['methods']) > 0); } /** - * Return injection methods - * - * @param string $class - * @param string $method - * @return bool + * {@inheritDoc} */ public function hasMethod($class, $method) { if (!array_key_exists($class, $this->classes)) { $this->processClass($class); } + return isset($this->classes[$class]['methods'][$method]); } /** - * Return an array of the injection methods - * - * @param string $class - * @return array + * {@inheritDoc} */ public function getMethods($class) { if (!array_key_exists($class, $this->classes)) { $this->processClass($class); } + return $this->classes[$class]['methods']; } + /** + * {@inheritDoc} + */ public function hasMethodParameters($class, $method) { if (!isset($this->classes[$class])) { return false; } + return (array_key_exists($method, $this->classes[$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 - * - * @param string $class - * @param string $method - * @return array + * {@inheritDoc} */ public function getMethodParameters($class, $method) { if (!is_array($this->classes[$class])) { $this->processClass($class); } + return $this->classes[$class]['parameters'][$method]; } } diff --git a/src/Definition/DefinitionInterface.php b/src/Definition/DefinitionInterface.php index 9b5370cd..a642d9d2 100644 --- a/src/Definition/DefinitionInterface.php +++ b/src/Definition/DefinitionInterface.php @@ -10,53 +10,67 @@ namespace Zend\Di\Definition; +/** + * @category Zend + * @package Zend_Di + */ interface DefinitionInterface { /** + * Retrieves classes in this definition + * * @abstract * @return string[] */ public function getClasses(); /** + * Return whether the class exists in this definition + * * @abstract - * @param string $class + * @param string $class * @return bool */ public function hasClass($class); /** + * Return the supertypes for this class + * * @abstract - * @param string $class + * @param string $class * @return string[] */ public function getClassSupertypes($class); /** * @abstract - * @param string $class + * @param string $class * @return string|array */ public function getInstantiator($class); /** + * Return if there are injection methods + * * @abstract - * @param string $class + * @param string $class * @return bool */ public function hasMethods($class); /** + * Return an array of the injection methods for a given class + * * @abstract - * @param string $class + * @param string $class * @return string[] */ public function getMethods($class); /** * @abstract - * @param string $class - * @param string $method + * @param string $class + * @param string $method * @return bool */ public function hasMethod($class, $method); @@ -83,10 +97,9 @@ public function hasMethodParameters($class, $method); * * * @abstract - * @param $class - * @param $method - * @return array[] + * @param string $class + * @param string $method + * @return array */ public function getMethodParameters($class, $method); } - diff --git a/src/Definition/IntrospectionStrategy.php b/src/Definition/IntrospectionStrategy.php index da3845bc..45aec4d5 100644 --- a/src/Definition/IntrospectionStrategy.php +++ b/src/Definition/IntrospectionStrategy.php @@ -13,6 +13,13 @@ use Zend\Code\Annotation\AnnotationManager; use Zend\Code\Annotation\Parser\GenericAnnotationParser; +/** + * Strategy used to discover methods to be considered as endpoints for dependency injection based on implemented + * interfaces, annotations and method names + * + * @category Zend + * @package Zend_Di + */ class IntrospectionStrategy { /** @@ -66,6 +73,7 @@ public function createDefaultAnnotationManager() $parser = new GenericAnnotationParser(); $parser->registerAnnotation(new Annotation\Inject()); $annotationManager->attach($parser); + return $annotationManager; } diff --git a/src/Definition/PartialMarker.php b/src/Definition/PartialMarker.php index 01c657b7..4a34203b 100644 --- a/src/Definition/PartialMarker.php +++ b/src/Definition/PartialMarker.php @@ -10,6 +10,10 @@ namespace Zend\Di\Definition; +/** + * @category Zend + * @package Zend_Di + */ interface PartialMarker { } diff --git a/src/Definition/RuntimeDefinition.php b/src/Definition/RuntimeDefinition.php index 7df58e94..f9b821bb 100644 --- a/src/Definition/RuntimeDefinition.php +++ b/src/Definition/RuntimeDefinition.php @@ -11,10 +11,15 @@ namespace Zend\Di\Definition; use Zend\Code\Annotation\AnnotationCollection; -use Zend\Code\Annotation\AnnotationManager; use Zend\Code\Reflection; use Zend\Di\Definition\Annotation; +/** + * Class definitions based on runtime reflection + * + * @category Zend + * @package Zend_Di + */ class RuntimeDefinition implements DefinitionInterface { @@ -42,7 +47,7 @@ class RuntimeDefinition implements DefinitionInterface * Constructor * * @param null|IntrospectionStrategy $introspectionStrategy - * @param array|null $explicitClasses + * @param array|null $explicitClasses */ public function __construct(IntrospectionStrategy $introspectionStrategy = null, array $explicitClasses = null) { @@ -53,7 +58,7 @@ public function __construct(IntrospectionStrategy $introspectionStrategy = null, } /** - * @param IntrospectionStrategy $introspectionStrategy + * @param IntrospectionStrategy $introspectionStrategy * @return void */ public function setIntrospectionStrategy(IntrospectionStrategy $introspectionStrategy) @@ -83,15 +88,16 @@ public function setExplicitClasses(array $explicitClasses) $this->classes = $explicitClasses; } + /** + * @param string $class + */ public function forceLoadClass($class) { $this->processClass($class); } /** - * Retrieves registered classes names - * - * @return array + * {@inheritDoc} */ public function getClasses() { @@ -99,10 +105,7 @@ public function getClasses() } /** - * Return whether the class exists - * - * @param string $class - * @return bool + * {@inheritDoc} */ public function hasClass($class) { @@ -114,111 +117,92 @@ public function hasClass($class) } /** - * Return the supertypes for this class - * - * @param string $class - * @return array of types + * {@inheritDoc} */ public function getClassSupertypes($class) { if (!array_key_exists($class, $this->classes)) { $this->processClass($class); } + return $this->classes[$class]['supertypes']; } /** - * Get the instantiator - * - * @param string $class - * @return string|callable + * {@inheritDoc} */ public function getInstantiator($class) { if (!array_key_exists($class, $this->classes)) { $this->processClass($class); } + return $this->classes[$class]['instantiator']; } /** - * Return if there are injection methods - * - * @param string $class - * @return bool + * {@inheritDoc} */ public function hasMethods($class) { if (!array_key_exists($class, $this->classes)) { $this->processClass($class); } + return (count($this->classes[$class]['methods']) > 0); } /** - * Return injection methods - * - * @param string $class - * @param string $method - * @return bool + * {@inheritDoc} */ public function hasMethod($class, $method) { if (!array_key_exists($class, $this->classes)) { $this->processClass($class); } + return isset($this->classes[$class]['methods'][$method]); } /** - * Return an array of the injection methods - * - * @param string $class - * @return array + * {@inheritDoc} */ public function getMethods($class) { if (!array_key_exists($class, $this->classes)) { $this->processClass($class); } + return $this->classes[$class]['methods']; } /** - * Check if method has parameters - * - * @param string $class - * @param string $method - * @return bool + * {@inheritDoc} */ public function hasMethodParameters($class, $method) { if (!isset($this->classes[$class])) { return false; } + return (array_key_exists($method, $this->classes[$class]['parameters'])); } /** - * 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 - * - * @param string $class - * @param string $method - * @return array + * {@inheritDoc} */ public function getMethodParameters($class, $method) { if (!is_array($this->classes[$class])) { $this->processClass($class); } + return $this->classes[$class]['parameters'][$method]; } + /** + * @param string $class + */ protected function processClass($class) { $strategy = $this->introspectionStrategy; // localize for readability @@ -244,7 +228,7 @@ protected function processClass($class) if (($annotations instanceof AnnotationCollection) && $annotations->hasAnnotation('Zend\Di\Definition\Annotation\Instantiator')) { - // @todo Instnatiator support in annotations + // @todo Instantiator support in annotations } } @@ -272,7 +256,6 @@ protected function processClass($class) $this->processParams($def, $rClass, $rClass->getMethod('__construct')); } - foreach ($rClass->getMethods(Reflection\MethodReflection::IS_PUBLIC) as $rMethod) { $methodName = $rMethod->getName(); @@ -305,7 +288,6 @@ protected function processClass($class) } } - // method // by annotation // by setter pattern, @@ -322,7 +304,8 @@ protected function processClass($class) preg_match($interfaceInjectorPattern, $rIface->getName(), $matches); if ($matches) { foreach ($rIface->getMethods() as $rMethod) { - if ($rMethod->getName() === '__construct') { // ctor not allowed in ifaces + if ($rMethod->getName() === '__construct') { + // constructor not allowed in interfaces continue; } $def['methods'][$rMethod->getName()] = true; @@ -334,6 +317,11 @@ protected function processClass($class) } } + /** + * @param array $def + * @param \Zend\Code\Reflection\ClassReflection $rClass + * @param \Zend\Code\Reflection\MethodReflection $rMethod + */ protected function processParams(&$def, Reflection\ClassReflection $rClass, Reflection\MethodReflection $rMethod) { if (count($rMethod->getParameters()) === 0) { diff --git a/src/DefinitionList.php b/src/DefinitionList.php index a5a5b389..47061f3b 100644 --- a/src/DefinitionList.php +++ b/src/DefinitionList.php @@ -12,9 +12,17 @@ use SplDoublyLinkedList; +/** + * Class definition based on multiple definitions + * + * @category Zend + * @package Zend_Di + */ class DefinitionList extends SplDoublyLinkedList implements Definition\DefinitionInterface { - + /** + * @param Definition\DefinitionInterface|Definition\DefinitionInterface[] $definitions + */ public function __construct($definitions) { if (!is_array($definitions)) { @@ -28,8 +36,8 @@ public function __construct($definitions) /** * Add definitions * - * @param Definition\DefinitionInterface $definition - * @param bool $addToBackOfList + * @param Definition\DefinitionInterface $definition + * @param bool $addToBackOfList * @return void */ public function addDefinition(Definition\DefinitionInterface $definition, $addToBackOfList = true) @@ -42,7 +50,7 @@ public function addDefinition(Definition\DefinitionInterface $definition, $addTo } /** - * @param string $type + * @param string $type * @return Definition[] */ public function getDefinitionsByType($type) @@ -53,13 +61,14 @@ public function getDefinitionsByType($type) $definitions[] = $definition; } } + return $definitions; } /** * Get definition by type * - * @param string $type + * @param string $type * @return Definition\DefinitionInterface */ public function getDefinitionByType($type) @@ -69,9 +78,14 @@ public function getDefinitionByType($type) return $definition; } } + return false; } + /** + * @param string $class + * @return bool|Definition\DefinitionInterface + */ public function getDefinitionForClass($class) { /** @var $definition Definition\DefinitionInterface */ @@ -80,18 +94,21 @@ public function getDefinitionForClass($class) return $definition; } } + return false; } + /** + * @param string $class + * @return bool|Definition\DefinitionInterface + */ public function forClass($class) { return $this->getDefinitionForClass($class); } /** - * Get classes - * - * @return array + * {@inheritDoc} */ public function getClasses() { @@ -100,14 +117,12 @@ public function getClasses() foreach ($this as $definition) { $classes = array_merge($classes, $definition->getClasses()); } + return $classes; } /** - * Check for class - * - * @param string $class - * @return bool + * {@inheritDoc} */ public function hasClass($class) { @@ -117,9 +132,13 @@ public function hasClass($class) return true; } } + return false; } - + + /** + * {@inheritDoc} + */ public function getClassSupertypes($class) { $supertypes = array(); @@ -130,7 +149,10 @@ public function getClassSupertypes($class) // @todo remove duplicates? return $supertypes; } - + + /** + * {@inheritDoc} + */ public function getInstantiator($class) { /** @var $definition Definition\DefinitionInterface */ @@ -144,14 +166,12 @@ public function getInstantiator($class) } } } + return false; } /** - * Check for methods - * - * @param string $class - * @return bool + * {@inheritDoc} */ public function hasMethods($class) { @@ -165,15 +185,12 @@ public function hasMethods($class) } } } + return false; } /** - * Check for method - * - * @param string $class - * @param string $method - * @return bool + * {@inheritDoc} */ public function hasMethod($class, $method) { @@ -187,14 +204,12 @@ public function hasMethod($class, $method) } } } + return false; } /** - * Get methods - * - * @param string $class - * @return array| + * {@inheritDoc} */ public function getMethods($class) { @@ -209,15 +224,23 @@ public function getMethods($class) } } } + return $methods; } + /** + * {@inheritDoc} + */ public function hasMethodParameters($class, $method) { $methodParameters = $this->getMethodParameters($class, $method); + return ($methodParameters !== array()); } + /** + * {@inheritDoc} + */ public function getMethodParameters($class, $method) { /** @var $definition Definition\DefinitionInterface */ @@ -226,7 +249,8 @@ public function getMethodParameters($class, $method) return $definition->getMethodParameters($class, $method); } } + return array(); } - + } diff --git a/src/DependencyInjectionInterface.php b/src/DependencyInjectionInterface.php index f2820305..dedf955c 100644 --- a/src/DependencyInjectionInterface.php +++ b/src/DependencyInjectionInterface.php @@ -10,6 +10,10 @@ namespace Zend\Di; +/** + * @category Zend + * @package Zend_Di + */ interface DependencyInjectionInterface extends LocatorInterface { /** @@ -17,9 +21,9 @@ interface DependencyInjectionInterface extends LocatorInterface * * 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 * @return object|null */ public function newInstance($name, array $params = array()); diff --git a/src/Di.php b/src/Di.php index b757897e..7509338f 100644 --- a/src/Di.php +++ b/src/Di.php @@ -13,6 +13,12 @@ use Closure; use ReflectionClass; +/** + * Dependency injector that can generate instances using class definitions and configured instance parameters + * + * @category Zend + * @package Zend_Di + */ class Di implements DependencyInjectionInterface { /** @@ -47,9 +53,9 @@ class Di implements DependencyInjectionInterface /** * Constructor * - * @param null|DefinitionList $definitions + * @param null|DefinitionList $definitions * @param null|InstanceManager $instanceManager - * @param null|Configuration $config + * @param null|Configuration $config */ public function __construct(DefinitionList $definitions = null, InstanceManager $instanceManager = null, Configuration $config = null) { @@ -64,7 +70,7 @@ public function __construct(DefinitionList $definitions = null, InstanceManager /** * Provide a configuration object to configure this instance * - * @param Configuration $config + * @param Configuration $config * @return void */ public function configure(Configuration $config) @@ -73,12 +79,13 @@ public function configure(Configuration $config) } /** - * @param DefinitionList $definition - * @return Di + * @param DefinitionList $definitions + * @return self */ public function setDefinitionList(DefinitionList $definitions) { $this->definitions = $definitions; + return $this; } @@ -93,12 +100,13 @@ public function definitions() /** * Set the instance manager * - * @param InstanceManager $instanceManager + * @param InstanceManager $instanceManager * @return Di */ public function setInstanceManager(InstanceManager $instanceManager) { $this->instanceManager = $instanceManager; + return $this; } @@ -111,7 +119,6 @@ public function instanceManager() return $this->instanceManager; } - /** * Lazy-load a class * @@ -119,8 +126,8 @@ public function instanceManager() * 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 + * @param string $name Class name or service alias + * @param null|array $params Parameters to pass to the constructor * @return object|null */ public function get($name, array $params = array()) @@ -133,16 +140,19 @@ public function get($name, array $params = array()) $fastHash = $im->hasSharedInstanceWithParameters($name, $params, true); if ($fastHash) { 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); } } $instance = $this->newInstance($name, $params); array_pop($this->instanceContext); + return $instance; } @@ -152,9 +162,9 @@ public function get($name, array $params = array()) * 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 bool $isShared + * @param mixed $name Class name or service alias + * @param array $params Parameters to pass to the constructor + * @param bool $isShared * @return object|null * @throws Exception\ClassNotFoundException * @throws Exception\RuntimeException @@ -225,14 +235,15 @@ public function newInstance($name, array $params = array(), $isShared = true) $this->handleInjectDependencies($instance, $injectionMethods, $params, $class, $alias, $name); array_pop($this->instanceContext); + return $instance; } /** * Inject dependencies * - * @param object $instance - * @param array $params + * @param object $instance + * @param array $params * @return void */ public function injectDependencies($instance, array $params = array()) @@ -256,6 +267,15 @@ public function injectDependencies($instance, array $params = array()) $this->handleInjectDependencies($instance, $injectionMethods, $params, $class, null, null); } + /** + * @param object $instance + * @param array $injectionMethods + * @param array $params + * @param string|null $instanceClass + * @param string|null$instanceAlias + * @param string $requestedName + * @throws Exception\RuntimeException + */ protected function handleInjectDependencies($instance, $injectionMethods, $params, $instanceClass, $instanceAlias, $requestedName) { // localize dependencies @@ -340,9 +360,9 @@ protected function handleInjectDependencies($instance, $injectionMethods, $param * 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|null $alias + * @param string $class + * @param array $params + * @param string|null $alias * @return object */ protected function createInstanceViaConstructor($class, $params, $alias = null) @@ -364,6 +384,7 @@ protected function createInstanceViaConstructor($class, $params, $alias = null) return new $class($callParameters[0], $callParameters[1], $callParameters[2]); default: $r = new \ReflectionClass($class); + return $r->newInstanceArgs($callParameters); } } @@ -371,9 +392,9 @@ protected function createInstanceViaConstructor($class, $params, $alias = null) /** * Get an object instance from the defined callback * - * @param callback $callback - * @param array $params - * @param string $alias + * @param callback $callback + * @param array $params + * @param string $alias * @return object * @throws Exception\InvalidCallbackException * @throws Exception\RuntimeException @@ -397,6 +418,7 @@ protected function createInstanceViaCallback($callback, $params, $alias) if ($this->definitions->hasMethod($class, $method)) { $callParameters = $this->resolveMethodParameters($class, $method, $params, $alias, true, true); } + return call_user_func_array($callback, $callParameters); } @@ -404,10 +426,12 @@ protected function createInstanceViaCallback($callback, $params, $alias) * This parameter will handle any injection methods and resolution of * dependencies for such methods * - * @param object $object - * @param string $method - * @param array $params - * @param string $alias + * @param object $instance + * @param string $method + * @param array $params + * @param string $alias + * @param bool $methodIsRequired + * @param string|null $methodClass * @return bool */ protected function resolveAndCallInjectionMethodForInstance($instance, $method, $params, $alias, $methodIsRequired, $methodClass = null) @@ -419,21 +443,25 @@ protected function resolveAndCallInjectionMethodForInstance($instance, $method, } if ($callParameters !== array_fill(0, count($callParameters), null)) { call_user_func_array(array($instance, $method), $callParameters); + return true; } + return false; } /** * Resolve parameters referencing other services * - * @param string $class - * @param string $method - * @param array $callTimeUserParams - * @param bool $isInstantiator - * @param string $alias - * @return array + * @param string $class + * @param string $method + * @param array $callTimeUserParams + * @param string $alias + * @param bool $methodIsRequired + * @param bool $isInstantiator + * @throws Exception\MissingPropertyException * @throws Exception\CircularDependencyException + * @return array */ protected function resolveMethodParameters($class, $method, array $callTimeUserParams, $alias, $methodIsRequired, $isInstantiator = false) { @@ -561,6 +589,7 @@ protected function resolveMethodParameters($class, $method, array $callTimeUserP } elseif (is_object($iConfigCurValue) && $iConfigCurValue instanceof Closure && $type !== 'Closure') { + /* @var $iConfigCurValue Closure */ $computedParams['value'][$fqParamPos] = $iConfigCurValue(); } else { $computedParams['value'][$fqParamPos] = $iConfigCurValue; @@ -674,7 +703,7 @@ protected function resolveMethodParameters($class, $method, array $callTimeUserP * @internal this method is used by the ServiceLocator\DependencyInjectorProxy class to interact with instances * and is a hack to be used internally until a major refactor does not split the `resolveMethodParameters`. Do not * rely on its functionality. - * @param Object $instance + * @param Object $instance * @return string */ protected function getClass($instance) @@ -689,20 +718,22 @@ protected function getClass($instance) * @see https://github.com/zendframework/zf2/pull/1807 * * @param string $className - * @param string $type + * @param $type + * @return bool */ protected static function isSubclassOf($className, $type) { - if (version_compare(PHP_VERSION, '5.3.7', '>=')) { - return is_subclass_of($className, $type); - } if (is_subclass_of($className, $type)) { return true; } + if (version_compare(PHP_VERSION, '5.3.7', '>=')) { + return false; + } if (!interface_exists($type)) { return false; } $r = new ReflectionClass($className); + return $r->implementsInterface($type); } } diff --git a/src/Display/Console.php b/src/Display/Console.php index 8426b551..6e587945 100644 --- a/src/Display/Console.php +++ b/src/Display/Console.php @@ -12,6 +12,12 @@ use Zend\Di\Di; +/** + * Exporter for class definitions + * + * @category Zend + * @package Zend_Di + */ class Console { @@ -21,15 +27,15 @@ class Console protected $di = null; /** - * @var array + * @var string[] */ protected $runtimeClasses = array(); /** * Export * - * @param Di $di - * @param array $runtimeClasses + * @param Di $di + * @param array $runtimeClasses * @return void */ public static function export(Di $di, array $runtimeClasses = array()) @@ -49,6 +55,9 @@ public function __construct(Di $di = null) $this->di = ($di) ?: new Di; } + /** + * @param string[] $runtimeClasses + */ public function addRuntimeClasses(array $runtimeClasses) { foreach ($runtimeClasses as $runtimeClass) { @@ -56,12 +65,14 @@ public function addRuntimeClasses(array $runtimeClasses) } } + /** + * @param string $runtimeClass + */ public function addRuntimeClass($runtimeClass) { $this->runtimeClasses[] = $runtimeClass; } - public function render() { @@ -89,7 +100,6 @@ public function render() $this->renderClassDefinition($definition, $runtimeClass); } - echo PHP_EOL . 'Instance Configuration Info:' . PHP_EOL; echo PHP_EOL . ' Aliases:' . PHP_EOL; @@ -108,7 +118,7 @@ public function render() } echo PHP_EOL . ' Configurations:' . PHP_EOL; - + foreach ($configuredTypes as $type) { $info = $this->di->instanceManager()->getConfiguration($type); echo ' ' . $type . PHP_EOL; @@ -130,6 +140,9 @@ public function render() } + /** + * @param object $definition + */ protected function renderDefinition($definition) { echo ' Definition Type: ' . get_class($definition) . PHP_EOL; @@ -147,6 +160,10 @@ protected function renderDefinition($definition) } } + /** + * @param \Zend\Di\Definition\DefinitionInterface $definition + * @param string $class + */ protected function renderClassDefinition($definition, $class) { echo PHP_EOL . ' Parameters For Class: ' . $class . PHP_EOL; diff --git a/src/Exception/CircularDependencyException.php b/src/Exception/CircularDependencyException.php index ae6aad68..c9bb0df5 100644 --- a/src/Exception/CircularDependencyException.php +++ b/src/Exception/CircularDependencyException.php @@ -12,6 +12,10 @@ use DomainException; +/** + * @category Zend + * @package Zend_Di + */ class CircularDependencyException extends DomainException implements ExceptionInterface { } diff --git a/src/Exception/ClassNotFoundException.php b/src/Exception/ClassNotFoundException.php index 35fed6e0..a88a133d 100644 --- a/src/Exception/ClassNotFoundException.php +++ b/src/Exception/ClassNotFoundException.php @@ -12,6 +12,10 @@ use DomainException; +/** + * @category Zend + * @package Zend_Di + */ class ClassNotFoundException extends DomainException implements ExceptionInterface { } diff --git a/src/Exception/ExceptionInterface.php b/src/Exception/ExceptionInterface.php index 57aa176f..e3f185c3 100644 --- a/src/Exception/ExceptionInterface.php +++ b/src/Exception/ExceptionInterface.php @@ -10,6 +10,10 @@ namespace Zend\Di\Exception; +/** + * @category Zend + * @package Zend_Di + */ interface ExceptionInterface { } diff --git a/src/Exception/InvalidArgumentException.php b/src/Exception/InvalidArgumentException.php index 9095b1e1..6f097a8c 100644 --- a/src/Exception/InvalidArgumentException.php +++ b/src/Exception/InvalidArgumentException.php @@ -10,8 +10,10 @@ namespace Zend\Di\Exception; -class InvalidArgumentException - extends \InvalidArgumentException - implements ExceptionInterface +/** + * @category Zend + * @package Zend_Di + */ +class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface { } diff --git a/src/Exception/InvalidCallbackException.php b/src/Exception/InvalidCallbackException.php index 2d9ecf63..3f1b49a0 100644 --- a/src/Exception/InvalidCallbackException.php +++ b/src/Exception/InvalidCallbackException.php @@ -10,6 +10,12 @@ namespace Zend\Di\Exception; +/** + * Exception to be thrown when an invalid php callback is provided + * + * @category Zend + * @package Zend_Di + */ class InvalidCallbackException extends InvalidArgumentException { } diff --git a/src/Exception/InvalidParamNameException.php b/src/Exception/InvalidParamNameException.php index 57b630dc..47e42452 100644 --- a/src/Exception/InvalidParamNameException.php +++ b/src/Exception/InvalidParamNameException.php @@ -10,6 +10,10 @@ namespace Zend\Di\Exception; +/** + * @category Zend + * @package Zend_Di + */ class InvalidParamNameException extends InvalidArgumentException { } diff --git a/src/Exception/InvalidPositionException.php b/src/Exception/InvalidPositionException.php index 97ae99ce..3ac44b64 100644 --- a/src/Exception/InvalidPositionException.php +++ b/src/Exception/InvalidPositionException.php @@ -10,6 +10,10 @@ namespace Zend\Di\Exception; +/** + * @category Zend + * @package Zend_Di + */ class InvalidPositionException extends InvalidArgumentException { } diff --git a/src/Exception/MissingPropertyException.php b/src/Exception/MissingPropertyException.php index 8fa191fd..29a7f6fb 100644 --- a/src/Exception/MissingPropertyException.php +++ b/src/Exception/MissingPropertyException.php @@ -12,6 +12,10 @@ use DomainException; +/** + * @category Zend + * @package Zend_Di + */ class MissingPropertyException extends DomainException implements ExceptionInterface { } diff --git a/src/Exception/RuntimeException.php b/src/Exception/RuntimeException.php index 58fd9904..7fb086ca 100644 --- a/src/Exception/RuntimeException.php +++ b/src/Exception/RuntimeException.php @@ -10,6 +10,10 @@ namespace Zend\Di\Exception; +/** + * @category Zend + * @package Zend_Di + */ class RuntimeException extends \RuntimeException implements ExceptionInterface { } diff --git a/src/Exception/UndefinedReferenceException.php b/src/Exception/UndefinedReferenceException.php index 2916dfa0..782d7b44 100644 --- a/src/Exception/UndefinedReferenceException.php +++ b/src/Exception/UndefinedReferenceException.php @@ -12,6 +12,10 @@ use DomainException; +/** + * @category Zend + * @package Zend_Di + */ class UndefinedReferenceException extends DomainException implements ExceptionInterface { } diff --git a/src/InstanceManager.php b/src/InstanceManager.php index b23b8da9..a94b7831 100644 --- a/src/InstanceManager.php +++ b/src/InstanceManager.php @@ -10,6 +10,12 @@ namespace Zend\Di; +/** + * Registry of instantiated objects, their names and the parameters used to build them + * + * @category Zend + * @package Zend_Di + */ class InstanceManager /* implements InstanceManagerInterface */ { /** @@ -23,19 +29,19 @@ class InstanceManager /* implements InstanceManagerInterface */ * @var array */ protected $sharedInstancesWithParams = array('hashShort' => array(), 'hashLong' => array()); - + /** * Array of class aliases * @var array key: alias, value: class */ protected $aliases = array(); - + /** * The template to use for housing configuration information - * @var array + * @var array */ protected $configurationTemplate = array( - /** + /** * alias|class => alias|class * interface|abstract => alias|class|object * name => value @@ -56,22 +62,23 @@ class InstanceManager /* implements InstanceManagerInterface */ * @var array */ protected $configurations = array(); - + /** * An array of globally preferred implementations for interfaces/abstracts * @var array */ protected $typePreferences = array(); - + /** * Does this instance manager have this shared instance + * @param string $classOrAlias * @return bool */ public function hasSharedInstance($classOrAlias) { return isset($this->sharedInstances[$classOrAlias]); } - + /** * getSharedInstance() */ @@ -83,8 +90,8 @@ public function getSharedInstance($classOrAlias) /** * Add shared instance * - * @param object $instance - * @param string $classOrAlias + * @param object $instance + * @param string $classOrAlias * @throws Exception\InvalidArgumentException */ public function addSharedInstance($instance, $classOrAlias) @@ -99,30 +106,31 @@ public function addSharedInstance($instance, $classOrAlias) /** * hasSharedInstanceWithParameters() * - * @param string $classOrAlias - * @param array $params - * @param bool $returnFashHashLookupKey + * @param string $classOrAlias + * @param array $params + * @param bool $returnFastHashLookupKey * @return bool|string */ - public function hasSharedInstanceWithParameters($classOrAlias, array $params, $returnFashHashLookupKey = false) + public function hasSharedInstanceWithParameters($classOrAlias, array $params, $returnFastHashLookupKey = false) { ksort($params); $hashKey = $this->createHashForKeys($classOrAlias, array_keys($params)); if (isset($this->sharedInstancesWithParams['hashShort'][$hashKey])) { $hashValue = $this->createHashForValues($classOrAlias, $params); if (isset($this->sharedInstancesWithParams['hashLong'][$hashKey . '/' . $hashValue])) { - return ($returnFashHashLookupKey) ? $hashKey . '/' . $hashValue : true; + return ($returnFastHashLookupKey) ? $hashKey . '/' . $hashValue : true; } } + return false; } /** * addSharedInstanceWithParameters() * - * @param object $instance - * @param string $classOrAlias - * @param array $params + * @param object $instance + * @param string $classOrAlias + * @param array $params * @return void */ public function addSharedInstanceWithParameters($instance, $classOrAlias, array $params) @@ -130,8 +138,8 @@ public function addSharedInstanceWithParameters($instance, $classOrAlias, array ksort($params); $hashKey = $this->createHashForKeys($classOrAlias, array_keys($params)); $hashValue = $this->createHashForValues($classOrAlias, $params); - - if (!isset($this->sharedInstancesWithParams[$hashKey]) + + if (!isset($this->sharedInstancesWithParams[$hashKey]) || !is_array($this->sharedInstancesWithParams[$hashKey])) { $this->sharedInstancesWithParams[$hashKey] = array(); } @@ -139,13 +147,21 @@ public function addSharedInstanceWithParameters($instance, $classOrAlias, array $this->sharedInstancesWithParams['hashShort'][$hashKey] = true; $this->sharedInstancesWithParams['hashLong'][$hashKey . '/' . $hashValue] = $instance; } - + + /** + * Retrieves an instance by its name and the parameters stored at its instantiation + * + * @param string $classOrAlias + * @param array $params + * @param bool|null $fastHashFromHasLookup + * @return object|bool false if no instance was found + */ public function getSharedInstanceWithParameters($classOrAlias, array $params, $fastHashFromHasLookup = null) { if ($fastHashFromHasLookup) { return $this->sharedInstancesWithParams['hashLong'][$fastHashFromHasLookup]; } - + ksort($params); $hashKey = $this->createHashForKeys($classOrAlias, array_keys($params)); if (isset($this->sharedInstancesWithParams['hashShort'][$hashKey])) { @@ -154,13 +170,14 @@ public function getSharedInstanceWithParameters($classOrAlias, array $params, $f return $this->sharedInstancesWithParams['hashLong'][$hashKey . '/' . $hashValue]; } } + return false; } /** * Check for an alias * - * @param string $alias + * @param string $alias * @return bool */ public function hasAlias($alias) @@ -177,7 +194,7 @@ public function getAliases() { return $this->aliases; } - + /** * getClassFromAlias() * @@ -200,9 +217,15 @@ public function getClassFromAlias($alias) ); } } + return $alias; } - + + /** + * @param string $alias + * @return string|bool + * @throws Exception\RuntimeException + */ protected function getBaseAlias($alias) { if (!$this->hasAlias($alias)) { @@ -217,19 +240,20 @@ protected function getBaseAlias($alias) if ($r > 100) { throw new Exception\RuntimeException( sprintf('Possible infinite recursion in DI alias! Max recursion of 100 levels reached at alias "%s".', $alias) - ); + ); } } + return $lastAlias; } - + /** * Add alias * * @throws Exception\InvalidArgumentException - * @param string $alias - * @param string $class - * @param array $parameters + * @param string $alias + * @param string $class + * @param array $parameters * @return void */ public function addAlias($alias, $class, array $parameters = array()) @@ -248,7 +272,7 @@ public function addAlias($alias, $class, array $parameters = array()) /** * Check for configuration * - * @param string $aliasOrClass + * @param string $aliasOrClass * @return bool */ public function hasConfiguration($aliasOrClass) @@ -260,9 +284,17 @@ public function hasConfiguration($aliasOrClass) if ($this->configurations[$key] === $this->configurationTemplate) { return false; } + return true; } - + + /** + * Sets configuration for a single alias/class + * + * @param string $aliasOrClass + * @param array $configuration + * @param bool $append + */ public function setConfiguration($aliasOrClass, array $configuration, $append = false) { $key = ($this->hasAlias($aliasOrClass)) ? 'alias:' . $this->getBaseAlias($aliasOrClass) : $aliasOrClass; @@ -290,38 +322,43 @@ public function getClasses() if (strpos($name, 'alias') === 0) continue; $classes[] = $name; } + return $classes; } + /** + * @param string $aliasOrClass + * @return array + */ public function getConfiguration($aliasOrClass) { $key = ($this->hasAlias($aliasOrClass)) ? 'alias:' . $this->getBaseAlias($aliasOrClass) : $aliasOrClass; if (isset($this->configurations[$key])) { - return $this->configurations[$key]; + return $this->configurations[$key]; } else { return $this->configurationTemplate; } } - + /** * setParameters() is a convenience method for: * setConfiguration($type, array('parameters' => array(...)), true); - * - * @param string $type Alias or Class - * @param array $parameters Multi-dim array of parameters and their values + * + * @param string $aliasOrClass Alias or Class + * @param array $parameters Multi-dim array of parameters and their values * @return void */ public function setParameters($aliasOrClass, array $parameters) { $this->setConfiguration($aliasOrClass, array('parameters' => $parameters), true); } - + /** * setInjections() is a convenience method for: * setConfiguration($type, array('injections' => array(...)), true); - * - * @param string $type Alias or Class - * @param array $methods Multi-dim array of methods and their parameters + * + * @param string $aliasOrClass Alias or Class + * @param array $injections Multi-dim array of methods and their parameters * @return void */ public function setInjections($aliasOrClass, array $injections) @@ -332,8 +369,8 @@ public function setInjections($aliasOrClass, array $injections) /** * Set shared * - * @param string $aliasOrClass - * @param bool $isShared + * @param string $aliasOrClass + * @param bool $isShared * @return void */ public function setShared($aliasOrClass, $isShared) @@ -344,20 +381,21 @@ public function setShared($aliasOrClass, $isShared) /** * Check for type preferences * - * @param string $interfaceOrAbstract + * @param string $interfaceOrAbstract * @return bool */ public function hasTypePreferences($interfaceOrAbstract) { $key = ($this->hasAlias($interfaceOrAbstract)) ? 'alias:' . $interfaceOrAbstract : $interfaceOrAbstract; + return (isset($this->typePreferences[$key]) && $this->typePreferences[$key]); } /** * Set type preference * - * @param string $interfaceOrAbstract - * @param array $preferredImplementations + * @param string $interfaceOrAbstract + * @param array $preferredImplementations * @return InstanceManager */ public function setTypePreference($interfaceOrAbstract, array $preferredImplementations) @@ -366,13 +404,14 @@ public function setTypePreference($interfaceOrAbstract, array $preferredImplemen foreach ($preferredImplementations as $preferredImplementation) { $this->addTypePreference($key, $preferredImplementation); } + return $this; } /** * Get type preferences * - * @param string $interfaceOrAbstract + * @param string $interfaceOrAbstract * @return array */ public function getTypePreferences($interfaceOrAbstract) @@ -381,13 +420,14 @@ public function getTypePreferences($interfaceOrAbstract) if (isset($this->typePreferences[$key])) { return $this->typePreferences[$key]; } + return array(); } /** * Unset type preferences * - * @param string $interfaceOrAbstract + * @param string $interfaceOrAbstract * @return void */ public function unsetTypePreferences($interfaceOrAbstract) @@ -396,6 +436,14 @@ public function unsetTypePreferences($interfaceOrAbstract) unset($this->typePreferences[$key]); } + /** + * Adds a type preference. A type preference is a redirection to a preferred alias or type when an abstract type + * $interfaceOrAbstract is requested + * + * @param string $interfaceOrAbstract + * @param string $preferredImplementation + * @return self + */ public function addTypePreference($interfaceOrAbstract, $preferredImplementation) { $key = ($this->hasAlias($interfaceOrAbstract)) ? 'alias:' . $interfaceOrAbstract : $interfaceOrAbstract; @@ -403,9 +451,17 @@ public function addTypePreference($interfaceOrAbstract, $preferredImplementation $this->typePreferences[$key] = array(); } $this->typePreferences[$key][] = $preferredImplementation; + return $this; } + /** + * Removes a previously set type preference + * + * @param string $interfaceOrAbstract + * @param string $preferredType + * @return bool|self + */ public function removeTypePreference($interfaceOrAbstract, $preferredType) { $key = ($this->hasAlias($interfaceOrAbstract)) ? 'alias:' . $interfaceOrAbstract : $interfaceOrAbstract; @@ -413,15 +469,25 @@ public function removeTypePreference($interfaceOrAbstract, $preferredType) return false; } unset($this->typePreferences[$key][array_search($key, $this->typePreferences)]); + return $this; } - + /** + * @param string $classOrAlias + * @param string[] $paramKeys + * @return string + */ protected function createHashForKeys($classOrAlias, $paramKeys) { return $classOrAlias . ':' . implode('|', $paramKeys); } - + + /** + * @param string $classOrAlias + * @param array $paramValues + * @return string + */ protected function createHashForValues($classOrAlias, $paramValues) { $hashValue = ''; @@ -445,6 +511,7 @@ protected function createHashForValues($classOrAlias, $paramValues) break; } } + return $hashValue; } } diff --git a/src/LocatorInterface.php b/src/LocatorInterface.php index de8b1742..88ee6fc5 100644 --- a/src/LocatorInterface.php +++ b/src/LocatorInterface.php @@ -10,13 +10,17 @@ namespace Zend\Di; +/** + * @category Zend + * @package Zend_Di + */ interface LocatorInterface { /** * Retrieve a class instance - * - * @param string $name Class name or service name - * @param null|array $params Parameters to be used when instantiating a new instance of $name + * + * @param string $name Class name or service name + * @param null|array $params Parameters to be used when instantiating a new instance of $name * @return object|null */ public function get($name, array $params = array()); diff --git a/src/ServiceLocator.php b/src/ServiceLocator.php index 3f33b5ac..2d94527b 100644 --- a/src/ServiceLocator.php +++ b/src/ServiceLocator.php @@ -12,12 +12,18 @@ use Closure; +/** + * Simple service locator implementation capable of using closures to generate instances + * + * @category Zend + * @package Zend_Di + */ class ServiceLocator implements ServiceLocatorInterface { /** * Map of service names to methods * - * As an example, you might define a getter method "getFoo", and map it to + * As an example, you might define a getter method "getFoo", and map it to * the service name "foo": * * @@ -26,49 +32,46 @@ class ServiceLocator implements ServiceLocatorInterface * * When encountered, the return value of that method will be used. * - * Methods mapped in this way may expect a single, array argument, the + * Methods mapped in this way may expect a single, array argument, the * $params passed to {@link get()}, if any. - * + * * @var array */ protected $map = array(); /** * Registered services and cached values - * + * * @var array */ protected $services = array(); /** - * Register a service with the locator - * - * @param string $name - * @param mixed $service - * @return ServiceLocator + * {@inheritDoc} */ public function set($name, $service) { $this->services[$name] = $service; + return $this; } /** * Retrieve a registered service * - * Tests first if a value is registered for the service, and, if so, + * Tests first if a value is registered for the service, and, if so, * returns it. * * If the value returned is a non-object callback or closure, the return - * value is retrieved, stored, and returned. Parameters passed to the method + * value is retrieved, stored, and returned. Parameters passed to the method * are passed to the callback, but only on the first retrieval. * * If the service requested matches a method in the method map, the return * value of that method is returned. Parameters are passed to the matching * method. - * - * @param string $name - * @param array $params + * + * @param string $name + * @param array $params * @return mixed */ public function get($name, array $params = array()) @@ -78,6 +81,7 @@ public function get($name, array $params = array()) return null; } $method = $this->map[$name]; + return $this->$method($params); } diff --git a/src/ServiceLocator/DependencyInjectorProxy.php b/src/ServiceLocator/DependencyInjectorProxy.php index 3582fbc1..c6056170 100644 --- a/src/ServiceLocator/DependencyInjectorProxy.php +++ b/src/ServiceLocator/DependencyInjectorProxy.php @@ -14,7 +14,11 @@ use Zend\Di\Exception; /** - * Proxy used to analyze how instances are created by a given Di. + * Proxy used to analyze how instances are created by a given Di. Overrides Zend\Di\Di to produce artifacts that + * represent the process used to instantiate a particular instance + * + * @category Zend + * @package Zend_Di */ class DependencyInjectorProxy extends Di { @@ -34,18 +38,17 @@ public function __construct(Di $di) } /** - * Override, as we want it to use the functionality defined in the proxy - * - * @param string $name - * @param array $params + * {@inheritDoc} * @return GeneratorInstance */ public function get($name, array $params = array()) { return parent::get($name, $params); } + /** * {@inheritDoc} + * @return GeneratorInstance */ public function newInstance($name, array $params = array(), $isShared = true) { @@ -66,13 +69,7 @@ public function newInstance($name, array $params = array(), $isShared = true) } /** - * Override createInstanceViaConstructor method from injector - * - * Returns code generation artifacts. - * - * @param string $class - * @param null|array $params - * @param null|string $alias + * {@inheritDoc} * @return GeneratorInstance */ public function createInstanceViaConstructor($class, $params, $alias = null) @@ -90,13 +87,9 @@ public function createInstanceViaConstructor($class, $params, $alias = null) } /** - * Override instance creation via callback - * - * @param callback $callback - * @param null|array $params - * @param null|string $alias + * {@inheritDoc} + * @throws \Zend\Di\Exception\InvalidCallbackException * @return GeneratorInstance - * @throws Exception\InvalidCallbackException */ public function createInstanceViaCallback($callback, $params, $alias) { @@ -156,6 +149,7 @@ protected function resolveAndCallInjectionMethodForInstance($instance, $method, 'method' => $method, 'params' => $callParameters, )); + return true; } @@ -169,6 +163,7 @@ protected function getClass($instance) { if ($instance instanceof GeneratorInstance) { /* @var $instance GeneratorInstance */ + return $instance->getClass(); } diff --git a/src/ServiceLocator/Generator.php b/src/ServiceLocator/Generator.php index 2daf0350..5e088987 100644 --- a/src/ServiceLocator/Generator.php +++ b/src/ServiceLocator/Generator.php @@ -18,7 +18,11 @@ use Zend\Di\Exception; /** - * @todo refactor to use new Definition interface + * Generator that creates the body of a service locator that can emulate the logic of the given Zend\Di\Di instance + * without class definitions + * + * @category Zend + * @package Zend_Di */ class Generator { @@ -44,36 +48,38 @@ public function __construct(Di $injector) /** * Set the class name for the generated service locator container * - * @param string $name + * @param string $name * @return Generator */ public function setContainerClass($name) { $this->containerClass = $name; + return $this; } /** * Set the namespace to use for the generated class file * - * @param string $namespace + * @param string $namespace * @return Generator */ public function setNamespace($namespace) { $this->namespace = $namespace; + return $this; } /** - * Construct, configure, and return a PHP classfile code generation object + * Construct, configure, and return a PHP class file code generation object * * Creates a Zend\CodeGenerator\Php\PhpFile object that has * created the specified class and service locator methods. * - * @param null|string $filename + * @param null|string $filename + * @throws \Zend\Di\Exception\RuntimeException * @return FileGenerator - * @throws Exception\RuntimeException */ public function getCodeGenerator($filename = null) { @@ -300,14 +306,15 @@ protected function reduceAliases(array $aliasList) } $reduced[$service][] = $alias; } + return $reduced; } /** * Create a PhpMethod code generation object named after a given alias * - * @param string $alias - * @param string $class Class to which alias refers + * @param string $alias + * @param string $class Class to which alias refers * @return MethodGenerator */ protected function getCodeGenMethodFromAlias($alias, $class) @@ -316,6 +323,7 @@ protected function getCodeGenMethodFromAlias($alias, $class) $method = new MethodGenerator(); $method->setName($alias); $method->setBody(sprintf('return $this->get(\'%s\');', $class)); + return $method; } @@ -329,6 +337,7 @@ protected function normalizeAlias($alias) { $normalized = preg_replace('/[^a-zA-Z0-9]/', ' ', $alias); $normalized = 'get' . str_replace(' ', '', ucwords($normalized)); + return $normalized; } } diff --git a/src/ServiceLocator/GeneratorInstance.php b/src/ServiceLocator/GeneratorInstance.php index 997b8085..ef5e64ca 100644 --- a/src/ServiceLocator/GeneratorInstance.php +++ b/src/ServiceLocator/GeneratorInstance.php @@ -12,6 +12,9 @@ /** * Container for methods and parameters used by by Di to create a particular instance + * + * @category Zend + * @package Zend_Di */ class GeneratorInstance { @@ -95,24 +98,26 @@ public function getAlias() * In the case of an instance created via a callback, we need to set the * class name after creating the generator instance. * - * @param string $class + * @param string $class * @return GeneratorInstance */ public function setClass($class) { $this->class = $class; + return $this; } /** * Set instance alias * - * @param string $alias + * @param string $alias * @return GeneratorInstance */ public function setAlias($alias) { $this->alias = $alias; + return $this; } @@ -140,12 +145,13 @@ public function getParams() /** * Set methods * - * @param array $methods + * @param array $methods * @return GeneratorInstance */ public function setMethods(array $methods) { $this->methods = $methods; + return $this; } @@ -158,6 +164,7 @@ public function setMethods(array $methods) public function addMethod($method) { $this->methods[] = $method; + return $this; } diff --git a/src/ServiceLocatorInterface.php b/src/ServiceLocatorInterface.php index fbf6d0c8..283c7ba9 100644 --- a/src/ServiceLocatorInterface.php +++ b/src/ServiceLocatorInterface.php @@ -10,7 +10,19 @@ namespace Zend\Di; +/** + * @category Zend + * @package Zend_Di + */ interface ServiceLocatorInterface extends LocatorInterface { + /** + * Register a service with the locator + * + * @abstract + * @param string $name + * @param mixed $service + * @return ServiceLocatorInterface + */ public function set($name, $service); } diff --git a/test/ConfigurationTest.php b/test/ConfigurationTest.php index 1d1ce5cd..b0f136fe 100644 --- a/test/ConfigurationTest.php +++ b/test/ConfigurationTest.php @@ -25,31 +25,31 @@ public function testConfigurationCanConfigureInstanceManagerWithIniFile() $di->configure($config); $im = $di->instanceManager(); - + $this->assertTrue($im->hasAlias('my-repository')); $this->assertEquals('My\RepositoryA', $im->getClassFromAlias('my-repository')); - + $this->assertTrue($im->hasAlias('my-mapper')); $this->assertEquals('My\Mapper', $im->getClassFromAlias('my-mapper')); - + $this->assertTrue($im->hasAlias('my-dbAdapter')); $this->assertEquals('My\DbAdapter', $im->getClassFromAlias('my-dbAdapter')); - + $this->assertTrue($im->hasTypePreferences('my-repository')); $this->assertContains('my-mapper', $im->getTypePreferences('my-repository')); - + $this->assertTrue($im->hasTypePreferences('my-mapper')); $this->assertContains('my-dbAdapter', $im->getTypePreferences('my-mapper')); $this->assertTrue($im->hasConfiguration('My\DbAdapter')); $expected = array('parameters' => array('username' => 'readonly', 'password' => 'mypassword'), 'injections' => array(), 'shared' => true); $this->assertEquals($expected, $im->getConfiguration('My\DbAdapter')); - + $this->assertTrue($im->hasConfiguration('my-dbAdapter')); $expected = array('parameters' => array('username' => 'readwrite'), 'injections' => array(), 'shared' => true); $this->assertEquals($expected, $im->getConfiguration('my-dbAdapter')); } - + public function testConfigurationCanConfigureBuilderDefinitionFromIni() { $this->markTestIncomplete('Builder not updated to new DI yet'); @@ -57,28 +57,28 @@ public function testConfigurationCanConfigureBuilderDefinitionFromIni() $config = new Configuration($ini->di); $di = new Di($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') ); - + } public function testConfigurationCanConfigureRuntimeDefinitionDefaultFromIni() diff --git a/test/Definition/ArrayDefinitionTest.php b/test/Definition/ArrayDefinitionTest.php index 3e32bc43..be5528d2 100644 --- a/test/Definition/ArrayDefinitionTest.php +++ b/test/Definition/ArrayDefinitionTest.php @@ -15,17 +15,17 @@ class ArrayDefinitionTest extends TestCase { - + /** * @var ArrayDefinition */ protected $definition = null; - + public function setup() { $this->definition = new ArrayDefinition(include __DIR__ . '/../_files/definition-array.php'); } - + public function testArrayDefinitionHasClasses() { $this->assertTrue($this->definition->hasClass('My\DbAdapter')); @@ -35,7 +35,7 @@ public function testArrayDefinitionHasClasses() $this->assertTrue($this->definition->hasClass('My\RepositoryB')); $this->assertFalse($this->definition->hasClass('My\Foo')); } - + public function testArrayDefinitionCanGetClassses() { $list = array( @@ -45,38 +45,38 @@ public function testArrayDefinitionCanGetClassses() 'My\RepositoryA', 'My\RepositoryB' ); - + $classes = $this->definition->getClasses(); - + foreach ($list as $class) { $this->assertContains($class, $classes); } - + } - + public function testArrayDefinitionCanGetClassSupertypes() { $this->assertEquals(array(), $this->definition->getClassSupertypes('My\EntityA')); $this->assertContains('My\RepositoryA', $this->definition->getClassSupertypes('My\RepositoryB')); } - - + + public function testArrayDefinitionCanGetInstantiator() { $this->assertEquals('__construct', $this->definition->getInstantiator('My\RepositoryA')); $this->assertNull($this->definition->getInstantiator('My\Foo')); } - + public function testArrayDefinitionHasInjectionMethods() { $this->markTestIncomplete(); } - + public function testArrayDefinitionHasInjectionMethod() { $this->markTestIncomplete(); } - + public function testArrayDefinitionGetInjectionMethods() { $this->markTestIncomplete(); @@ -87,6 +87,6 @@ public function testArrayDefinitionGetInjectionMethodParameters() $this->markTestIncomplete(); } - - + + } diff --git a/test/Definition/BuilderDefinitionTest.php b/test/Definition/BuilderDefinitionTest.php index afed0866..45acce5f 100644 --- a/test/Definition/BuilderDefinitionTest.php +++ b/test/Definition/BuilderDefinitionTest.php @@ -17,28 +17,28 @@ class BuilderDefinitionTest extends TestCase { - + public function testBuilderImplementsDefinition() { $builder = new BuilderDefinition(); $this->assertInstanceOf('Zend\Di\Definition\DefinitionInterface', $builder); } - + public function testBuilderCanBuildClassWithMethods() { $class = new Builder\PhpClass(); $class->setName('Foo'); $class->addSuperType('Parent'); - + $injectionMethod = new Builder\InjectionMethod(); $injectionMethod->setName('injectBar'); $injectionMethod->addParameter('bar', 'Bar'); - + $class->addInjectionMethod($injectionMethod); - + $definition = new BuilderDefinition(); $definition->addClass($class); - + $this->assertTrue($definition->hasClass('Foo')); $this->assertEquals('__construct', $definition->getInstantiator('Foo')); $this->assertContains('Parent', $definition->getClassSupertypes('Foo')); @@ -50,17 +50,17 @@ public function testBuilderCanBuildClassWithMethods() $definition->getMethodParameters('Foo', 'injectBar') ); } - + public function testBuilderCanBuildFromArray() { $ini = ConfigFactory::fromFile(__DIR__ . '/../_files/sample.ini'); $iniAsArray = $ini['section-b']; $definitionArray = $iniAsArray['di']['definitions'][1]; unset($definitionArray['class']); - + $definition = new BuilderDefinition(); $definition->createClassesFromArray($definitionArray); - + $this->assertTrue($definition->hasClass('My\DbAdapter')); $this->assertEquals('__construct', $definition->getInstantiator('My\DbAdapter')); $this->assertEquals( @@ -70,21 +70,21 @@ public function testBuilderCanBuildFromArray() ), $definition->getMethodParameters('My\DbAdapter', '__construct') ); - + $this->assertTrue($definition->hasClass('My\Mapper')); $this->assertEquals('__construct', $definition->getInstantiator('My\Mapper')); $this->assertEquals( array('My\Mapper::__construct:0' => array('dbAdapter', 'My\DbAdapter', true)), $definition->getMethodParameters('My\Mapper', '__construct') ); - + $this->assertTrue($definition->hasClass('My\Repository')); $this->assertEquals('__construct', $definition->getInstantiator('My\Repository')); $this->assertEquals( array('My\Repository::__construct:0' => array('mapper', 'My\Mapper', true)), $definition->getMethodParameters('My\Repository', '__construct') ); - + } public function testCanCreateClassFromFluentInterface() @@ -94,7 +94,7 @@ public function testCanCreateClassFromFluentInterface() $this->assertTrue($builder->hasClass('Foo')); } - + public function testCanCreateInjectionMethodsAndPopulateFromFluentInterface() { $builder = new BuilderDefinition(); diff --git a/test/Definition/CompilerDefinitionTest.php b/test/Definition/CompilerDefinitionTest.php index c3a7a179..27a18833 100644 --- a/test/Definition/CompilerDefinitionTest.php +++ b/test/Definition/CompilerDefinitionTest.php @@ -24,7 +24,7 @@ public function testCompilerCompilesAgainstConstructorInjectionAssets() $definition->compile(); $this->assertTrue($definition->hasClass('ZendTest\Di\TestAsset\CompilerClasses\A')); - + $assertClasses = array( 'ZendTest\Di\TestAsset\CompilerClasses\A', 'ZendTest\Di\TestAsset\CompilerClasses\B', @@ -38,14 +38,14 @@ public function testCompilerCompilesAgainstConstructorInjectionAssets() // @todo this needs to be resolved, not the short name // $this->assertContains('ZendTest\Di\TestAsset\CompilerClasses\C', $definition->getClassSupertypes('ZendTest\Di\TestAsset\CompilerClasses\D')); - + $this->assertEquals('__construct', $definition->getInstantiator('ZendTest\Di\TestAsset\CompilerClasses\A')); $this->assertTrue($definition->hasMethods('ZendTest\Di\TestAsset\CompilerClasses\C')); - + $this->assertArrayHasKey('setB', $definition->getMethods('ZendTest\Di\TestAsset\CompilerClasses\C')); $this->assertTrue($definition->hasMethod('ZendTest\Di\TestAsset\CompilerClasses\C', 'setB')); - + $this->assertEquals( array('ZendTest\Di\TestAsset\CompilerClasses\C::setB:0' => array('b', 'ZendTest\Di\TestAsset\CompilerClasses\B', true)), $definition->getMethodParameters('ZendTest\Di\TestAsset\CompilerClasses\C', 'setB') @@ -64,7 +64,7 @@ public function testCompilerSupertypes() $this->assertContains('ZendTest\Di\TestAsset\CompilerClasses\C', $definition->getClassSupertypes('ZendTest\Di\TestAsset\CompilerClasses\E')); $this->assertContains('ZendTest\Di\TestAsset\CompilerClasses\D', $definition->getClassSupertypes('ZendTest\Di\TestAsset\CompilerClasses\E')); } - + public function testCompilerDirectoryScannerAndFileScanner() { $definition = new CompilerDefinition; @@ -75,7 +75,7 @@ public function testCompilerDirectoryScannerAndFileScanner() $this->assertContains('ZendTest\Di\TestAsset\CompilerClasses\C', $definition->getClassSupertypes('ZendTest\Di\TestAsset\CompilerClasses\E')); $this->assertContains('ZendTest\Di\TestAsset\CompilerClasses\D', $definition->getClassSupertypes('ZendTest\Di\TestAsset\CompilerClasses\E')); } - + public function testCompilerFileScanner() { $definition = new CompilerDefinition; diff --git a/test/InstanceManagerTest.php b/test/InstanceManagerTest.php index 079450aa..970732b3 100644 --- a/test/InstanceManagerTest.php +++ b/test/InstanceManagerTest.php @@ -15,27 +15,27 @@ class InstanceManagerTest extends TestCase { - + public function testInstanceManagerCanPersistInstances() { $im = new InstanceManager(); $obj = new TestAsset\BasicClass(); $im->addSharedInstance($obj, 'ZendTest\Di\TestAsset\BasicClass'); $this->assertTrue($im->hasSharedInstance('ZendTest\Di\TestAsset\BasicClass')); - $this->assertSame($obj, $im->getSharedInstance('ZendTest\Di\TestAsset\BasicClass')); + $this->assertSame($obj, $im->getSharedInstance('ZendTest\Di\TestAsset\BasicClass')); } - + public function testInstanceManagerCanPersistInstancesWithParameters() { $im = new InstanceManager(); $obj1 = new TestAsset\BasicClass(); $obj2 = new TestAsset\BasicClass(); $obj3 = new TestAsset\BasicClass(); - + $im->addSharedInstance($obj1, 'foo'); $im->addSharedInstanceWithParameters($obj2, 'foo', array('foo' => 'bar')); $im->addSharedInstanceWithParameters($obj3, 'foo', array('foo' => 'baz')); - + $this->assertSame($obj1, $im->getSharedInstance('foo')); $this->assertSame($obj2, $im->getSharedInstanceWithParameters('foo', array('foo' => 'bar'))); $this->assertSame($obj3, $im->getSharedInstanceWithParameters('foo', array('foo' => 'baz'))); @@ -80,7 +80,7 @@ public function testInstanceManagerResolvesRecursiveAliasesForConfiguration() $config['injections'] = array(); $config['shared'] = true; - + $this->assertEquals($config, $im->getConfiguration('foo-alias')); } diff --git a/test/TestAsset/BasicClass.php b/test/TestAsset/BasicClass.php index 93da3441..d9923478 100644 --- a/test/TestAsset/BasicClass.php +++ b/test/TestAsset/BasicClass.php @@ -12,5 +12,5 @@ class BasicClass { - + } diff --git a/test/TestAsset/CircularClasses/A.php b/test/TestAsset/CircularClasses/A.php index 8ad527ca..2bb95291 100644 --- a/test/TestAsset/CircularClasses/A.php +++ b/test/TestAsset/CircularClasses/A.php @@ -14,6 +14,6 @@ class A { public function __construct(B $b) { - + } } diff --git a/test/TestAsset/CircularClasses/C.php b/test/TestAsset/CircularClasses/C.php index 43c1391c..6e1631e5 100644 --- a/test/TestAsset/CircularClasses/C.php +++ b/test/TestAsset/CircularClasses/C.php @@ -14,6 +14,6 @@ class C { public function __construct(D $d) { - + } } diff --git a/test/TestAsset/CircularClasses/D.php b/test/TestAsset/CircularClasses/D.php index 6a3974ff..61170047 100644 --- a/test/TestAsset/CircularClasses/D.php +++ b/test/TestAsset/CircularClasses/D.php @@ -14,6 +14,6 @@ class D { public function __construct(E $e) { - + } } diff --git a/test/TestAsset/CircularClasses/E.php b/test/TestAsset/CircularClasses/E.php index 71e5bb00..d0e11917 100644 --- a/test/TestAsset/CircularClasses/E.php +++ b/test/TestAsset/CircularClasses/E.php @@ -14,6 +14,6 @@ class E { public function __construct(C $c) { - + } } diff --git a/test/TestAsset/CircularClasses/X.php b/test/TestAsset/CircularClasses/X.php index 7934b370..ff9c0e67 100644 --- a/test/TestAsset/CircularClasses/X.php +++ b/test/TestAsset/CircularClasses/X.php @@ -14,6 +14,6 @@ class X { public function __construct(X $x) { - + } } diff --git a/test/TestAsset/CompilerClasses/B.php b/test/TestAsset/CompilerClasses/B.php index a96bc5c0..79f74e1f 100644 --- a/test/TestAsset/CompilerClasses/B.php +++ b/test/TestAsset/CompilerClasses/B.php @@ -14,6 +14,6 @@ class B { public function __construct(B $b) { - + } } diff --git a/test/TestAsset/CompilerClasses/C.php b/test/TestAsset/CompilerClasses/C.php index 9afd7a7a..b6b9a680 100644 --- a/test/TestAsset/CompilerClasses/C.php +++ b/test/TestAsset/CompilerClasses/C.php @@ -14,6 +14,6 @@ class C { public function setB(B $b) { - + } } diff --git a/test/TestAsset/InheritanceClasses/A.php b/test/TestAsset/InheritanceClasses/A.php index 418bdf13..e65cd54b 100644 --- a/test/TestAsset/InheritanceClasses/A.php +++ b/test/TestAsset/InheritanceClasses/A.php @@ -13,7 +13,7 @@ class A { public $test; - + public function setTest($test) { $this->test = $test; diff --git a/test/TestAsset/PreferredImplClasses/D.php b/test/TestAsset/PreferredImplClasses/D.php index a9b11e1d..792335e9 100644 --- a/test/TestAsset/PreferredImplClasses/D.php +++ b/test/TestAsset/PreferredImplClasses/D.php @@ -12,5 +12,5 @@ class D extends C { - + } diff --git a/test/_files/config.yml b/test/_files/config.yml index 880ef3ff..8cc57d6f 100644 --- a/test/_files/config.yml +++ b/test/_files/config.yml @@ -27,7 +27,7 @@ testing: setObject: name: setObject params: - - + - __reference: params - class: ZendTest\Di\TestAsset\InspectedClass @@ -39,4 +39,4 @@ testing: params: ZendTest\Di\TestAsset\DummyParams injected: ZendTest\Di\TestAsset\InjectedMethod inspected: ZendTest\Di\TestAsset\InspectedClass - + diff --git a/test/_files/definition-array.php b/test/_files/definition-array.php index 7f724aed..4557e750 100644 --- a/test/_files/definition-array.php +++ b/test/_files/definition-array.php @@ -1,20 +1,20 @@ + 'My\\DbAdapter' => array ( - 'superTypes' => + 'superTypes' => array ( ), 'instantiator' => '__construct', 'methods' => array ( - '__construct' => + '__construct' => array ( 'username' => NULL, 'password' => NULL, ), ), ), - 'My\\EntityA' => + 'My\\EntityA' => array ( 'supertypes' => array ( @@ -24,7 +24,7 @@ array ( ), ), - 'My\\Mapper' => + 'My\\Mapper' => array ( 'supertypes' => array ( @@ -33,27 +33,27 @@ 'instantiator' => '__construct', 'methods' => array ( - 'setDbAdapter' => + 'setDbAdapter' => array ( 'dbAdapter' => 'My\\DbAdapter', ), ), ), - 'My\\RepositoryA' => + 'My\\RepositoryA' => array ( 'superTypes' => array ( ), 'instantiator' => '__construct', - 'injectionMethods' => + 'injectionMethods' => array ( - 'setMapper' => + 'setMapper' => array ( 'mapper' => 'My\\Mapper', ), ), ), - 'My\\RepositoryB' => + 'My\\RepositoryB' => array ( 'superTypes' => array (