Changeset 825

Show
Ignore:
Timestamp:
08/14/07 21:00:12 (11 months ago)
Author:
schst
Message:

Added @Named annotation, all classes now are stubObjects

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/ioc/injection/stubBinder.php

    r821 r825  
    2020                      'net.stubbles.ioc.injection.annotations.stubInjectAnnotation', 
    2121                      'net.stubbles.ioc.injection.annotations.stubSingletonAnnotation', 
     22                      'net.stubbles.ioc.injection.annotations.stubNamedAnnotation', 
    2223                      'net.stubbles.ioc.injection.annotations.stubImplementedByAnnotation'); 
    2324/** 
     
    3031 * @subpackage  ioc_injection 
    3132 */ 
    32 class stubBinder
     33class stubBinder extends stubBaseObject
    3334 
    3435    /** 
  • trunk/src/main/php/net/stubbles/ioc/injection/stubBinding.php

    r823 r825  
    1818 * @todo        Introduce providers that create the concrete instances 
    1919 */ 
    20 class stubBinding
     20class stubBinding extends stubBaseObject
    2121 
    2222    /** 
     
    2828 
    2929    /** 
    30      * The key for this binding 
     30     * The type for this binding 
    3131     * 
    3232     * @var string 
    3333     */ 
    34     protected $key = null; 
     34    protected $type = null; 
    3535 
    3636    /** 
     
    4040     */ 
    4141    protected $impl = null; 
     42 
     43    /** 
     44     * Annotated with a name 
     45     * 
     46     * @var string 
     47     */ 
     48    protected $name = null; 
    4249 
    4350    /** 
     
    5259     * 
    5360     * @param stubInjector $injector 
    54      * @param string $key 
     61     * @param string $type 
    5562     */ 
    56     public function __construct($injector, $key) { 
     63    public function __construct($injector, $type) { 
    5764        $this->injector = $injector; 
    58         $this->key = $key; 
    59         $this->impl = $key; 
    60     } 
    61  
    62     /** 
    63      * Get the key 
    64      * 
    65      * @return string 
    66      */ 
    67     public function getKey() { 
    68         return $this->key; 
     65        $this->type     = $type; 
     66        $this->impl     = $type; 
    6967    } 
    7068 
     
    8280     * 
    8381     * @param stubReflectionClass|string $impl 
     82     * @return stubBinding 
    8483     */ 
    8584    public function to($impl) { 
     
    9291     * 
    9392     * @param stubBindingScope $scope 
     93     * @return stubBinding 
    9494     */ 
    9595    public function in(stubBindingScope $scope) { 
    9696        $this->scope = $scope; 
     97        return $this; 
     98    } 
     99 
     100    /** 
     101     * Set the name of the injection 
     102     * 
     103     * @param string $name 
     104     * @return stubBinding 
     105     */ 
     106    public function named($name) { 
     107        $this->name = $name; 
    97108        return $this; 
    98109    } 
     
    113124            } 
    114125        } 
    115  
    116126        if ($this->scope != null) { 
    117             return $this->scope->getInstance($this->key, $this); 
     127            return $this->scope->getInstance($this->type, $this); 
    118128        } 
    119129        return $this->create(); 
     
    158168            foreach ($method->getParameters() as $param) { 
    159169                $class = $param->getClass(); 
    160                 $paramValues[] = $this->injector->getInstance($class->getName()); 
     170                $name  = null; 
     171                if ($method->hasAnnotation('Named')) { 
     172                    $name = $method->getAnnotation('Named')->getName(); 
     173                } 
     174                $paramValues[] = $this->injector->getInstance($class->getName(), $name); 
    161175            } 
    162176            $method->invokeArgs($instance, $paramValues); 
     
    164178        return $instance; 
    165179    } 
     180 
     181    /** 
     182     * Creates a unique key for this binding 
     183     */ 
     184    public function getKey() { 
     185        if ($this->name === null) { 
     186            return $this->type; 
     187        } 
     188        return $this->type . '#' . $this->name; 
     189    } 
     190 
     191 
    166192} 
    167193?> 
  • trunk/src/main/php/net/stubbles/ioc/injection/stubBindingScope.php

    r810 r825  
    1616 * @subpackage  ioc_injection 
    1717 */ 
    18 interface stubBindingScope
     18interface stubBindingScope extends stubObject
    1919    public function getInstance($key, stubBinding $binding); 
    2020 
  • trunk/src/main/php/net/stubbles/ioc/injection/stubBindingScopeSingleton.php

    r821 r825  
    1919 * @subpackage  ioc_injection 
    2020 */ 
    21 class stubBindingScopeSingleton implements stubBindingScope { 
     21class stubBindingScopeSingleton extends stubBaseObject implements stubBindingScope { 
    2222 
    2323    /** 
  • trunk/src/main/php/net/stubbles/ioc/injection/stubBindingScopes.php

    r810 r825  
    1919 * @subpackage  ioc_injection 
    2020 */ 
    21 class stubBindingScopes
     21class stubBindingScopes extends stubBaseObject
    2222 
    2323    /** 
  • trunk/src/main/php/net/stubbles/ioc/injection/stubInjector.php

    r822 r825  
    2121 * @subpackage  ioc_injection 
    2222 */ 
    23 class stubInjector
     23class stubInjector extends stubBaseObject
    2424 
    2525    /** 
     
    4040     * Get an instance 
    4141     * 
    42      * @param   string $key 
     42     * @param   string $type 
    4343     * @return  object 
    44      * @todo    build an index of the bindings 
    4544     */ 
    46     public function getInstance($key) { 
     45    public function getInstance($type, $name = null) { 
    4746        if (!empty($this->bindings)) { 
    4847            $this->updateIndex(); 
    4948        } 
    50  
    51         if (isset($this->bindingIndex[$key])) { 
    52             $binding = $this->bindingIndex[$key]; 
     49        $binding = $this->getBinding($type, $name); 
     50        if ($binding != null) { 
    5351            $scope = $binding->getScope(); 
    5452            if ($scope != null) { 
    55                 return $scope->getInstance($key, $binding); 
     53                return $scope->getInstance($type, $binding); 
    5654            } else { 
    5755                return $binding->getInstance(); 
     
    6058 
    6159        // check for default implementation 
    62         $clazz = new stubReflectionClass($key); 
     60        $clazz = new stubReflectionClass($type); 
    6361        if ($clazz->hasAnnotation('ImplementedBy')) { 
    6462            $implementedBy = $clazz->getAnnotation('ImplementedBy'); 
    65             $tmp = new stubBinding($this, $key); 
     63            $tmp = new stubBinding($this, $type); 
    6664            $tmp->to($implementedBy->getDefaultImplementation()); 
    6765            $this->addBinding($tmp); 
     
    7270        // TODO: Find a useful location for this code 
    7371        if (!$clazz->isInterface()) { 
    74             $tmp = new stubBinding($this, $key); 
     72            $tmp = new stubBinding($this, $type); 
    7573            $this->addBinding($tmp); 
    7674            return $tmp->getInstance(); 
    7775        } 
    78         throw new stubException('No binding for ' . $key . ' defined'); 
     76        throw new stubException('No binding for ' . $type . ' defined'); 
    7977    } 
    8078 
     
    8482    protected function updateIndex() { 
    8583        foreach ($this->bindings as $binding) { 
    86             $this->bindingIndex[$this->createKey($binding)] = $binding; 
     84            $this->bindingIndex[$binding->getKey()] = $binding; 
    8785        } 
    8886        $this->bindings = array(); 
    89     } 
    90  
    91     /** 
    92      * Creates a unique key for a binding 
    93      * 
    94      * @param stubBinding $binding 
    95      * @todo  Key must also contain @Named() annotation value 
    96      */ 
    97     protected function createKey(stubBinding $binding) { 
    98         $interface = $binding->getKey(); 
    99         if ($interface instanceof stubReflectionClass) { 
    100             $interface = $interface->getName(); 
    101         } 
    102         return $interface; 
    10387    } 
    10488 
     
    11195        $this->bindings[] = $binding; 
    11296    } 
     97 
     98    protected function getBinding($type, $name = null) { 
     99        if ($name !== null) { 
     100            if (isset($this->bindingIndex[$type.'#'.$name])) { 
     101                return $this->bindingIndex[$type.'#'.$name]; 
     102            } 
     103        } 
     104        if (isset($this->bindingIndex[$type])) { 
     105            return $this->bindingIndex[$type]; 
     106        } 
     107        return null; 
     108    } 
    113109} 
    114110?>