Changeset 809

Show
Ignore:
Timestamp:
08/13/07 21:27:49 (1 year ago)
Author:
schst
Message:

Check, whether @Inject annotation is present,
first version of setter injection (still pre-alpha)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/experiments/people/schst/ioc/simple.php

    r808 r809  
    1616} 
    1717 
     18interface Person { 
     19    public function sayHello(); 
     20} 
     21 
     22class Schst implements Person { 
     23    public function sayHello() { 
     24        echo "My name is schst.\n"; 
     25    } 
     26} 
     27 
    1828interface Tire { 
    1929    public function rotate(); 
     
    2232class Goodyear implements Tire { 
    2333 
     34    /** 
     35     * No injection done here.... 
     36     * 
     37     */ 
     38    public function __construct($size = 20) { 
     39    } 
     40 
    2441    public function rotate() { 
    25         echo "I'm doing something.\n"; 
     42        echo "I'm driving with Goodyear tires.\n"; 
    2643    } 
    2744} 
     
    3350class Car implements Vehicle { 
    3451    protected $tire; 
     52    protected $driver; 
    3553 
     54    /** 
     55     * Create a new car 
     56     * 
     57     * @param Tire $tire 
     58     * @Inject 
     59     */ 
    3660    public function __construct(Tire $tire) { 
    3761        $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; 
    3881    } 
    3982 
     
    5093$binder->bind('Tire')->to('Goodyear'); 
    5194$binder->bind('Vehicle')->to('Car'); 
     95$binder->bind('Person')->to('Schst'); 
    5296 
    53 $foo = $injector->getInstance('Vehicle'); 
    54 $foo->moveForward(); 
     97$vehicle = $injector->getInstance('Vehicle'); 
     98$vehicle->getDriver()->sayHello(); 
     99$vehicle->moveForward(); 
    55100?> 
  • trunk/src/main/php/net/stubbles/ioc/injection/annotations/stubInjectAnnotation.php

    r808 r809  
    1616 * @package     stubbles 
    1717 * @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 
    1919 *              discard prefixes from injections for method and property names 
    2020 */ 
     
    2727     */ 
    2828    protected $injections = array(); 
    29      
     29 
    3030    /** 
    3131     * sets the list of class names to inject 
     
    3737        $this->injections = explode(':', $value); 
    3838    } 
    39      
     39 
    4040    /** 
    4141     * Returns the target of the annotation as bitmap. 
     
    4545    public function getAnnotationTarget() 
    4646    { 
    47         return stubAnnotation::TARGET_CLASS
     47        return stubAnnotation::TARGET_ALL
    4848    } 
    49      
     49 
    5050    /** 
    5151     * returns the list of injections 
     
    5757        return $this->injections; 
    5858    } 
    59      
     59 
    6060    /** 
    6161     * processes all injections 
     
    7373                throw new stubInjectionException('Injectible ' . $className . ' needs ' . $injection . ' but it was not given in list of injected classes.'); 
    7474            } 
    75              
     75 
    7676            if (substr($injection, 0, 4) == 'stub') { 
    7777                $injectionShort = substr($injection, 4); 
     
    7979                $injectionShort = $injection; 
    8080            } 
    81              
     81 
    8282            $methodName = 'set' . ucfirst($injectionShort); 
    8383            if ($refClass->hasMethod($methodName) == true && $refClass->getMethod($methodName)->isPublic() == true) { 
     
    9797        } 
    9898    } 
    99      
     99 
    100100    /** 
    101101     * static call to hide all injection details 
     
    110110            throw new stubInjectionException('Given injectible is not an object. Can only inject into objects.'); 
    111111        } 
    112          
     112 
    113113        $refClass = new stubReflectionClass(get_class($injectible)); 
    114114        if ($refClass->hasAnnotation('Inject') == true) { 
  • trunk/src/main/php/net/stubbles/ioc/injection/stubBinder.php

    r808 r809  
    1515stubClassLoader::load('net.stubbles.reflection.stubReflectionClass', 
    1616                      'net.stubbles.ioc.injection.stubBinding', 
    17                       'net.stubbles.ioc.injection.stubInjector'); 
     17                      'net.stubbles.ioc.injection.stubInjector', 
     18                      'net.stubbles.ioc.injection.annotations.stubInjectAnnotation'); 
    1819/** 
    1920 * Binder for the IoC functionality. 
  • trunk/src/main/php/net/stubbles/ioc/injection/stubBinding.php

    r808 r809  
    8686 
    8787        $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); 
    9196        } 
    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; 
    93113    } 
    94114}