/** * @param $entity * @return ArrayObject * @internal param $spec */ protected function getSpecification($entity) { $annotationManager = $this->getAnnotationManager(); $spec = new ArrayObject(); $reflection = new ClassReflection($entity); $annotations = $reflection->getAnnotations($annotationManager); if ($annotations instanceof AnnotationCollection) { $this->configureEntity($annotations, $reflection, $spec); } foreach ($reflection->getProperties() as $property) { $annotations = $property->getAnnotations($annotationManager); if ($annotations instanceof AnnotationCollection) { $this->configureProperty($annotations, $property, $spec); } } return $spec; }
protected function processClass($class) { $strategy = $this->introspectionStrategy; // localize for readability try { $rClass = new Reflection\ClassReflection($class); } catch (\ReflectionException $e) { if (!$this->allowReflectionExceptions) { throw $e; } return; } $className = $rClass->getName(); $matches = null; // used for regex below // setup the key in classes $this->classes[$className] = array('supertypes' => array(), 'instantiator' => null, 'methods' => array(), 'parameters' => array()); $def =& $this->classes[$className]; // localize for brevity // class annotations? if ($strategy->getUseAnnotations() == true) { $annotations = $rClass->getAnnotations($strategy->getAnnotationManager()); if ($annotations instanceof AnnotationCollection && $annotations->hasAnnotation('Zend\\Di\\Definition\\Annotation\\Instantiator')) { // @todo Instnatiator support in annotations } } $rTarget = $rClass; $supertypes = array(); do { $supertypes = array_merge($supertypes, $rTarget->getInterfaceNames()); if (!($rTargetParent = $rTarget->getParentClass())) { break; } $supertypes[] = $rTargetParent->getName(); $rTarget = $rTargetParent; } while (true); $def['supertypes'] = $supertypes; if ($def['instantiator'] == null) { if ($rClass->isInstantiable()) { $def['instantiator'] = '__construct'; } } if ($rClass->hasMethod('__construct')) { $def['methods']['__construct'] = true; // required try { $this->processParams($def, $rClass, $rClass->getMethod('__construct')); } catch (\ReflectionException $e) { if (!$this->allowReflectionExceptions) { throw $e; } return; } } foreach ($rClass->getMethods(Reflection\MethodReflection::IS_PUBLIC) as $rMethod) { $methodName = $rMethod->getName(); if ($rMethod->getName() === '__construct') { continue; } if ($strategy->getUseAnnotations() == true) { $annotations = $rMethod->getAnnotations($strategy->getAnnotationManager()); if ($annotations instanceof AnnotationCollection && $annotations->hasAnnotation('Zend\\Di\\Definition\\Annotation\\Inject')) { $def['methods'][$methodName] = true; $this->processParams($def, $rClass, $rMethod); continue; } } $methodPatterns = $this->introspectionStrategy->getMethodNameInclusionPatterns(); // matches a method injection pattern? foreach ($methodPatterns as $methodInjectorPattern) { preg_match($methodInjectorPattern, $methodName, $matches); if ($matches) { $def['methods'][$methodName] = false; // check ot see if this is required? $this->processParams($def, $rClass, $rMethod); continue 2; } } // method // by annotation // by setter pattern, // by interface } $interfaceInjectorPatterns = $this->introspectionStrategy->getInterfaceInjectionInclusionPatterns(); // matches the interface injection pattern /** @var $rIface \ReflectionClass */ foreach ($rClass->getInterfaces() as $rIface) { foreach ($interfaceInjectorPatterns as $interfaceInjectorPattern) { preg_match($interfaceInjectorPattern, $rIface->getName(), $matches); if ($matches) { foreach ($rIface->getMethods() as $rMethod) { if ($rMethod->getName() === '__construct') { // ctor not allowed in ifaces continue; } $def['methods'][$rMethod->getName()] = true; $this->processParams($def, $rClass, $rMethod); } continue 2; } } } }
/** * @param string $class * @param bool $forceLoad */ protected function processClass($class, $forceLoad = false) { if (!$forceLoad && $this->hasProcessedClass($class)) { return; } $strategy = $this->introspectionStrategy; // localize for readability /** @var $rClass \Zend\Code\Reflection\ClassReflection */ $rClass = new Reflection\ClassReflection($class); $className = $rClass->getName(); $matches = null; // used for regex below // setup the key in classes $this->classes[$className] = array('supertypes' => array(), 'instantiator' => null, 'methods' => array(), 'parameters' => array()); $def =& $this->classes[$className]; // localize for brevity // class annotations? if ($strategy->getUseAnnotations() == true) { $annotations = $rClass->getAnnotations($strategy->getAnnotationManager()); if ($annotations instanceof AnnotationCollection && $annotations->hasAnnotation('Zend\\Di\\Definition\\Annotation\\Instantiator')) { // @todo Instantiator support in annotations } } $rTarget = $rClass; $supertypes = array(); do { $supertypes = array_merge($supertypes, $rTarget->getInterfaceNames()); if (!($rTargetParent = $rTarget->getParentClass())) { break; } $supertypes[] = $rTargetParent->getName(); $rTarget = $rTargetParent; } while (true); $def['supertypes'] = $supertypes; if ($def['instantiator'] == null) { if ($rClass->isInstantiable()) { $def['instantiator'] = '__construct'; } } if ($rClass->hasMethod('__construct')) { $def['methods']['__construct'] = Di::METHOD_IS_CONSTRUCTOR; // required $this->processParams($def, $rClass, $rClass->getMethod('__construct')); } foreach ($rClass->getMethods(Reflection\MethodReflection::IS_PUBLIC) as $rMethod) { $methodName = $rMethod->getName(); if ($rMethod->getName() === '__construct' || $rMethod->isStatic()) { continue; } if ($strategy->getUseAnnotations() == true) { $annotations = $rMethod->getAnnotations($strategy->getAnnotationManager()); if ($annotations instanceof AnnotationCollection && $annotations->hasAnnotation('Zend\\Di\\Definition\\Annotation\\Inject')) { // use '@inject' and search for parameters $def['methods'][$methodName] = Di::METHOD_IS_EAGER; $this->processParams($def, $rClass, $rMethod); continue; } } $methodPatterns = $this->introspectionStrategy->getMethodNameInclusionPatterns(); // matches a method injection pattern? foreach ($methodPatterns as $methodInjectorPattern) { preg_match($methodInjectorPattern, $methodName, $matches); if ($matches) { $def['methods'][$methodName] = Di::METHOD_IS_OPTIONAL; // check ot see if this is required? $this->processParams($def, $rClass, $rMethod); continue 2; } } // method // by annotation // by setter pattern, // by interface } $interfaceInjectorPatterns = $this->introspectionStrategy->getInterfaceInjectionInclusionPatterns(); // matches the interface injection pattern /** @var $rIface \ReflectionClass */ foreach ($rClass->getInterfaces() as $rIface) { foreach ($interfaceInjectorPatterns as $interfaceInjectorPattern) { preg_match($interfaceInjectorPattern, $rIface->getName(), $matches); if ($matches) { foreach ($rIface->getMethods() as $rMethod) { if ($rMethod->getName() === '__construct' || !count($rMethod->getParameters())) { // constructor not allowed in interfaces // Don't call interface methods without a parameter (Some aware interfaces define setters in ZF2) continue; } $def['methods'][$rMethod->getName()] = Di::METHOD_IS_AWARE; $this->processParams($def, $rClass, $rMethod); } continue 2; } } } }
/** * Creates and returns a form specification for use with a factory * * Parses the object provided, and processes annotations for the class and * all properties. Information from annotations is then used to create * specifications for a form, its elements, and its input filter. * * @param string|object $entity Either an instance or a valid class name for an entity * @throws Exception\InvalidArgumentException if $entity is not an object or class name * @return ArrayObject */ public function getFormSpecification($entity) { if (!is_object($entity)) { if (is_string($entity) && !class_exists($entity) || !is_string($entity)) { throw new Exception\InvalidArgumentException(sprintf('%s expects an object or valid class name; received "%s"', __METHOD__, var_export($entity, 1))); } } $this->entity = $entity; $annotationManager = $this->getAnnotationManager(); $formSpec = new ArrayObject(); $filterSpec = new ArrayObject(); $reflection = new ClassReflection($entity); $annotations = $reflection->getAnnotations($annotationManager); if ($annotations instanceof AnnotationCollection) { $this->configureForm($annotations, $reflection, $formSpec, $filterSpec); } foreach ($reflection->getProperties() as $property) { $annotations = $property->getAnnotations($annotationManager); if ($annotations instanceof AnnotationCollection) { $this->configureElement($annotations, $property, $formSpec, $filterSpec); } } if (!isset($formSpec['input_filter'])) { $formSpec['input_filter'] = $filterSpec; } return $formSpec; }
/** * Returns class-level annotations. * * @param ClassReflection $controller * @return AnnotationCollection */ public function getControllerAnnotations(ClassReflection $controller) { $annotations = $controller->getAnnotations($this->annotationManager); if (!$annotations) { $annotations = new AnnotationCollection(); } return $annotations; }