Changeset 855
- Timestamp:
- 08/20/07 21:39:23 (11 months ago)
- Files:
-
- trunk/experiments/people/schst/ioc/provider.php (added)
- trunk/src/main/php/net/stubbles/ioc/ioc.php (moved) (moved from trunk/src/main/php/net/stubbles/ioc/injection.php)
- trunk/src/main/php/net/stubbles/ioc/stubBindingScope.php (modified) (1 diff)
- trunk/src/main/php/net/stubbles/ioc/stubBindingScopeSingleton.php (modified) (1 diff)
- trunk/src/main/php/net/stubbles/ioc/stubClassBinding.php (modified) (6 diffs)
- trunk/src/main/php/net/stubbles/ioc/stubDefaultInjectionProvider.php (added)
- trunk/src/main/php/net/stubbles/ioc/stubInjectionProvider.php (modified) (2 diffs)
- trunk/src/main/php/net/stubbles/ioc/stubInjector.php (modified) (1 diff)
- trunk/src/test/php/net/stubbles/ioc/stubInjectorSingletonTestCase.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/ioc/stubBindingScope.php
r852 r855 14 14 */ 15 15 interface stubBindingScope extends stubObject { 16 public function getInstance( $key, stubBinding $binding);16 public function getInstance(stubReflectionClass $type, stubBinding $binding); 17 17 18 18 } trunk/src/main/php/net/stubbles/ioc/stubBindingScopeSingleton.php
r852 r855 32 32 * @return object 33 33 */ 34 public function getInstance($key, stubBinding $binding) { 34 public function getInstance(stubReflectionClass $type, stubBinding $binding) { 35 $key = $type->getName(); 35 36 if (!isset($this->instances[$key])) { 36 37 $this->instances[$key] = $binding->create(); trunk/src/main/php/net/stubbles/ioc/stubClassBinding.php
r852 r855 7 7 * @subpackage ioc_injection 8 8 */ 9 stubClassLoader::load('net.stubbles.ioc.exceptions.stubBindingException'); 9 stubClassLoader::load('net.stubbles.ioc.exceptions.stubBindingException', 10 'net.stubbles.ioc.stubDefaultInjectionProvider'); 10 11 /** 11 12 * Binding to bind an interface to an implementation … … 60 61 61 62 /** 63 * The provider to use for this binding 64 * 65 * @var stubInjectionProvider 66 */ 67 protected $provider = null; 68 69 /** 62 70 * Create a new binding 63 71 * … … 103 111 104 112 /** 113 * Set the provider that should be used to create 114 * instances for this binding. 115 * 116 * This cannot be used in conjuction with the 117 * 'toInstance()' method. 118 * 119 * @param stubInjectionProvider 120 * @return stubBinding 121 */ 122 public function toProvider(stubInjectionProvider $provider) { 123 $this->provider = $provider; 124 return $this; 125 } 126 127 /** 105 128 * Set the scope 106 129 * … … 125 148 126 149 /** 127 * Get the instance for this binding 150 * Get the instance for this binding. 151 * 152 * This method will at first look for an existing 153 * instance in the scope and then create the instance 154 * using the create() method. 128 155 * 129 156 * @return object 157 * @see create() 130 158 */ 131 159 public function getInstance() { … … 144 172 } 145 173 if ($this->scope != null) { 146 return $this->scope->getInstance($this-> type, $this);174 return $this->scope->getInstance($this->impl, $this); 147 175 } 148 176 return $this->create(); 149 150 177 } 151 178 … … 154 181 * 155 182 * @return object 156 * @todo Decide, where to put this functionality157 183 */ 158 184 public function create() { 185 if ($this->provider === null) { 186 $this->provider = new stubDefaultInjectionProvider($this->injector); 187 } 159 188 if (is_string($this->impl)) { 160 189 $this->impl = new stubReflectionClass($this->impl); 161 190 } 162 $constructor = $this->impl->getConstructor(); 163 if ($constructor === null) { 164 $instance = $this->impl->newInstance(); 165 } elseif (!$constructor->hasAnnotation('Inject')) { 166 $instance = $this->impl->newInstance(); 167 } else { 168 $paramValues = array(); 169 foreach ($constructor->getParameters() as $param) { 170 $class = $param->getClass(); 171 $paramValues[] = $this->injector->getInstance($class->getName()); 172 } 173 $instance = $this->impl->newInstanceArgs($paramValues); 174 } 175 176 foreach ($this->impl->getMethods() as $method) { 177 if (strncmp($method->getName(), '__', 2) === 0) { 178 continue; 179 } 180 if (!$method->isPublic()) { 181 continue; 182 } 183 if (!$method->hasAnnotation('Inject')) { 184 continue; 185 } 186 $paramValues = array(); 187 foreach ($method->getParameters() as $param) { 188 $class = $param->getClass(); 189 if ($class !== null) { 190 $type = $class->getName(); 191 } else { 192 $type = stubConstantBinding::TYPE; 193 } 194 195 $name = null; 196 if ($method->hasAnnotation('Named')) { 197 $name = $method->getAnnotation('Named')->getName(); 198 } 199 if (!$this->injector->hasBinding($type, $name)) { 200 if ($method->getAnnotation('Inject')->isOptional()) { 201 continue 2; 202 } 203 throw new stubBindingException("Could not create instance of {$this->type}. No binding for type {$type} specified."); 204 } 205 $paramValues[] = $this->injector->getInstance($type, $name); 206 } 207 $method->invokeArgs($instance, $paramValues); 208 } 209 return $instance; 191 return $this->provider->get($this->impl); 210 192 } 211 193 trunk/src/main/php/net/stubbles/ioc/stubInjectionProvider.php
r852 r855 5 5 * features of Stubbles. 6 6 * 7 * @author Frank Kleine <mikey@stubbles.net>8 7 * @author Stephan Schmidt <schst@stubbles.net> 9 8 * @package stubbles … … 22 21 { 23 22 /** 24 * Returns the injection for the given keyword23 * Returns the object 25 24 * 26 * @param string $key keyword under which injection is stored 27 * @return stubObject 25 * @return object 28 26 */ 29 public function getInjection($key); 30 31 /** 32 * Check whether an injection for the given keyword exists 33 * 34 * @param string $key keyword under which injection is stored 35 * @return bool true if an injection for given keyword exists 36 */ 37 public function hasInjection($key); 27 public function get(stubReflectionClass $type); 38 28 } 39 29 ?> trunk/src/main/php/net/stubbles/ioc/stubInjector.php
r852 r855 47 47 $binding = $this->getBinding($type, $name); 48 48 if ($binding != null) { 49 $scope = $binding->getScope(); 50 if ($scope != null) { 51 return $scope->getInstance($type, $binding); 52 } else { 53 return $binding->getInstance(); 54 } 49 return $binding->getInstance(); 55 50 } 56 51 trunk/src/test/php/net/stubbles/ioc/stubInjectorSingletonTestCase.php
r854 r855 121 121 $this->assertIsA($slot, 'stubInjectorSingletonTestCase_SlotMachine'); 122 122 $this->assertIsA($slot->number1, 'stubInjectorSingletonTestCase_Number'); 123 $this->assertIsA($slot->number1, 'stubInjectorSingletonTestCase_Random ');123 $this->assertIsA($slot->number1, 'stubInjectorSingletonTestCase_RandomSingleton'); 124 124 $this->assertIsA($slot->number2, 'stubInjectorSingletonTestCase_Number'); 125 $this->assertIsA($slot->number2, 'stubInjectorSingletonTestCase_Random ');125 $this->assertIsA($slot->number2, 'stubInjectorSingletonTestCase_RandomSingleton'); 126 126 $this->assertIdentical($slot->number1, $slot->number2); 127 127 }
