first pass at automatic setter injection
authorChuck Hagenbuch <chuck@horde.org>
Sun, 14 Mar 2010 02:03:46 +0000 (21:03 -0500)
committerChuck Hagenbuch <chuck@horde.org>
Sun, 14 Mar 2010 02:16:28 +0000 (21:16 -0500)
framework/Injector/lib/Horde/Injector.php
framework/Injector/lib/Horde/Injector/Binder/Implementation.php

index c37bd27..eefb3ef 100644 (file)
@@ -234,5 +234,4 @@ class Horde_Injector implements Horde_Injector_Scope
 
         return $this->_instances[$interface];
     }
-
 }
index 66928a3..2ccb4a3 100644 (file)
@@ -16,8 +16,9 @@ class Horde_Injector_Binder_Implementation implements Horde_Injector_Binder
 
     /**
      * TODO
+     * @var array
      */
-    private $_setters;
+    private $_setters = array();
 
     /**
      * TODO
@@ -62,6 +63,10 @@ class Horde_Injector_Binder_Implementation implements Horde_Injector_Binder
         $reflectionClass = new ReflectionClass($this->_implementation);
         $this->_validateImplementation($reflectionClass);
         $instance = $this->_getInstance($injector, $reflectionClass);
+        $setters = $this->_findSetters($reflectionClass);
+        foreach ($setters as $setter) {
+            $this->bindSetter($setter);
+        }
         $this->_callSetters($injector, $instance);
         return $instance;
     }
@@ -131,4 +136,25 @@ class Horde_Injector_Binder_Implementation implements Horde_Injector_Binder
         }
     }
 
+    /**
+     * Find annotated setters in the class docblock
+     *
+     * @param ReflectionClass $reflectionClass
+     *
+     * @return array
+     */
+    private function _findSetters(ReflectionClass $reflectionClass)
+    {
+        $setters = array();
+        $docBlock = $reflectionClass->getDocComment();
+        if ($docBlock) {
+            if (preg_match_all('/@inject (\w+)/', $docBlock, $matches)) {
+                foreach ($matches[1] as $setter) {
+                    $setters[] = $setter;
+                }
+            }
+        }
+
+        return $setters;
+    }
 }