Changeset 825
- Timestamp:
- 08/14/07 21:00:12 (11 months ago)
- Files:
-
- trunk/experiments/people/schst/ioc/naming.php (added)
- trunk/src/main/php/net/stubbles/ioc/injection/annotations/stubNamedAnnotation.php (added)
- trunk/src/main/php/net/stubbles/ioc/injection/stubBinder.php (modified) (2 diffs)
- trunk/src/main/php/net/stubbles/ioc/injection/stubBinding.php (modified) (9 diffs)
- trunk/src/main/php/net/stubbles/ioc/injection/stubBindingScope.php (modified) (1 diff)
- trunk/src/main/php/net/stubbles/ioc/injection/stubBindingScopeSingleton.php (modified) (1 diff)
- trunk/src/main/php/net/stubbles/ioc/injection/stubBindingScopes.php (modified) (1 diff)
- trunk/src/main/php/net/stubbles/ioc/injection/stubInjector.php (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/ioc/injection/stubBinder.php
r821 r825 20 20 'net.stubbles.ioc.injection.annotations.stubInjectAnnotation', 21 21 'net.stubbles.ioc.injection.annotations.stubSingletonAnnotation', 22 'net.stubbles.ioc.injection.annotations.stubNamedAnnotation', 22 23 'net.stubbles.ioc.injection.annotations.stubImplementedByAnnotation'); 23 24 /** … … 30 31 * @subpackage ioc_injection 31 32 */ 32 class stubBinder {33 class stubBinder extends stubBaseObject { 33 34 34 35 /** trunk/src/main/php/net/stubbles/ioc/injection/stubBinding.php
r823 r825 18 18 * @todo Introduce providers that create the concrete instances 19 19 */ 20 class stubBinding {20 class stubBinding extends stubBaseObject { 21 21 22 22 /** … … 28 28 29 29 /** 30 * The keyfor this binding30 * The type for this binding 31 31 * 32 32 * @var string 33 33 */ 34 protected $ key= null;34 protected $type = null; 35 35 36 36 /** … … 40 40 */ 41 41 protected $impl = null; 42 43 /** 44 * Annotated with a name 45 * 46 * @var string 47 */ 48 protected $name = null; 42 49 43 50 /** … … 52 59 * 53 60 * @param stubInjector $injector 54 * @param string $ key61 * @param string $type 55 62 */ 56 public function __construct($injector, $ key) {63 public function __construct($injector, $type) { 57 64 $this->injector = $injector; 58 $this->key = $key; 59 $this->impl = $key; 60 } 61 62 /** 63 * Get the key 64 * 65 * @return string 66 */ 67 public function getKey() { 68 return $this->key; 65 $this->type = $type; 66 $this->impl = $type; 69 67 } 70 68 … … 82 80 * 83 81 * @param stubReflectionClass|string $impl 82 * @return stubBinding 84 83 */ 85 84 public function to($impl) { … … 92 91 * 93 92 * @param stubBindingScope $scope 93 * @return stubBinding 94 94 */ 95 95 public function in(stubBindingScope $scope) { 96 96 $this->scope = $scope; 97 return $this; 98 } 99 100 /** 101 * Set the name of the injection 102 * 103 * @param string $name 104 * @return stubBinding 105 */ 106 public function named($name) { 107 $this->name = $name; 97 108 return $this; 98 109 } … … 113 124 } 114 125 } 115 116 126 if ($this->scope != null) { 117 return $this->scope->getInstance($this-> key, $this);127 return $this->scope->getInstance($this->type, $this); 118 128 } 119 129 return $this->create(); … … 158 168 foreach ($method->getParameters() as $param) { 159 169 $class = $param->getClass(); 160 $paramValues[] = $this->injector->getInstance($class->getName()); 170 $name = null; 171 if ($method->hasAnnotation('Named')) { 172 $name = $method->getAnnotation('Named')->getName(); 173 } 174 $paramValues[] = $this->injector->getInstance($class->getName(), $name); 161 175 } 162 176 $method->invokeArgs($instance, $paramValues); … … 164 178 return $instance; 165 179 } 180 181 /** 182 * Creates a unique key for this binding 183 */ 184 public function getKey() { 185 if ($this->name === null) { 186 return $this->type; 187 } 188 return $this->type . '#' . $this->name; 189 } 190 191 166 192 } 167 193 ?> trunk/src/main/php/net/stubbles/ioc/injection/stubBindingScope.php
r810 r825 16 16 * @subpackage ioc_injection 17 17 */ 18 interface stubBindingScope {18 interface stubBindingScope extends stubObject { 19 19 public function getInstance($key, stubBinding $binding); 20 20 trunk/src/main/php/net/stubbles/ioc/injection/stubBindingScopeSingleton.php
r821 r825 19 19 * @subpackage ioc_injection 20 20 */ 21 class stubBindingScopeSingleton implements stubBindingScope {21 class stubBindingScopeSingleton extends stubBaseObject implements stubBindingScope { 22 22 23 23 /** trunk/src/main/php/net/stubbles/ioc/injection/stubBindingScopes.php
r810 r825 19 19 * @subpackage ioc_injection 20 20 */ 21 class stubBindingScopes {21 class stubBindingScopes extends stubBaseObject { 22 22 23 23 /** trunk/src/main/php/net/stubbles/ioc/injection/stubInjector.php
r822 r825 21 21 * @subpackage ioc_injection 22 22 */ 23 class stubInjector {23 class stubInjector extends stubBaseObject { 24 24 25 25 /** … … 40 40 * Get an instance 41 41 * 42 * @param string $ key42 * @param string $type 43 43 * @return object 44 * @todo build an index of the bindings45 44 */ 46 public function getInstance($ key) {45 public function getInstance($type, $name = null) { 47 46 if (!empty($this->bindings)) { 48 47 $this->updateIndex(); 49 48 } 50 51 if (isset($this->bindingIndex[$key])) { 52 $binding = $this->bindingIndex[$key]; 49 $binding = $this->getBinding($type, $name); 50 if ($binding != null) { 53 51 $scope = $binding->getScope(); 54 52 if ($scope != null) { 55 return $scope->getInstance($ key, $binding);53 return $scope->getInstance($type, $binding); 56 54 } else { 57 55 return $binding->getInstance(); … … 60 58 61 59 // check for default implementation 62 $clazz = new stubReflectionClass($ key);60 $clazz = new stubReflectionClass($type); 63 61 if ($clazz->hasAnnotation('ImplementedBy')) { 64 62 $implementedBy = $clazz->getAnnotation('ImplementedBy'); 65 $tmp = new stubBinding($this, $ key);63 $tmp = new stubBinding($this, $type); 66 64 $tmp->to($implementedBy->getDefaultImplementation()); 67 65 $this->addBinding($tmp); … … 72 70 // TODO: Find a useful location for this code 73 71 if (!$clazz->isInterface()) { 74 $tmp = new stubBinding($this, $ key);72 $tmp = new stubBinding($this, $type); 75 73 $this->addBinding($tmp); 76 74 return $tmp->getInstance(); 77 75 } 78 throw new stubException('No binding for ' . $ key. ' defined');76 throw new stubException('No binding for ' . $type . ' defined'); 79 77 } 80 78 … … 84 82 protected function updateIndex() { 85 83 foreach ($this->bindings as $binding) { 86 $this->bindingIndex[$ this->createKey($binding)] = $binding;84 $this->bindingIndex[$binding->getKey()] = $binding; 87 85 } 88 86 $this->bindings = array(); 89 }90 91 /**92 * Creates a unique key for a binding93 *94 * @param stubBinding $binding95 * @todo Key must also contain @Named() annotation value96 */97 protected function createKey(stubBinding $binding) {98 $interface = $binding->getKey();99 if ($interface instanceof stubReflectionClass) {100 $interface = $interface->getName();101 }102 return $interface;103 87 } 104 88 … … 111 95 $this->bindings[] = $binding; 112 96 } 97 98 protected function getBinding($type, $name = null) { 99 if ($name !== null) { 100 if (isset($this->bindingIndex[$type.'#'.$name])) { 101 return $this->bindingIndex[$type.'#'.$name]; 102 } 103 } 104 if (isset($this->bindingIndex[$type])) { 105 return $this->bindingIndex[$type]; 106 } 107 return null; 108 } 113 109 } 114 110 ?>
