[Beeframework-svn] SF.net SVN: beeframework:[182] trunk/framework/Bee
Brought to you by:
b_hartmann,
m_plomer
From: <m_p...@us...> - 2014-07-20 15:45:57
|
Revision: 182 http://sourceforge.net/p/beeframework/code/182 Author: m_plomer Date: 2014-07-20 15:45:49 +0000 (Sun, 20 Jul 2014) Log Message: ----------- - Doctrine2: first design of entity injection Modified Paths: -------------- trunk/framework/Bee/Context/BeanCreationException.php trunk/framework/Bee/Context/BeanDefinitionValueResolver.php Added Paths: ----------- trunk/framework/Bee/Persistence/Doctrine2/BeanInjection/ trunk/framework/Bee/Persistence/Doctrine2/BeanInjection/Inject.php trunk/framework/Bee/Persistence/Doctrine2/BeanInjection/InjectionEventListener.php Modified: trunk/framework/Bee/Context/BeanCreationException.php =================================================================== --- trunk/framework/Bee/Context/BeanCreationException.php 2014-07-12 17:39:11 UTC (rev 181) +++ trunk/framework/Bee/Context/BeanCreationException.php 2014-07-20 15:45:49 UTC (rev 182) @@ -48,6 +48,4 @@ public function getBeanName() { return $this->name; } -} - -?> \ No newline at end of file +} \ No newline at end of file Modified: trunk/framework/Bee/Context/BeanDefinitionValueResolver.php =================================================================== --- trunk/framework/Bee/Context/BeanDefinitionValueResolver.php 2014-07-12 17:39:11 UTC (rev 181) +++ trunk/framework/Bee/Context/BeanDefinitionValueResolver.php 2014-07-20 15:45:49 UTC (rev 182) @@ -153,7 +153,7 @@ } catch (Bee_Context_BeansException $ex) { // @todo: a lot of debug information is lost here - throw new Bee_Context_BeanCreationException($this->beanName, "error resolving reference for argument $argName", $ex); + throw new Bee_Context_BeanCreationException($this->beanName, "Error resolving reference for argument $argName", $ex); } } Added: trunk/framework/Bee/Persistence/Doctrine2/BeanInjection/Inject.php =================================================================== --- trunk/framework/Bee/Persistence/Doctrine2/BeanInjection/Inject.php (rev 0) +++ trunk/framework/Bee/Persistence/Doctrine2/BeanInjection/Inject.php 2014-07-20 15:45:49 UTC (rev 182) @@ -0,0 +1,18 @@ +<?php + +namespace Bee\Persistence\Doctrine2\BeanInjection; + + +/** + * Class Inject + * @package Bee\Persistence\Doctrine2\BeanInjection + * + * @Annotation + * @Target({"METHOD","PROPERTY"}) + */ +class Inject { + /** + * @var string + */ + public $beanName; +} \ No newline at end of file Added: trunk/framework/Bee/Persistence/Doctrine2/BeanInjection/InjectionEventListener.php =================================================================== --- trunk/framework/Bee/Persistence/Doctrine2/BeanInjection/InjectionEventListener.php (rev 0) +++ trunk/framework/Bee/Persistence/Doctrine2/BeanInjection/InjectionEventListener.php 2014-07-20 15:45:49 UTC (rev 182) @@ -0,0 +1,116 @@ +<?php +namespace Bee\Persistence\Doctrine2\BeanInjection; + +use Bee_IContext; +use Doctrine\Common\Annotations\AnnotationReader; +use Doctrine\Common\EventSubscriber; +use Doctrine\ORM\Event\LifecycleEventArgs; +use Doctrine\ORM\Event\LoadClassMetadataEventArgs; +use Doctrine\ORM\Events; +use ReflectionClass; +use ReflectionMethod; +use ReflectionProperty; + +/** + * Class InjectionEventListener + */ +class InjectionEventListener implements EventSubscriber, \Bee_Context_Config_IContextAware { + /** + * @var AnnotationReader + */ + private $reader; + + /** + * @var Bee_IContext + */ + private $context; + + /** + * @param LifecycleEventArgs $eventArgs + */ + public function postLoad(LifecycleEventArgs $eventArgs) { + + $entity = $eventArgs->getEntity(); + $reflClass = new ReflectionClass($entity); + + foreach($reflClass->getProperties() as $prop) { + foreach ($this->reader->getPropertyAnnotations($prop) AS $annot) { + if ($annot instanceof Inject) { + $this->injectIntoProperty($entity, $prop, $annot); + } + } + } + + foreach($reflClass->getMethods(ReflectionProperty::IS_PUBLIC) as $method) { + if($method->getNumberOfRequiredParameters() == 1) { + foreach ($this->reader->getPropertyAnnotations($method) AS $annot) { + if ($annot instanceof Inject) { + $this->injectIntoSetter($entity, $method, $annot); + } + } + } + } + } + + /** + * @param $entity + * @param ReflectionProperty $prop + * @param Inject $annotation + */ + protected function injectIntoProperty($entity, ReflectionProperty $prop, Inject $annotation) { + $value = $this->context->getBean($annotation->beanName); + $prop->setAccessible(true); + $prop->setValue($entity, $value); + } + + /** + * @param $entity + * @param ReflectionMethod $method + * @param Inject $annotation + */ + protected function injectIntoSetter($entity, ReflectionMethod $method, Inject $annotation) { + $value = $this->context->getBean($annotation->beanName); + $method->setAccessible(true); + $method->invoke($entity, $value); + } + + /** + * @param LoadClassMetadataEventArgs $eventArgs + */ +// public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs) { +// $classMata = $eventArgs->getClassMetadata(); +// } + + /** + * Returns an array of events this subscriber wants to listen to. + * + * @return array + */ + public function getSubscribedEvents() { + return array( + Events::postLoad//, +// Events::loadClassMetadata + ); + } + + /** + * @return AnnotationReader + */ + public function getReader() { + return $this->reader; + } + + /** + * @param AnnotationReader $reader + */ + public function setReader(AnnotationReader $reader) { + $this->reader = $reader; + } + + /** + * @param Bee_IContext $context + */ + public function setBeeContext(Bee_IContext $context) { + $this->context = $context; + } +} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |