Changeset 846

Show
Ignore:
Timestamp:
08/19/07 16:05:30 (1 year ago)
Author:
schst
Message:

Added bindConstant(), some refactoring and cleanup

Files:

Legend:

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

    r808 r846  
    33 * Injection handling bootstrap file. 
    44 * 
    5  * @author      Frank Kleine <mikey@stubbles.net> 
     5 * @author      Stephan Schmidt <schst@stubbles.net> 
    66 * @package     stubbles 
    77 * @subpackage  ioc_injection 
    88 */ 
    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 ); 
     9stubClassLoader::load('net.stubbles.ioc.injection.stubBinder'); 
    1410?> 
  • trunk/src/main/php/net/stubbles/ioc/injection/stubBinder.php

    r844 r846  
    66 * the same injector. 
    77 * 
    8  * WARNING: This is experimental and far from finished! 
    9  *          Huge refactorings will follow! 
    10  * 
    118 * @author      Stephan Schmidt <schst@stubbles.net> 
    129 * @package     stubbles 
     
    1512stubClassLoader::load('net.stubbles.reflection.stubReflectionClass', 
    1613                      'net.stubbles.ioc.injection.stubBinding', 
     14                      'net.stubbles.ioc.injection.stubClassBinding', 
     15                      'net.stubbles.ioc.injection.stubConstantBinding', 
    1716                      'net.stubbles.ioc.injection.stubInjector', 
    1817                      'net.stubbles.ioc.injection.stubBindingScope', 
     
    5756     * 
    5857     * @param string $interface 
    59      * @return stubBinding 
     58     * @return stubClassBinding 
    6059     */ 
    6160    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); 
    6373        $this->injector->addBinding($binding); 
    6474        return $binding; 
  • trunk/src/main/php/net/stubbles/ioc/injection/stubBinding.php

    r844 r846  
    22/** 
    33 * Binding to bind an interface to an implementation 
    4  * 
    5  * WARNING: This is experimental and far from finished! 
    6  *          Huge refactorings will follow! 
    74 * 
    85 * @author      Stephan Schmidt <schst@stubbles.net> 
     
    1815 * @todo        Introduce providers that create the concrete instances 
    1916 */ 
    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     } 
     17interface stubBinding extends stubObject { 
    11718 
    11819    /** 
     
    12223     * @return stubBinding 
    12324     */ 
    124     public function named($name) { 
    125         $this->name = $name; 
    126         return $this; 
    127     } 
     25    public function named($name); 
    12826 
    12927    /** 
     
    13230     * @return object 
    13331     */ 
    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(); 
    20933 
    21034    /** 
    21135     * Creates a unique key for this binding 
    21236     */ 
    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(); 
    21945} 
    22046?> 
  • trunk/src/main/php/net/stubbles/ioc/injection/stubBindingScope.php

    r825 r846  
    22/** 
    33 * Interface for all scopes 
    4  * 
    5  * WARNING: This is experimental and far from finished! 
    6  *          Huge refactorings will follow! 
    74 * 
    85 * @author      Stephan Schmidt <schst@stubbles.net> 
  • trunk/src/main/php/net/stubbles/ioc/injection/stubBindingScopeSingleton.php

    r825 r846  
    22/** 
    33 * Scope for singletons 
    4  * 
    5  * WARNING: This is experimental and far from finished! 
    6  *          Huge refactorings will follow! 
    74 * 
    85 * @author      Stephan Schmidt <schst@stubbles.net> 
  • trunk/src/main/php/net/stubbles/ioc/injection/stubBindingScopes.php

    r825 r846  
    22/** 
    33 * All built-in scopes 
    4  * 
    5  * WARNING: This is experimental and far from finished! 
    6  *          Huge refactorings will follow! 
    74 * 
    85 * @author      Stephan Schmidt <schst@stubbles.net> 
  • trunk/src/main/php/net/stubbles/ioc/injection/stubInjectionProvider.php

    r344 r846  
    11<?php 
    22/** 
    3  * Map for injections. 
     3 * Interface for providers that create objects 
     4 * that are required by the Inversion of Control 
     5 * features of Stubbles. 
    46 * 
    57 * @author      Frank Kleine <mikey@stubbles.net> 
     8 * @author      Stephan Schmidt <schst@stubbles.net> 
    69 * @package     stubbles 
    710 * @subpackage  ioc_injection 
    811 */ 
     12 
    913/** 
    10  * Map for injections. 
     14 * Interface for providers that create objects 
     15 * that are required by the Inversion of Control 
     16 * features of Stubbles. 
    1117 * 
    1218 * @package     stubbles 
     
    2228     */ 
    2329    public function getInjection($key); 
    24      
     30 
    2531    /** 
    2632     * Check whether an injection for the given keyword exists 
  • trunk/src/main/php/net/stubbles/ioc/injection/stubInjector.php

    r844 r846  
    44 * 
    55 * Used to create the instances. 
    6  * 
    7  * WARNING: This is experimental and far from finished! 
    8  *          Huge refactorings will follow! 
    96 * 
    107 * @author      Stephan Schmidt <schst@stubbles.net> 
     
    6259        if ($clazz->hasAnnotation('ImplementedBy')) { 
    6360            $implementedBy = $clazz->getAnnotation('ImplementedBy'); 
    64             $tmp = new stubBinding($this, $type); 
     61            $tmp = new stubClassBinding($this, $type); 
    6562            $tmp->to($implementedBy->getDefaultImplementation()); 
    6663            $this->addBinding($tmp); 
     
    7168        // TODO: Find a useful location for this code 
    7269        if (!$clazz->isInterface()) { 
    73             $tmp = new stubBinding($this, $type); 
     70            $tmp = new stubClassBinding($this, $type); 
    7471            $this->addBinding($tmp); 
    7572            return $tmp->getInstance(); 
     
    128125            $this->updateIndex(); 
    129126        } 
    130  
    131127        return ($this->getBinding($type, $name) != null); 
    132128    } 
  • trunk/src/test/php/net/stubbles/ioc/IOCTestSuite.php

    r845 r846  
    3030 
    3131        $this->addTestFile(dirname(__FILE__) . '/injection/stubInjectorBasicTestCase.php'); 
     32        $this->addTestFile(dirname(__FILE__) . '/injection/stubInjectorConstantTestCase.php'); 
    3233        $this->addTestFile(dirname(__FILE__) . '/injection/stubInjectorImplementedByTestCase.php'); 
    3334        $this->addTestFile(dirname(__FILE__) . '/injection/stubInjectorNamedTestCase.php');