Changeset 809
- Timestamp:
- 08/13/07 21:27:49 (1 year ago)
- Files:
-
- trunk/experiments/people/schst/ioc/simple.php (modified) (4 diffs)
- trunk/src/main/php/net/stubbles/ioc/injection/annotations/stubInjectAnnotation.php (modified) (9 diffs)
- trunk/src/main/php/net/stubbles/ioc/injection/stubBinder.php (modified) (1 diff)
- trunk/src/main/php/net/stubbles/ioc/injection/stubBinding.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/experiments/people/schst/ioc/simple.php
r808 r809 16 16 } 17 17 18 interface Person { 19 public function sayHello(); 20 } 21 22 class Schst implements Person { 23 public function sayHello() { 24 echo "My name is schst.\n"; 25 } 26 } 27 18 28 interface Tire { 19 29 public function rotate(); … … 22 32 class Goodyear implements Tire { 23 33 34 /** 35 * No injection done here.... 36 * 37 */ 38 public function __construct($size = 20) { 39 } 40 24 41 public function rotate() { 25 echo "I'm d oing something.\n";42 echo "I'm driving with Goodyear tires.\n"; 26 43 } 27 44 } … … 33 50 class Car implements Vehicle { 34 51 protected $tire; 52 protected $driver; 35 53 54 /** 55 * Create a new car 56 * 57 * @param Tire $tire 58 * @Inject 59 */ 36 60 public function __construct(Tire $tire) { 37 61 $this->tire = $tire; 62 } 63 64 /** 65 * Set the driver 66 * 67 * @param Person $driver 68 * @Inject 69 */ 70 public function setDriver(Person $driver) { 71 $this->driver = $driver; 72 } 73 74 /** 75 * Get the driver 76 * 77 * @return Person 78 */ 79 public function getDriver() { 80 return $this->driver; 38 81 } 39 82 … … 50 93 $binder->bind('Tire')->to('Goodyear'); 51 94 $binder->bind('Vehicle')->to('Car'); 95 $binder->bind('Person')->to('Schst'); 52 96 53 $foo = $injector->getInstance('Vehicle'); 54 $foo->moveForward(); 97 $vehicle = $injector->getInstance('Vehicle'); 98 $vehicle->getDriver()->sayHello(); 99 $vehicle->moveForward(); 55 100 ?> trunk/src/main/php/net/stubbles/ioc/injection/annotations/stubInjectAnnotation.php
r808 r809 16 16 * @package stubbles 17 17 * @subpackage ioc_injection 18 * @todo think about a public static addPrefix() method that will 18 * @todo think about a public static addPrefix() method that will 19 19 * discard prefixes from injections for method and property names 20 20 */ … … 27 27 */ 28 28 protected $injections = array(); 29 29 30 30 /** 31 31 * sets the list of class names to inject … … 37 37 $this->injections = explode(':', $value); 38 38 } 39 39 40 40 /** 41 41 * Returns the target of the annotation as bitmap. … … 45 45 public function getAnnotationTarget() 46 46 { 47 return stubAnnotation::TARGET_ CLASS;47 return stubAnnotation::TARGET_ALL; 48 48 } 49 49 50 50 /** 51 51 * returns the list of injections … … 57 57 return $this->injections; 58 58 } 59 59 60 60 /** 61 61 * processes all injections … … 73 73 throw new stubInjectionException('Injectible ' . $className . ' needs ' . $injection . ' but it was not given in list of injected classes.'); 74 74 } 75 75 76 76 if (substr($injection, 0, 4) == 'stub') { 77 77 $injectionShort = substr($injection, 4); … … 79 79 $injectionShort = $injection; 80 80 } 81 81 82 82 $methodName = 'set' . ucfirst($injectionShort); 83 83 if ($refClass->hasMethod($methodName) == true && $refClass->getMethod($methodName)->isPublic() == true) { … … 97 97 } 98 98 } 99 99 100 100 /** 101 101 * static call to hide all injection details … … 110 110 throw new stubInjectionException('Given injectible is not an object. Can only inject into objects.'); 111 111 } 112 112 113 113 $refClass = new stubReflectionClass(get_class($injectible)); 114 114 if ($refClass->hasAnnotation('Inject') == true) { trunk/src/main/php/net/stubbles/ioc/injection/stubBinder.php
r808 r809 15 15 stubClassLoader::load('net.stubbles.reflection.stubReflectionClass', 16 16 'net.stubbles.ioc.injection.stubBinding', 17 'net.stubbles.ioc.injection.stubInjector'); 17 'net.stubbles.ioc.injection.stubInjector', 18 'net.stubbles.ioc.injection.annotations.stubInjectAnnotation'); 18 19 /** 19 20 * Binder for the IoC functionality. trunk/src/main/php/net/stubbles/ioc/injection/stubBinding.php
r808 r809 86 86 87 87 $paramValues = array(); 88 foreach ($constructor->getParameters() as $param) { 89 $class = $param->getClass(); 90 $paramValues[] = $this->injector->getInstance($class->getName()); 88 if (!$constructor->hasAnnotation('Inject')) { 89 $instance = $this->impl->newInstanceArgs($paramValues); 90 } else { 91 foreach ($constructor->getParameters() as $param) { 92 $class = $param->getClass(); 93 $paramValues[] = $this->injector->getInstance($class->getName()); 94 } 95 $instance = $this->impl->newInstanceArgs($paramValues); 91 96 } 92 return $this->impl->newInstanceArgs($paramValues); 97 98 foreach ($this->impl->getMethods() as $method) { 99 if (!$method->isPublic()) { 100 continue; 101 } 102 if (!$method->hasAnnotation('Inject')) { 103 continue; 104 } 105 $paramValues = array(); 106 foreach ($method->getParameters() as $param) { 107 $class = $param->getClass(); 108 $paramValues[] = $this->injector->getInstance($class->getName()); 109 } 110 $method->invokeArgs($instance, $paramValues); 111 } 112 return $instance; 93 113 } 94 114 }
