Changeset 810
- Timestamp:
- 08/13/07 22:17:42 (1 year ago)
- Files:
-
- trunk/experiments/people/schst/ioc/simple.php (modified) (4 diffs)
- trunk/experiments/people/schst/ioc/singleton.php (added)
- trunk/src/main/php/net/stubbles/ioc/injection/stubBinder.php (modified) (1 diff)
- trunk/src/main/php/net/stubbles/ioc/injection/stubBinding.php (modified) (7 diffs)
- trunk/src/main/php/net/stubbles/ioc/injection/stubBindingScope.php (added)
- trunk/src/main/php/net/stubbles/ioc/injection/stubBindingScopeSingleton.php (added)
- trunk/src/main/php/net/stubbles/ioc/injection/stubBindingScopes.php (added)
- trunk/src/main/php/net/stubbles/ioc/injection/stubInjector.php (modified) (1 diff)
- trunk/src/main/php/net/stubbles/stubClassLoader.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/experiments/people/schst/ioc/simple.php
r809 r810 30 30 } 31 31 32 class Window { 33 } 34 32 35 class Goodyear implements Tire { 33 36 … … 51 54 protected $tire; 52 55 protected $driver; 56 protected $window; 53 57 54 58 /** … … 70 74 public function setDriver(Person $driver) { 71 75 $this->driver = $driver; 76 } 77 78 /** 79 * Set the window 80 * 81 * @param Window $concrete 82 * @Inject 83 */ 84 public function setWindow(Window $window) { 85 $this->window = $window; 72 86 } 73 87 … … 95 109 $binder->bind('Person')->to('Schst'); 96 110 111 // This will bind the class 'Window' to 'Window' 112 // It could be ommitted, as this will be done by implicit binding 113 $binder->bind('Window'); 114 97 115 $vehicle = $injector->getInstance('Vehicle'); 98 116 $vehicle->getDriver()->sayHello(); trunk/src/main/php/net/stubbles/ioc/injection/stubBinder.php
r809 r810 16 16 'net.stubbles.ioc.injection.stubBinding', 17 17 'net.stubbles.ioc.injection.stubInjector', 18 'net.stubbles.ioc.injection.stubBindingScope', 18 19 'net.stubbles.ioc.injection.annotations.stubInjectAnnotation'); 19 20 /** trunk/src/main/php/net/stubbles/ioc/injection/stubBinding.php
r809 r810 16 16 * @package stubbles 17 17 * @subpackage ioc_injection 18 * @todo Introduce providers that create the concrete instances 18 19 */ 19 20 class stubBinding { … … 41 42 42 43 /** 44 * Scope of the binding 45 * 46 * @var stubBindingScope 47 */ 48 protected $scope = null; 49 50 /** 43 51 * Create a new binding 44 52 * … … 49 57 $this->injector = $injector; 50 58 $this->key = $key; 59 $this->impl = $key; 51 60 } 52 61 … … 61 70 62 71 /** 72 * Get the scope of the binding 73 * 74 * @return stubBindingScope 75 */ 76 public function getScope() { 77 return $this->scope; 78 } 79 80 /** 63 81 * Set the concrete implementation 64 82 * … … 67 85 public function to($impl) { 68 86 $this->impl = $impl; 87 return $this; 88 } 89 90 /** 91 * Set the scope 92 * 93 * @param stubBindingScope $scope 94 */ 95 public function in(stubBindingScope $scope) { 96 $this->scope = $scope; 97 return $this; 69 98 } 70 99 … … 82 111 $constructor = $this->impl->getConstructor(); 83 112 if ($constructor === null) { 84 return $this->impl->newInstance(); 85 } 86 87 $paramValues = array(); 88 if (!$constructor->hasAnnotation('Inject')) { 89 $instance = $this->impl->newInstanceArgs($paramValues); 113 $instance = $this->impl->newInstance(); 114 } elseif (!$constructor->hasAnnotation('Inject')) { 115 $instance = $this->impl->newInstance(); 90 116 } else { 117 $paramValues = array(); 91 118 foreach ($constructor->getParameters() as $param) { 92 119 $class = $param->getClass(); … … 97 124 98 125 foreach ($this->impl->getMethods() as $method) { 126 if (strncmp($method->getName(), '__', 2) === 0) { 127 continue; 128 } 99 129 if (!$method->isPublic()) { 100 130 continue; trunk/src/main/php/net/stubbles/ioc/injection/stubInjector.php
r808 r810 33 33 * Get an instance 34 34 * 35 * @param string $key 36 * @return object 35 * @param string $key 36 * @return object 37 * @todo build an index of the bindings 37 38 */ 38 39 public function getInstance($key) { 39 40 foreach ($this->bindings as $binding) { 40 41 if ($binding->getKey() === $key) { 41 return $binding->getInstance(); 42 $scope = $binding->getScope(); 43 if ($scope != null) { 44 return $scope->getInstance($key, $binding); 45 } else { 46 return $binding->getInstance(); 47 } 42 48 } 49 } 50 51 // try implicit binding 52 // TODO: Find a useful location for this code 53 $clazz = new stubReflectionClass($key); 54 if (!$clazz->isInterface()) { 55 $tmp = new stubBinding($this, $key); 56 $this->bindings[] = $tmp; 57 return $tmp->getInstance(); 43 58 } 44 59 throw new stubException('No binding for ' . $key . ' defined'); trunk/src/main/php/net/stubbles/stubClassLoader.php
r808 r810 210 210 } 211 211 212 if (( @include $uri) == false) {212 if ((include $uri) == false) { 213 213 throw new stubClassNotFoundException($fqClassName); 214 214 }
