/** * inject a value in the property field of the instance, scan for a setter then a property * @param $instance Object * @param $propertyName String * @param $value Object * @return */ private function injectProperty($instance, $value, BeanDefinition $bean) { $propertyName = $this->name; $setter = 'set' . ucfirst($propertyName); if (method_exists($instance, $setter)) { $instance->{$setter}($value); } else { if (property_exists($instance, $propertyName)) { $instance->{$propertyName} = $value; } else { throw new IocException("cannot find setter ({$setter}) nor property ({$propertyName}) for bean ({$bean->getId()})"); } } }
protected function addBean(\SimpleXMLElement $node, $xmlFile) { $bean = new BeanDefinition(); $id = trim((string) $node['id']); if ($id == '') { throw new IocException("A bean without id has been found! In file ({$xmlFile})"); } $bean->setId($id); $className = trim((string) $node['class']); if ($className == '') { throw new IocException("Bean ({$id}) must have a class attribute in file ({$xmlFile})"); } $bean->setClassName($className); $scope = trim((string) $node['scope']); if ($scope == '') { $scope = BeanDefinition::SCOPE_SINGLETON; } $bean->setScope($scope); foreach ($node->property as $property) { $bean->addProperty($this->doProperty($property, $xmlFile)); } $bean->setInitMethod(trim((string) $node['init-method'])); $lazyInit = trim((string) $node['lazy-init']); if ($lazyInit == 'false') { $bean->setLazyInit(false); } $this->applicationContext->addBeanDefinition($bean); }
/** * * @param String $filename réal path of the php file to scan ex: /home/project/src/org/foo/bar.php * @param String $namespace the name space of this file ex: org\foo * @return void * @throws IocException */ protected function scanFile($filename, $namespace) { if (!is_file($filename)) { \org\equinox\utils\Logger::error("({$filename}) is not a file!"); throw new IocException("({$filename}) is not a file!"); } include_once $filename; $name = basename($filename); $className = $namespace . '\\' . substr($name, 0, strpos($name, '.')); if (class_exists($className)) { $ref = new \ReflectionAnnotatedClass($className); $listAnnotation = $ref->getAllAnnotations(); if (empty($listAnnotation)) { return; // this class is not a Component ! } $bean = new BeanDefinition(); $bean->setClassName($className); foreach ($listAnnotation as $annotation) { if ($annotation instanceof annotations\ClassAnnotation) { $annotation->doBean($bean, $className); } } if ($bean->getId() == null) { throw new IocException("Equinox annotation found but no @Component annotation found (mandatory) in {$className}"); } $listProperties = $ref->getProperties(); foreach ($listProperties as $property) { $this->doProperty($bean, $property); } $listMethod = $ref->getMethods(); foreach ($listMethod as $method) { $this->doMethod($bean, $method); } $this->context->addBeanDefinition($bean); } }
public function addBeanDefinition(BeanDefinition $definition) { if (array_key_exists($definition->getId(), $this->beanDefinitions)) { throw new IocException("Bean ({$definition->getId()}) already defined"); } $this->beanDefinitions[$definition->getId()] = $definition; if (!$definition->isLazyInit()) { $this->beanToInitialize[] = $definition->getId(); } }
/** * Check if the bean have an init method and execute it * @param $instance * @return unknown_type */ private function doInit($instance, BeanDefinition $bean) { $init = $bean->getInitMethod(); if ($init != '') { if (method_exists($instance, $init)) { $instance->{$init}(); } else { throw new IocException("cannot find method ({$init}) defined in (init-method) of bean ({$bean->getId()})"); } } }