Changeset 846
- Timestamp:
- 08/19/07 16:05:30 (1 year ago)
- Files:
-
- trunk/experiments/people/schst/ioc/constants.php (added)
- trunk/src/main/php/net/stubbles/ioc/injection/EXPERIMENTAL (added)
- trunk/src/main/php/net/stubbles/ioc/injection/injection.php (modified) (1 diff)
- trunk/src/main/php/net/stubbles/ioc/injection/stubBinder.php (modified) (3 diffs)
- trunk/src/main/php/net/stubbles/ioc/injection/stubBinding.php (modified) (4 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/stubClassBinding.php (added)
- trunk/src/main/php/net/stubbles/ioc/injection/stubConstantBinding.php (added)
- trunk/src/main/php/net/stubbles/ioc/injection/stubInjectionProvider.php (modified) (2 diffs)
- trunk/src/main/php/net/stubbles/ioc/injection/stubInjector.php (modified) (4 diffs)
- trunk/src/test/php/net/stubbles/ioc/IOCTestSuite.php (modified) (1 diff)
- trunk/src/test/php/net/stubbles/ioc/injection/stubInjectorConstantTestCase.php (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/ioc/injection/injection.php
r808 r846 3 3 * Injection handling bootstrap file. 4 4 * 5 * @author Frank Kleine <mikey@stubbles.net>5 * @author Stephan Schmidt <schst@stubbles.net> 6 6 * @package stubbles 7 7 * @subpackage ioc_injection 8 8 */ 9 stubClassLoader::load('net.stubbles.ioc.injection.stubInjectionException', 10 'net.stubbles.ioc.injection.stubInjectionProvider', 11 'net.stubbles.ioc.injection.stubInjectionMap', 12 'net.stubbles.ioc.injection.annotations.stubInjectAnnotation' 13 ); 9 stubClassLoader::load('net.stubbles.ioc.injection.stubBinder'); 14 10 ?> trunk/src/main/php/net/stubbles/ioc/injection/stubBinder.php
r844 r846 6 6 * the same injector. 7 7 * 8 * WARNING: This is experimental and far from finished!9 * Huge refactorings will follow!10 *11 8 * @author Stephan Schmidt <schst@stubbles.net> 12 9 * @package stubbles … … 15 12 stubClassLoader::load('net.stubbles.reflection.stubReflectionClass', 16 13 'net.stubbles.ioc.injection.stubBinding', 14 'net.stubbles.ioc.injection.stubClassBinding', 15 'net.stubbles.ioc.injection.stubConstantBinding', 17 16 'net.stubbles.ioc.injection.stubInjector', 18 17 'net.stubbles.ioc.injection.stubBindingScope', … … 57 56 * 58 57 * @param string $interface 59 * @return stub Binding58 * @return stubClassBinding 60 59 */ 61 60 public function bind($interface) { 62 $binding = new stubBinding($this->injector, $interface); 61 $binding = new stubClassBinding($this->injector, $interface); 62 $this->injector->addBinding($binding); 63 return $binding; 64 } 65 66 /** 67 * Bind a new constant 68 * 69 * @return stubConstantBinding 70 */ 71 public function bindConstant() { 72 $binding = new stubConstantBinding($this->injector); 63 73 $this->injector->addBinding($binding); 64 74 return $binding; trunk/src/main/php/net/stubbles/ioc/injection/stubBinding.php
r844 r846 2 2 /** 3 3 * Binding to bind an interface to an implementation 4 *5 * WARNING: This is experimental and far from finished!6 * Huge refactorings will follow!7 4 * 8 5 * @author Stephan Schmidt <schst@stubbles.net> … … 18 15 * @todo Introduce providers that create the concrete instances 19 16 */ 20 class stubBinding extends stubBaseObject { 21 22 /** 23 * The injector used by this binding 24 * 25 * @var stubInjector 26 */ 27 protected $injector = null; 28 29 /** 30 * The type for this binding 31 * 32 * @var string 33 */ 34 protected $type = null; 35 36 /** 37 * The class that implements this binding 38 * 39 * @var stubReflectionClass 40 */ 41 protected $impl = null; 42 43 /** 44 * Annotated with a name 45 * 46 * @var string 47 */ 48 protected $name = null; 49 50 /** 51 * Scope of the binding 52 * 53 * @var stubBindingScope 54 */ 55 protected $scope = null; 56 57 /** 58 * Instance this type is bound to 59 * 60 * @var object 61 */ 62 protected $instance = null; 63 64 /** 65 * Create a new binding 66 * 67 * @param stubInjector $injector 68 * @param string $type 69 */ 70 public function __construct($injector, $type) { 71 $this->injector = $injector; 72 $this->type = $type; 73 $this->impl = $type; 74 } 75 76 /** 77 * Get the scope of the binding 78 * 79 * @return stubBindingScope 80 */ 81 public function getScope() { 82 return $this->scope; 83 } 84 85 /** 86 * Set the concrete implementation 87 * 88 * @param stubReflectionClass|string $impl 89 * @return stubBinding 90 */ 91 public function to($impl) { 92 $this->impl = $impl; 93 return $this; 94 } 95 96 /** 97 * Set the concrete instance 98 * 99 * @param object $impl 100 * @return stubBinding 101 */ 102 public function toInstance($instance) { 103 $this->instance = $instance; 104 return $this; 105 } 106 107 /** 108 * Set the scope 109 * 110 * @param stubBindingScope $scope 111 * @return stubBinding 112 */ 113 public function in(stubBindingScope $scope) { 114 $this->scope = $scope; 115 return $this; 116 } 17 interface stubBinding extends stubObject { 117 18 118 19 /** … … 122 23 * @return stubBinding 123 24 */ 124 public function named($name) { 125 $this->name = $name; 126 return $this; 127 } 25 public function named($name); 128 26 129 27 /** … … 132 30 * @return object 133 31 */ 134 public function getInstance() { 135 if ($this->instance != null) { 136 return $this->instance; 137 } 138 139 if (is_string($this->impl)) { 140 $this->impl = new stubReflectionClass($this->impl); 141 } 142 143 if ($this->scope === null) { 144 if ($this->impl->hasAnnotation('Singleton')) { 145 $this->scope = stubBindingScopes::$SINGLETON; 146 } 147 } 148 if ($this->scope != null) { 149 return $this->scope->getInstance($this->type, $this); 150 } 151 return $this->create(); 152 153 } 154 155 /** 156 * Create an instance 157 * 158 * @return object 159 * @todo Decide, where to put this functionality 160 */ 161 public function create() { 162 if (is_string($this->impl)) { 163 $this->impl = new stubReflectionClass($this->impl); 164 } 165 $constructor = $this->impl->getConstructor(); 166 if ($constructor === null) { 167 $instance = $this->impl->newInstance(); 168 } elseif (!$constructor->hasAnnotation('Inject')) { 169 $instance = $this->impl->newInstance(); 170 } else { 171 $paramValues = array(); 172 foreach ($constructor->getParameters() as $param) { 173 $class = $param->getClass(); 174 $paramValues[] = $this->injector->getInstance($class->getName()); 175 } 176 $instance = $this->impl->newInstanceArgs($paramValues); 177 } 178 179 foreach ($this->impl->getMethods() as $method) { 180 if (strncmp($method->getName(), '__', 2) === 0) { 181 continue; 182 } 183 if (!$method->isPublic()) { 184 continue; 185 } 186 if (!$method->hasAnnotation('Inject')) { 187 continue; 188 } 189 $paramValues = array(); 190 foreach ($method->getParameters() as $param) { 191 $class = $param->getClass(); 192 $type = $class->getName(); 193 $name = null; 194 if ($method->hasAnnotation('Named')) { 195 $name = $method->getAnnotation('Named')->getName(); 196 } 197 if (!$this->injector->hasBinding($type, $name)) { 198 if ($method->getAnnotation('Inject')->isOptional()) { 199 continue 2; 200 } 201 throw new stubBindingException("Could not create instance of {$this->type}. No binding for type {$type} specified."); 202 } 203 $paramValues[] = $this->injector->getInstance($type, $name); 204 } 205 $method->invokeArgs($instance, $paramValues); 206 } 207 return $instance; 208 } 32 public function getInstance(); 209 33 210 34 /** 211 35 * Creates a unique key for this binding 212 36 */ 213 public function getKey() { 214 if ($this->name === null) { 215 return $this->type; 216 } 217 return $this->type . '#' . $this->name; 218 } 37 public function getKey(); 38 39 /** 40 * Get the scope for this binding 41 * 42 * @return stubBindingScope 43 */ 44 public function getScope(); 219 45 } 220 46 ?> trunk/src/main/php/net/stubbles/ioc/injection/stubBindingScope.php
r825 r846 2 2 /** 3 3 * Interface for all scopes 4 *5 * WARNING: This is experimental and far from finished!6 * Huge refactorings will follow!7 4 * 8 5 * @author Stephan Schmidt <schst@stubbles.net> trunk/src/main/php/net/stubbles/ioc/injection/stubBindingScopeSingleton.php
r825 r846 2 2 /** 3 3 * Scope for singletons 4 *5 * WARNING: This is experimental and far from finished!6 * Huge refactorings will follow!7 4 * 8 5 * @author Stephan Schmidt <schst@stubbles.net> trunk/src/main/php/net/stubbles/ioc/injection/stubBindingScopes.php
r825 r846 2 2 /** 3 3 * All built-in scopes 4 *5 * WARNING: This is experimental and far from finished!6 * Huge refactorings will follow!7 4 * 8 5 * @author Stephan Schmidt <schst@stubbles.net> trunk/src/main/php/net/stubbles/ioc/injection/stubInjectionProvider.php
r344 r846 1 1 <?php 2 2 /** 3 * Map for injections. 3 * Interface for providers that create objects 4 * that are required by the Inversion of Control 5 * features of Stubbles. 4 6 * 5 7 * @author Frank Kleine <mikey@stubbles.net> 8 * @author Stephan Schmidt <schst@stubbles.net> 6 9 * @package stubbles 7 10 * @subpackage ioc_injection 8 11 */ 12 9 13 /** 10 * Map for injections. 14 * Interface for providers that create objects 15 * that are required by the Inversion of Control 16 * features of Stubbles. 11 17 * 12 18 * @package stubbles … … 22 28 */ 23 29 public function getInjection($key); 24 30 25 31 /** 26 32 * Check whether an injection for the given keyword exists trunk/src/main/php/net/stubbles/ioc/injection/stubInjector.php
r844 r846 4 4 * 5 5 * Used to create the instances. 6 *7 * WARNING: This is experimental and far from finished!8 * Huge refactorings will follow!9 6 * 10 7 * @author Stephan Schmidt <schst@stubbles.net> … … 62 59 if ($clazz->hasAnnotation('ImplementedBy')) { 63 60 $implementedBy = $clazz->getAnnotation('ImplementedBy'); 64 $tmp = new stub Binding($this, $type);61 $tmp = new stubClassBinding($this, $type); 65 62 $tmp->to($implementedBy->getDefaultImplementation()); 66 63 $this->addBinding($tmp); … … 71 68 // TODO: Find a useful location for this code 72 69 if (!$clazz->isInterface()) { 73 $tmp = new stub Binding($this, $type);70 $tmp = new stubClassBinding($this, $type); 74 71 $this->addBinding($tmp); 75 72 return $tmp->getInstance(); … … 128 125 $this->updateIndex(); 129 126 } 130 131 127 return ($this->getBinding($type, $name) != null); 132 128 } trunk/src/test/php/net/stubbles/ioc/IOCTestSuite.php
r845 r846 30 30 31 31 $this->addTestFile(dirname(__FILE__) . '/injection/stubInjectorBasicTestCase.php'); 32 $this->addTestFile(dirname(__FILE__) . '/injection/stubInjectorConstantTestCase.php'); 32 33 $this->addTestFile(dirname(__FILE__) . '/injection/stubInjectorImplementedByTestCase.php'); 33 34 $this->addTestFile(dirname(__FILE__) . '/injection/stubInjectorNamedTestCase.php');
