/** * (non-PHPdoc) * @see Ding\Bean\Lifecycle.IAfterDefinitionListener::afterDefinition() */ public function afterDefinition(BeanDefinition $bean) { $class = $bean->getClass(); $annotations = $this->reflectionFactory->getClassAnnotations($class); if ($annotations->contains('initmethod')) { $annotation = $annotations->getSingleAnnotation('initmethod'); if ($annotation->hasOption('method')) { $bean->setInitMethod($annotation->getOptionSingleValue('method')); } } if ($annotations->contains('destroymethod')) { $annotation = $annotations->getSingleAnnotation('destroymethod'); if ($annotation->hasOption('method')) { $bean->setDestroyMethod($annotation->getOptionSingleValue('method')); } } foreach ($this->reflectionFactory->getClass($class)->getMethods() as $method) { $methodName = $method->getName(); $annotations = $this->reflectionFactory->getMethodAnnotations($class, $methodName); if ($annotations->contains('postconstruct')) { $bean->setInitMethod($methodName); break; } if ($annotations->contains('predestroy')) { $bean->setDestroyMethod($methodName); break; } } return $bean; }
/** * (non-PHPdoc) * @see Ding\Bean\Lifecycle.IAfterDefinitionListener::afterDefinition() */ public function afterDefinition(BeanDefinition $bean) { $class = $bean->getClass(); $rClass = $this->reflectionFactory->getClass($class); $annotations = $this->reflectionFactory->getClassAnnotations($class); $props = $bean->getProperties(); foreach ($rClass->getMethods() as $rMethod) { $methodName = $rMethod->getName(); if (strpos($methodName, 'set') !== 0) { continue; } $annotations = $this->reflectionFactory->getMethodAnnotations($class, $methodName); if (!$annotations->contains('required')) { continue; } $propName = lcfirst(substr($methodName, 3)); if (!isset($props[$propName])) { throw new BeanFactoryException('Missing @Required property: ' . $methodName); } } return $bean; }
/** * (non-PHPdoc) * @see Ding\Bean\Lifecycle.IAfterDefinitionListener::afterDefinition() */ public function afterDefinition(BeanDefinition $bean) { $class = $bean->getClass(); $rClass = $this->reflectionFactory->getClass($class); $properties = $bean->getProperties(); foreach ($rClass->getMethods() as $method) { $methodName = $method->getName(); if (strpos($methodName, 'set') !== 0) { continue; } $annotations = $this->reflectionFactory->getMethodAnnotations($class, $methodName); if (!$annotations->contains('resource')) { continue; } $propName = lcfirst(substr($methodName, 3)); $name = $propName; $annotation = $annotations->getSingleAnnotation('resource'); if ($annotation->hasOption('name')) { $name = $annotation->getOptionSingleValue('name'); } $properties[$propName] = new BeanPropertyDefinition($propName, BeanPropertyDefinition::PROPERTY_BEAN, $name); } foreach ($rClass->getProperties() as $property) { $propertyName = $property->getName(); $annotations = $this->reflectionFactory->getPropertyAnnotations($class, $propertyName); if (!$annotations->contains('resource')) { continue; } $annotation = $annotations->getSingleAnnotation('resource'); $name = $propertyName; if ($annotation->hasOption('name')) { $name = $annotation->getOptionSingleValue('name'); } $properties[$propertyName] = new BeanPropertyDefinition($propertyName, BeanPropertyDefinition::PROPERTY_BEAN, $name); } $bean->setProperties($properties); return $bean; }
/** * (non-PHPdoc) * @see Ding\Bean\Lifecycle.IAfterDefinitionListener::afterDefinition() */ public function afterDefinition(BeanDefinition $bean) { $properties = $bean->getProperties(); $class = $bean->getClass(); $rClass = $this->reflectionFactory->getClass($class); foreach ($rClass->getProperties() as $rProperty) { $propertyName = $rProperty->getName(); $annotations = $this->reflectionFactory->getPropertyAnnotations($class, $propertyName); if ($annotations->contains('value')) { $annotation = $annotations->getSingleAnnotation('value'); if ($annotation->hasOption('value')) { $value = $annotation->getOptionSingleValue('value'); $properties[$propertyName] = new BeanPropertyDefinition($propertyName, BeanPropertyDefinition::PROPERTY_SIMPLE, $value); } } } $bean->setProperties($properties); if ($bean->isCreatedWithFactoryBean()) { $factoryMethod = $bean->getFactoryMethod(); $factoryBean = $bean->getFactoryBean(); $def = $this->_container->getBeanDefinition($factoryBean); $annotations = $this->reflectionFactory->getMethodAnnotations($def->getClass(), $factoryMethod); $this->_applyToConstructor($annotations, $bean); } else { if ($bean->isCreatedByConstructor()) { $class = $bean->getClass(); $rClass = $this->reflectionFactory->getClass($bean->getClass()); $rMethod = $rClass->getConstructor(); if ($rMethod) { $annotations = $this->reflectionFactory->getMethodAnnotations($class, $rMethod->getName()); $this->_applyToConstructor($annotations, $bean); } } } return $bean; }
/** * Returns a constructor argument definition. * * @param SimpleXML $simpleXmlArg Argument node. * * @throws BeanFactoryException * @return BeanConstructorArgumentDefinition */ private function _loadConstructorArg($simpleXmlArg) { if (isset($simpleXmlArg->ref)) { $argType = BeanConstructorArgumentDefinition::BEAN_CONSTRUCTOR_BEAN; $argValue = (string) $simpleXmlArg->ref->attributes()->bean; } else { if (isset($simpleXmlArg->bean)) { $argType = BeanConstructorArgumentDefinition::BEAN_CONSTRUCTOR_BEAN; $name = BeanDefinition::generateName('Bean'); $argValue = $name; $simpleXmlArg->bean->addAttribute('id', $name); } else { if (isset($simpleXmlArg->null)) { $argType = BeanConstructorArgumentDefinition::BEAN_CONSTRUCTOR_VALUE; $argValue = null; } else { if (isset($simpleXmlArg->false)) { $argType = BeanConstructorArgumentDefinition::BEAN_CONSTRUCTOR_VALUE; $argValue = false; } else { if (isset($simpleXmlArg->true)) { $argType = BeanConstructorArgumentDefinition::BEAN_CONSTRUCTOR_VALUE; $argValue = true; } else { if (isset($simpleXmlArg->array)) { $argType = BeanConstructorArgumentDefinition::BEAN_CONSTRUCTOR_ARRAY; $argValue = array(); foreach ($simpleXmlArg->array->entry as $arrayEntry) { $key = (string) $arrayEntry->attributes()->key; $argValue[$key] = $this->_loadConstructorArg($arrayEntry); } } else { if (isset($simpleXmlArg->eval)) { $argType = BeanConstructorArgumentDefinition::BEAN_CONSTRUCTOR_CODE; $argValue = (string) $simpleXmlArg->eval; } else { $argType = BeanConstructorArgumentDefinition::BEAN_CONSTRUCTOR_VALUE; $argValue = (string) $simpleXmlArg->value; } } } } } } } if (isset($simpleXmlArg->attributes()->name)) { $argName = (string) $simpleXmlArg->attributes()->name; } else { $argName = false; } return new BeanConstructorArgumentDefinition($argType, $argValue, $argName); }
private function _injectConstructorArguments(BeanDefinition $bean) { if ($bean->isCreatedWithFactoryBean()) { $factoryMethod = $bean->getFactoryMethod(); $factoryBean = $bean->getFactoryBean(); $def = $this->_container->getBeanDefinition($factoryBean); $class = $def->getClass(); $rMethod = $this->_reflectionFactory->getMethod($class, $factoryMethod); $annotations = $this->_reflectionFactory->getMethodAnnotations($class, $factoryMethod); $this->_applyToConstructor($rMethod, $annotations, $bean); } else { if ($bean->isCreatedByConstructor()) { $class = $bean->getClass(); $rClass = $this->_reflectionFactory->getClass($class); $rMethod = $rClass->getConstructor(); if ($rMethod) { $annotations = $this->_reflectionFactory->getMethodAnnotations($class, $rMethod->getName()); $this->_applyToConstructor($rMethod, $annotations, $bean); } } } }
/** * (non-PHPdoc) * @see Ding\Bean.IBeanDefinitionProvider::getBeanDefinition() */ public function getBeanDefinition($name) { if ($name == 'PropertiesHolder') { $bDef = new BeanDefinition('PropertiesHolder'); $bDef->setClass('Ding\\Helpers\\Properties\\PropertiesHelper'); return $bDef; } return null; }
private function _newAspect($aspectBean, $classExpression, $expression, $method, $type) { $pointcutName = BeanDefinition::generateName('PointcutAnnotationAspectDriver'); $pointcutDef = new PointcutDefinition($pointcutName, $expression, $method); $aspectName = BeanDefinition::generateName('AnnotationAspected'); $aspectDef = new AspectDefinition($aspectName, array($pointcutName), $type, $aspectBean, $classExpression); $this->_aspectManager->setPointcut($pointcutDef); return $aspectDef; }
/** * (non-PHPdoc) * @see Ding\Bean.IBeanDefinitionProvider::get() */ public function getBeanDefinition($name) { $bean = null; switch ($name) { case 'dingAnnotationParser': $bean = new BeanDefinition($name); $bean->setClass('\\Ding\\Annotation\\Parser'); break; case 'dingAnnotationsCache': $bean = new BeanDefinition($name); $bean->setClass('\\Ding\\Cache\\Locator\\CacheLocator'); $bean->setFactoryMethod('getAnnotationsCacheInstance'); break; case 'dingBeanCache': $bean = new BeanDefinition($name); $bean->setClass('\\Ding\\Cache\\Locator\\CacheLocator'); $bean->setFactoryMethod('getBeansCacheInstance'); break; case 'dingDefinitionsCache': $bean = new BeanDefinition($name); $bean->setClass('\\Ding\\Cache\\Locator\\CacheLocator'); $bean->setFactoryMethod('getDefinitionsCacheInstance'); break; case 'dingProxyCache': $bean = new BeanDefinition($name); $bean->setClass('\\Ding\\Cache\\Locator\\CacheLocator'); $bean->setFactoryMethod('getProxyCacheInstance'); break; case 'dingAspectCache': $bean = new BeanDefinition($name); $bean->setClass('\\Ding\\Cache\\Locator\\CacheLocator'); $bean->setFactoryMethod('getAspectCacheInstance'); break; case 'dingAspectManager': $bean = new BeanDefinition($name); $bean->setClass('\\Ding\\Aspect\\AspectManager'); $bean->setProperties(array(new BeanPropertyDefinition('cache', BeanPropertyDefinition::PROPERTY_BEAN, 'dingAspectCache'))); break; case 'dingXmlBeanDefinitionProvider': $bean = new BeanDefinition($name); $bean->setClass('\\Ding\\Bean\\Provider\\Xml'); $bean->setArguments(array(new BeanConstructorArgumentDefinition(BeanConstructorArgumentDefinition::BEAN_CONSTRUCTOR_VALUE, $this->options['bdef']['xml']))); break; case 'dingAnnotationBeanDefinitionProvider': $bean = new BeanDefinition($name); $bean->setClass('\\Ding\\Bean\\Provider\\Annotation'); $bean->setArguments(array(new BeanConstructorArgumentDefinition(BeanConstructorArgumentDefinition::BEAN_CONSTRUCTOR_VALUE, $this->options['bdef']['annotation']))); $bean->setInitMethod('init'); $bean->setProperties(array(new BeanPropertyDefinition('cache', BeanPropertyDefinition::PROPERTY_BEAN, 'dingAnnotationsCache'))); break; case 'dingYamlBeanDefinitionProvider': $bean = new BeanDefinition($name); $bean->setClass('\\Ding\\Bean\\Provider\\Yaml'); $bean->setArguments(array(new BeanConstructorArgumentDefinition(BeanConstructorArgumentDefinition::BEAN_CONSTRUCTOR_VALUE, $this->options['bdef']['yaml']))); break; case 'dingAspectCallDispatcher': $bean = new BeanDefinition($name); $bean->setClass('\\Ding\\Aspect\\Interceptor\\DispatcherImpl'); break; case 'dingAnnotationInitDestroyMethodDriver': $bean = new BeanDefinition($name); $bean->setClass('\\Ding\\Bean\\Factory\\Driver\\AnnotationInitDestroyMethodDriver'); break; case 'dingAnnotationValueDriver': $bean = new BeanDefinition($name); $bean->setClass('\\Ding\\Bean\\Factory\\Driver\\AnnotationValueDriver'); break; case 'dingMvcAnnotationDriver': $bean = new BeanDefinition($name); $bean->setClass('\\Ding\\Bean\\Factory\\Driver\\MvcAnnotationDriver'); break; case 'dingAnnotationRequiredDriver': $bean = new BeanDefinition($name); $bean->setClass('\\Ding\\Bean\\Factory\\Driver\\AnnotationRequiredDriver'); break; case 'dingAnnotationDiscovererDriver': $bean = new BeanDefinition($name); $bean->setClass('\\Ding\\Bean\\Factory\\Driver\\AnnotationDiscovererDriver'); $bean->setProperties(array(new BeanPropertyDefinition('cache', BeanPropertyDefinition::PROPERTY_BEAN, 'dingAnnotationsCache'))); $bean->setInitMethod('parse'); $bean->setArguments(array(new BeanConstructorArgumentDefinition(BeanConstructorArgumentDefinition::BEAN_CONSTRUCTOR_VALUE, $this->options['bdef']['annotation']['scanDir']))); break; case 'dingAnnotationResourceDriver': $bean = new BeanDefinition($name); $bean->setClass('\\Ding\\Bean\\Factory\\Driver\\AnnotationResourceDriver'); break; case 'dingAnnotationInjectDriver': $bean = new BeanDefinition($name); $bean->setClass('\\Ding\\Bean\\Factory\\Driver\\AnnotationInjectDriver'); break; case 'dingPropertiesDriver': $bean = new BeanDefinition($name); $bean->setClass('\\Ding\\Bean\\Factory\\Driver\\PropertiesDriver'); break; case 'dingLifecycleManager': $bean = new BeanDefinition($name); $bean->setClass('\\Ding\\Bean\\Lifecycle\\BeanLifecycleManager'); break; case 'dingMethodInjectionDriver': $bean = new BeanDefinition($name); $bean->setClass('\\Ding\\Bean\\Factory\\Driver\\MethodInjectionDriver'); break; case 'dingMessageSourceDriver': $bean = new BeanDefinition($name); $bean->setClass('\\Ding\\Bean\\Factory\\Driver\\MessageSourceDriver'); break; case 'dingReflectionFactory': $bean = new BeanDefinition($name); $bean->setClass('\\Ding\\Reflection\\ReflectionFactory'); $bean->setArguments(array(new BeanConstructorArgumentDefinition(BeanConstructorArgumentDefinition::BEAN_CONSTRUCTOR_VALUE, isset($this->options['bdef']['annotation'])))); $bean->setProperties(array(new BeanPropertyDefinition('cache', BeanPropertyDefinition::PROPERTY_BEAN, 'dingAnnotationsCache'), new BeanPropertyDefinition('annotationParser', BeanPropertyDefinition::PROPERTY_BEAN, 'dingAnnotationParser'))); break; case 'dingProxyFactory': $bean = new BeanDefinition($name); $bean->setClass('\\Ding\\Aspect\\Proxy'); $bean->setProperties(array(new BeanPropertyDefinition('cache', BeanPropertyDefinition::PROPERTY_BEAN, 'dingProxyCache'))); break; default: break; } return $bean; }
public function beforeCreate(BeanDefinition $beanDefinition) { $props = $beanDefinition->getProperties(); $props[] = new BeanPropertyDefinition('bc', BeanPropertyDefinition::PROPERTY_SIMPLE, 'yeah'); $beanDefinition->setProperties($props); return $beanDefinition; }
/** * (non-PHPdoc) * @see Ding\Bean\Lifecycle.IAfterDefinitionListener::afterDefinition() */ public function afterDefinition(BeanDefinition $bean) { foreach ($bean->getMethodInjections() as $method) { $aspectBeanName = BeanDefinition::generateName('MethodInjectionAspect'); $aspectBean = new BeanDefinition($aspectBeanName); $aspectBean->setClass('\\Ding\\Bean\\Factory\\Driver\\MethodInjectionAspect'); $aspectBean->setProperties(array(new BeanPropertyDefinition('beanName', BeanPropertyDefinition::PROPERTY_SIMPLE, $method[1]))); $this->_beans[$aspectBeanName] = $aspectBean; $aspectName = BeanDefinition::generateName('MethodInjectionAspect'); $pointcutName = BeanDefinition::generateName('MethodInjectionPointcut'); $pointcut = new PointcutDefinition($pointcutName, $method[0], 'invoke'); $this->_aspectManager->setPointcut($pointcut); $aspect = new AspectDefinition($aspectName, array($pointcutName), AspectDefinition::ASPECT_METHOD, $aspectBeanName, ''); $aspects = $bean->getAspects(); $aspects[] = $aspect; $bean->setAspects($aspects); } return $bean; }
/** * Will look for "aware" kind of interfaces and inject whatever necessary. * * @param BeanDefinition $def The Bean Definition * @param object $bean The bean * * @return void */ public function fillAware(BeanDefinition $def, $bean) { $class = get_class($bean); $rClass = $this->_reflectionFactory->getClass($class); if ($rClass->implementsInterface('Ding\\Reflection\\IReflectionFactoryAware')) { $bean->setReflectionFactory($this->_reflectionFactory); } if ($rClass->implementsInterface('Ding\\Bean\\IBeanNameAware')) { $bean->setBeanName($def->getName()); } if ($rClass->implementsInterface('Ding\\Logger\\ILoggerAware')) { $bean->setLogger(\Logger::getLogger($class)); } if ($rClass->implementsInterface('Ding\\Container\\IContainerAware')) { $bean->setContainer($this); } if ($rClass->implementsInterface('Ding\\Resource\\IResourceLoaderAware')) { $bean->setResourceLoader($this); } if ($rClass->implementsInterface('Ding\\Aspect\\IAspectManagerAware')) { $bean->setAspectManager($this->_aspectManager); } if ($rClass->implementsInterface('Ding\\Bean\\IBeanDefinitionProvider')) { $this->registerBeanDefinitionProvider($bean); } if ($rClass->implementsInterface('Ding\\Bean\\Lifecycle\\IAfterConfigListener')) { $this->_lifecycleManager->addAfterConfigListener($bean); } if ($rClass->implementsInterface('Ding\\Bean\\Lifecycle\\IAfterDefinitionListener')) { $this->_lifecycleManager->addAfterDefinitionListener($bean); } if ($rClass->implementsInterface('Ding\\Bean\\Lifecycle\\IBeforeCreateListener')) { $this->_lifecycleManager->addBeforeCreateListener($bean); } if ($rClass->implementsInterface('Ding\\Bean\\Lifecycle\\IAfterCreateListener')) { $this->_lifecycleManager->addAfterCreateListener($bean); } if ($rClass->implementsInterface('Ding\\Bean\\Lifecycle\\IBeforeAssembleListener')) { $this->_lifecycleManager->addBeforeAssembleListener($bean); } if ($rClass->implementsInterface('Ding\\Bean\\Lifecycle\\IAfterAssembleListener')) { $this->_lifecycleManager->addAfterAssembleListener($bean); } if ($rClass->implementsInterface('Ding\\Aspect\\IAspectProvider')) { $this->_aspectManager->registerAspectProvider($bean); } if ($rClass->implementsInterface('Ding\\Aspect\\IPointcutProvider')) { $this->_aspectManager->registerPointcutProvider($bean); } }
/** * Returns a constructor argument definition. * * @param mixed $value Constructor arg YAML structure value. * @param string $yamlFilename Filename for yaml file. * * @throws BeanFactoryException * @return BeanConstructorArgumentDefinition */ private function _loadConstructorArg($name, $value, $yamlFilename) { if (is_array($value)) { if (isset($value['ref'])) { $argType = BeanConstructorArgumentDefinition::BEAN_CONSTRUCTOR_BEAN; $argValue = $value['ref']; } else { if (isset($value['eval'])) { $argType = BeanConstructorArgumentDefinition::BEAN_CONSTRUCTOR_CODE; $argValue = $value['eval']; } else { if (isset($value['bean'])) { $argType = BeanConstructorArgumentDefinition::BEAN_CONSTRUCTOR_BEAN; $innerBean = BeanDefinition::generateName('Bean'); $this->_yamlFiles[$yamlFilename]['beans'][$innerBean] = $value['bean']; $argValue = $innerBean; } else { $argType = BeanConstructorArgumentDefinition::BEAN_CONSTRUCTOR_ARRAY; $argValue = array(); foreach ($value as $key => $inValue) { $argValue[$key] = $this->_loadConstructorArg(false, $inValue, $yamlFilename); } } } } } else { $argType = BeanConstructorArgumentDefinition::BEAN_CONSTRUCTOR_VALUE; $argValue = $value; } if (is_string($name)) { $argName = $name; } else { $argName = false; } return new BeanConstructorArgumentDefinition($argType, $argValue, $argName); }