From: Chuck Hagenbuch Date: Mon, 17 May 2010 20:37:21 +0000 (-0400) Subject: Some tighter and cleaner code in the AnnotatedSetters binder X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=24042552d10058d39c00ad0d8b96fbec82333706;p=horde.git Some tighter and cleaner code in the AnnotatedSetters binder - Fixed lifetime of $setters array, removing members variable that lasted too long - Implemented equals() correctly - Removed an unnecessary method distiction (between findSetters and bindSetters) - Broke determination of what is and isn't a setter into a separate method --- diff --git a/framework/Injector/lib/Horde/Injector/Binder/AnnotatedSetters.php b/framework/Injector/lib/Horde/Injector/Binder/AnnotatedSetters.php index eb8e0ce3b..c980746df 100644 --- a/framework/Injector/lib/Horde/Injector/Binder/AnnotatedSetters.php +++ b/framework/Injector/lib/Horde/Injector/Binder/AnnotatedSetters.php @@ -22,8 +22,6 @@ class Horde_Injector_Binder_AnnotatedSetters implements Horde_Injector_Binder */ private $_dependencyFinder; - private $_setters = array(); - /** * */ @@ -38,7 +36,7 @@ class Horde_Injector_Binder_AnnotatedSetters implements Horde_Injector_Binder */ public function equals(Horde_Injector_Binder $otherBinder) { - return false; + return $otherBinder instanceof Horde_Injector_Binder_AnnotatedSetters; } /** @@ -49,22 +47,13 @@ class Horde_Injector_Binder_AnnotatedSetters implements Horde_Injector_Binder $instance = $this->_binder->create($injector); $reflectionClass = new ReflectionClass(get_class($instance)); - $this->_bindAnnotatedSetters($reflectionClass); - $this->_callSetters($injector, $instance); + $setters = $this->_findAnnotatedSetters($reflectionClass); + $this->_callSetters($setters, $injector, $instance); return $instance; } /** - */ - private function _bindAnnotatedSetters(ReflectionClass $reflectionClass) - { - foreach ($this->_findAnnotatedSetters($reflectionClass) as $setter) { - $this->_setters[] = $setter; - } - } - - /** * Find all public methods in $reflectionClass that are annotated with * @inject. * @@ -76,11 +65,8 @@ class Horde_Injector_Binder_AnnotatedSetters implements Horde_Injector_Binder { $setters = array(); foreach ($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC) as $reflectionMethod) { - $docBlock = $reflectionMethod->getDocComment(); - if ($docBlock) { - if (strpos($docBlock, '@inject') !== false) { - $setters[] = $reflectionMethod->name; - } + if ($this->_isSetterMethod($reflectionMethod)) { + $setters[] = $reflectionMethod; } } @@ -88,15 +74,32 @@ class Horde_Injector_Binder_AnnotatedSetters implements Horde_Injector_Binder } /** - * TODO + * Is a method a setter method, by the criteria we define (has a doc comment + * that includes @inject). + * + * @param ReflectionMethod $reflectionMethod + */ + private function _isSetterMethod(ReflectionMethod $reflectionMethod) + { + $docBlock = $reflectionMethod->getDocComment(); + if ($docBlock) { + if (strpos($docBlock, '@inject') !== false) { + return true; + } + } + + return false; + } + + /** + * */ - protected function _callSetters(Horde_Injector $injector, $instance) + private function _callSetters(array $setters, Horde_Injector $injector, $instance) { - foreach ($this->_setters as $setter) { - $reflectionMethod = new ReflectionMethod($instance, $setter); - $reflectionMethod->invokeArgs( + foreach ($setters as $setterMethod) { + $setterMethod->invokeArgs( $instance, - $this->_dependencyFinder->getMethodDependencies($injector, $reflectionMethod) + $this->_dependencyFinder->getMethodDependencies($injector, $setterMethod) ); } }