Changeset 855

Show
Ignore:
Timestamp:
08/20/07 21:39:23 (11 months ago)
Author:
schst
Message:

Implemented ticket #79 (ability to use custom providers), fixed bug with scopes that did receive the interface instead of the actual implementation, some refactoring, fixed Singleton test case

Files:

Legend:

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

    r852 r855  
    1414 */ 
    1515interface stubBindingScope extends stubObject { 
    16     public function getInstance($key, stubBinding $binding); 
     16    public function getInstance(stubReflectionClass $type, stubBinding $binding); 
    1717 
    1818} 
  • trunk/src/main/php/net/stubbles/ioc/stubBindingScopeSingleton.php

    r852 r855  
    3232     * @return object 
    3333     */ 
    34     public function getInstance($key, stubBinding $binding) { 
     34    public function getInstance(stubReflectionClass $type, stubBinding $binding) { 
     35        $key = $type->getName(); 
    3536        if (!isset($this->instances[$key])) { 
    3637            $this->instances[$key] = $binding->create(); 
  • trunk/src/main/php/net/stubbles/ioc/stubClassBinding.php

    r852 r855  
    77 * @subpackage  ioc_injection 
    88 */ 
    9 stubClassLoader::load('net.stubbles.ioc.exceptions.stubBindingException'); 
     9stubClassLoader::load('net.stubbles.ioc.exceptions.stubBindingException', 
     10                      'net.stubbles.ioc.stubDefaultInjectionProvider'); 
    1011/** 
    1112 * Binding to bind an interface to an implementation 
     
    6061 
    6162    /** 
     63     * The provider to use for this binding 
     64     * 
     65     * @var stubInjectionProvider 
     66     */ 
     67    protected $provider = null; 
     68 
     69    /** 
    6270     * Create a new binding 
    6371     * 
     
    103111 
    104112    /** 
     113     * Set the provider that should be used to create 
     114     * instances for this binding. 
     115     * 
     116     * This cannot be used in conjuction with the 
     117     * 'toInstance()' method. 
     118     * 
     119     * @param stubInjectionProvider 
     120     * @return stubBinding 
     121     */ 
     122    public function toProvider(stubInjectionProvider $provider) { 
     123        $this->provider = $provider; 
     124        return $this; 
     125    } 
     126 
     127    /** 
    105128     * Set the scope 
    106129     * 
     
    125148 
    126149    /** 
    127      * Get the instance for this binding 
     150     * Get the instance for this binding. 
     151     * 
     152     * This method will at first look for an existing 
     153     * instance in the scope and then create the instance 
     154     * using the create() method. 
    128155     * 
    129156     * @return object 
     157     * @see    create() 
    130158     */ 
    131159    public function getInstance() { 
     
    144172        } 
    145173        if ($this->scope != null) { 
    146             return $this->scope->getInstance($this->type, $this); 
     174            return $this->scope->getInstance($this->impl, $this); 
    147175        } 
    148176        return $this->create(); 
    149  
    150177    } 
    151178 
     
    154181     * 
    155182     * @return object 
    156      * @todo   Decide, where to put this functionality 
    157183     */ 
    158184    public function create() { 
     185        if ($this->provider === null) { 
     186            $this->provider = new stubDefaultInjectionProvider($this->injector); 
     187        } 
    159188        if (is_string($this->impl)) { 
    160189            $this->impl = new stubReflectionClass($this->impl); 
    161190        } 
    162         $constructor = $this->impl->getConstructor(); 
    163         if ($constructor === null) { 
    164             $instance = $this->impl->newInstance(); 
    165         } elseif (!$constructor->hasAnnotation('Inject')) { 
    166             $instance = $this->impl->newInstance(); 
    167         } else { 
    168             $paramValues = array(); 
    169             foreach ($constructor->getParameters() as $param) { 
    170                 $class = $param->getClass(); 
    171                 $paramValues[] = $this->injector->getInstance($class->getName()); 
    172             } 
    173             $instance = $this->impl->newInstanceArgs($paramValues); 
    174         } 
    175  
    176         foreach ($this->impl->getMethods() as $method) { 
    177             if (strncmp($method->getName(), '__', 2) === 0) { 
    178                 continue; 
    179             } 
    180             if (!$method->isPublic()) { 
    181                 continue; 
    182             } 
    183             if (!$method->hasAnnotation('Inject')) { 
    184                 continue; 
    185             } 
    186             $paramValues = array(); 
    187             foreach ($method->getParameters() as $param) { 
    188                 $class = $param->getClass(); 
    189                 if ($class !== null) { 
    190                     $type = $class->getName(); 
    191                 } else { 
    192                     $type = stubConstantBinding::TYPE; 
    193                 } 
    194  
    195                 $name  = null; 
    196                 if ($method->hasAnnotation('Named')) { 
    197                     $name = $method->getAnnotation('Named')->getName(); 
    198                 } 
    199                 if (!$this->injector->hasBinding($type, $name)) { 
    200                     if ($method->getAnnotation('Inject')->isOptional()) { 
    201                         continue 2; 
    202                     } 
    203                     throw new stubBindingException("Could not create instance of {$this->type}. No binding for type {$type} specified."); 
    204                 } 
    205                 $paramValues[] = $this->injector->getInstance($type, $name); 
    206             } 
    207             $method->invokeArgs($instance, $paramValues); 
    208         } 
    209         return $instance; 
     191        return $this->provider->get($this->impl); 
    210192    } 
    211193 
  • trunk/src/main/php/net/stubbles/ioc/stubInjectionProvider.php

    r852 r855  
    55 * features of Stubbles. 
    66 * 
    7  * @author      Frank Kleine <mikey@stubbles.net> 
    87 * @author      Stephan Schmidt <schst@stubbles.net> 
    98 * @package     stubbles 
     
    2221{ 
    2322    /** 
    24      * Returns the injection for the given keyword 
     23     * Returns the object 
    2524     * 
    26      * @param   string      $key  keyword under which injection is stored 
    27      * @return  stubObject 
     25     * @return  object 
    2826     */ 
    29     public function getInjection($key); 
    30  
    31     /** 
    32      * Check whether an injection for the given keyword exists 
    33      * 
    34      * @param   string  $key  keyword under which injection is stored 
    35      * @return  bool    true if an injection for given keyword exists 
    36      */ 
    37     public function hasInjection($key); 
     27    public function get(stubReflectionClass $type); 
    3828} 
    3929?> 
  • trunk/src/main/php/net/stubbles/ioc/stubInjector.php

    r852 r855  
    4747        $binding = $this->getBinding($type, $name); 
    4848        if ($binding != null) { 
    49             $scope = $binding->getScope(); 
    50             if ($scope != null) { 
    51                 return $scope->getInstance($type, $binding); 
    52             } else { 
    53                 return $binding->getInstance(); 
    54             } 
     49            return $binding->getInstance(); 
    5550        } 
    5651 
  • trunk/src/test/php/net/stubbles/ioc/stubInjectorSingletonTestCase.php

    r854 r855  
    121121        $this->assertIsA($slot, 'stubInjectorSingletonTestCase_SlotMachine'); 
    122122        $this->assertIsA($slot->number1, 'stubInjectorSingletonTestCase_Number'); 
    123         $this->assertIsA($slot->number1, 'stubInjectorSingletonTestCase_Random'); 
     123        $this->assertIsA($slot->number1, 'stubInjectorSingletonTestCase_RandomSingleton'); 
    124124        $this->assertIsA($slot->number2, 'stubInjectorSingletonTestCase_Number'); 
    125         $this->assertIsA($slot->number2, 'stubInjectorSingletonTestCase_Random'); 
     125        $this->assertIsA($slot->number2, 'stubInjectorSingletonTestCase_RandomSingleton'); 
    126126        $this->assertIdentical($slot->number1, $slot->number2); 
    127127    }