private function _inject($name, Annotation $annotation, $class = null, Annotation $named = null) { $ret = false; $required = true; if ($annotation->hasOption('required')) { $required = $annotation->getOptionSinglevalue('required') == 'true'; } if (!$annotation->hasOption('type')) { if ($class === null) { throw new InjectByTypeException($name, 'Unknown', "Missing type= specification"); } } else { $class = $annotation->getOptionSingleValue('type'); } $isArray = strpos(substr($class, -2), "[]") !== false; if ($isArray) { $class = substr($class, 0, -2); $ret = array(); } $candidates = $this->_container->getBeansByClass($class); if (empty($candidates)) { if ($required) { throw new InjectByTypeException($name, $class, "Did not find any candidates for injecting by type"); } else { return array(); } } if (!$isArray && count($candidates) > 1) { $preferredName = null; if ($named !== null) { if (!$named->hasOption('name')) { throw new InjectByTypeException($name, 'Unknown', "@Named needs the name= specification"); } $preferredName = $named->getOptionSingleValue('name'); } if ($preferredName !== null) { if (in_array($preferredName, $candidates)) { $candidates = array($preferredName); } else { throw new InjectByTypeException($name, 'Unknown', "Specified bean name in @Named not found"); } } else { $foundPrimary = false; $beans = $candidates; foreach ($beans as $beanName) { $beanCandidateDef = $this->_container->getBeanDefinition($beanName); if ($beanCandidateDef->isPrimaryCandidate()) { if ($foundPrimary) { throw new InjectByTypeException($name, $class, "Too many (primary) candidates for injecting by type"); } $foundPrimary = true; $candidates = array($beanName); } } } if (count($candidates) > 1) { throw new InjectByTypeException($name, $class, "Too many candidates for injecting by type"); } } if ($isArray) { $propertyValue = array(); foreach ($candidates as $value) { $ret[] = $value; } } else { $ret = array_shift($candidates); } return $ret; }
/** * Returns all possible names for a bean. If none are found in the bean * annotation, the optional $overrideWithName will be chosen. If not, one * will be generated. * * @param AnnotationDefinition $beanAnnotation * @param string $overrideWithName * * @return string[] */ private function _getAllNames(AnnotationDefinition $beanAnnotation, $overrideWithName = false) { if ($beanAnnotation->hasOption('name')) { return $beanAnnotation->getOptionValues('name'); } if ($overrideWithName !== false) { return array($overrideWithName); } return array(BeanDefinition::generateName('Bean')); }