Changeset 1370

Show
Ignore:
Timestamp:
02/25/08 18:56:55 (3 months ago)
Author:
mikey
Message:

implemented enhancement #97
todo: increase test coverage, not all use cases are tested
todo: update documentation

Files:

Legend:

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

    r1222 r1370  
    2525 
    2626    /** 
    27      * get the instance for this binding 
     27     * returns the provider that has or creates the required instance 
    2828     * 
    29      * @return  object 
     29     * @return  stubInjectionProvider 
    3030     */ 
    31     public function getInstance(); 
     31    public function getProvider(); 
    3232 
    3333    /** 
  • trunk/src/main/php/net/stubbles/ioc/stubBindingScope.php

    r1222 r1370  
    1919{ 
    2020    /** 
    21      * returns the instance from the scop
     21     * returns the provider that has or creates the required instanc
    2222     * 
    2323     * @param   stubBaseReflectionClass  $type      type of the object 
    2424     * @param   stubBaseReflectionClass  $impl      concrete implementation 
    2525     * @param   stubInjectionProvider    $provider 
    26      * @return  object 
     26     * @return  stubInjectionProvider 
    2727     */ 
    28     public function getInstance(stubBaseReflectionClass $type, stubBaseReflectionClass $impl, stubInjectionProvider $provider); 
     28    public function getProvider(stubBaseReflectionClass $type, stubBaseReflectionClass $impl, stubInjectionProvider $provider); 
    2929} 
    3030?> 
  • trunk/src/main/php/net/stubbles/ioc/stubBindingScopeSingleton.php

    r1296 r1370  
    88 */ 
    99stubClassLoader::load('net::stubbles::ioc::stubBindingScope', 
    10                       'net::stubbles::reflection::stubBaseReflectionClass
     10                      'net::stubbles::ioc::stubValueInjectionProvider
    1111); 
    1212/** 
     
    1919{ 
    2020    /** 
    21      * Instances in this scope 
     21     * instances in this scope 
    2222     * 
    23      * @var  array<string,object
     23     * @var  array<string,stubValueInjectionProvider
    2424     */ 
    2525    protected $instances = array(); 
    2626 
    2727    /** 
    28      * Create the instance, if it has not been created befor
     28     * returns the provider that has or creates the required instanc
    2929     * 
    3030     * @param   stubBaseReflectionClass  $type      type of the object 
    3131     * @param   stubBaseReflectionClass  $impl      concrete implementation 
    3232     * @param   stubInjectionProvider    $provider 
    33      * @return  object 
     33     * @return  stubInjectionProvider 
    3434     */ 
    35     public function getInstance(stubBaseReflectionClass $type, stubBaseReflectionClass $impl, stubInjectionProvider $provider) 
     35    public function getProvider(stubBaseReflectionClass $type, stubBaseReflectionClass $impl, stubInjectionProvider $provider) 
    3636    { 
    3737        $key = $impl->getName(); 
    38         if (!isset($this->instances[$key])) { 
    39             $this->instances[$key] = $provider->get($type); 
     38        if (isset($this->instances[$key]) === false) { 
     39            $this->instances[$key] = new stubValueInjectionProvider($provider->get($type)); 
    4040        } 
    4141         
  • trunk/src/main/php/net/stubbles/ioc/stubClassBinding.php

    r1296 r1370  
    143143 
    144144    /** 
    145      * Get the instance for this binding. 
    146      * 
    147      * This method will at first look for an existing 
    148      * instance in the scope and then create the instance 
    149      * using the injection provider. 
    150      * 
    151      * @return  object 
    152      */ 
    153     public function getInstance() 
    154     { 
    155         if (null !== $this->instance) { 
    156             return $this->instance; 
    157         } 
    158  
    159         if (is_string($this->impl) === true) { 
    160             $this->impl = new stubReflectionClass($this->impl); 
    161         } 
    162  
    163         if (null === $this->scope) { 
    164             if ($this->impl->hasAnnotation('Singleton') === true) { 
    165                 $this->scope = stubBindingScopes::$SINGLETON; 
     145     * returns the provider that has or creates the required instance 
     146     * 
     147     * This method will at first look for an existing instance in the scope and 
     148     * then create the instance using the injection provider. 
     149     * 
     150     * @return  stubInjectionProvider 
     151     */ 
     152    public function getProvider() 
     153    { 
     154        if (null === $this->instance) { 
     155            if (is_string($this->impl) === true) { 
     156                $this->impl = new stubReflectionClass($this->impl); 
    166157            } 
    167         } 
    168  
    169         if (null === $this->provider) { 
    170             $this->provider = new stubDefaultInjectionProvider($this->injector, $this->impl); 
     158     
     159            if (null === $this->scope) { 
     160                if ($this->impl->hasAnnotation('Singleton') === true) { 
     161                    $this->scope = stubBindingScopes::$SINGLETON; 
     162                } 
     163            } 
     164             
     165            if (null === $this->provider) { 
     166                $this->provider = new stubDefaultInjectionProvider($this->injector, $this->impl); 
     167            } 
     168        } else { 
     169            $this->provider = new stubValueInjectionProvider($this->instance); 
    171170        } 
    172171 
    173172        if (null !== $this->scope) { 
    174             return $this->scope->getInstance(new stubReflectionClass($this->type), $this->impl, $this->provider); 
     173            return $this->scope->getProvider(new stubReflectionClass($this->type), $this->impl, $this->provider); 
    175174        } 
    176175         
    177         return $this->provider->get(new stubReflectionClass($this->type))
     176        return $this->provider
    178177    } 
    179178 
  • trunk/src/main/php/net/stubbles/ioc/stubConstantBinding.php

    r1301 r1370  
    77 * @subpackage  ioc 
    88 */ 
    9 stubClassLoader::load('net::stubbles::ioc::exceptions::stubBinding'); 
     9stubClassLoader::load('net::stubbles::ioc::stubBinding', 
     10                      'net::stubbles::ioc::stubValueInjectionProvider' 
     11); 
    1012/** 
    1113 * Binding to bind a property to a constant value. 
     
    3335    protected $name     = null; 
    3436    /** 
    35      * instance this type is bound to 
     37     * provider that holds the binded value 
    3638     * 
    37      * @var  object 
     39     * @var  stubValueInjectionProvider 
    3840     */ 
    39     protected $value    = null
     41    protected $valueProvider
    4042 
    4143    /** 
     
    4648    public function __construct($injector) 
    4749    { 
    48         $this->injector = $injector; 
     50        $this->injector      = $injector; 
     51        $this->valueProvider = new stubValueInjectionProvider(); 
    4952    } 
    5053 
     
    5760    public function to($value) 
    5861    { 
    59         $this->value = $value
     62        $this->valueProvider->setValue($value)
    6063        return $this; 
    6164    } 
     
    7477 
    7578    /** 
    76      * get the instance for this binding 
     79     * returns the provider that has or creates the required instance 
    7780     * 
    78      * @return  object 
     81     * @return  stubInjectionProvider 
    7982     */ 
    80     public function getInstance() 
     83    public function getProvider() 
    8184    { 
    82         return $this->value
     85        return $this->valueProvider
    8386    } 
    8487 
  • trunk/src/main/php/net/stubbles/ioc/stubDefaultInjectionProvider.php

    r1222 r1370  
    99 */ 
    1010stubClassLoader::load('net::stubbles::ioc::stubInjectionProvider', 
    11                       'net::stubbles::ioc::stubInjector', 
    12                       'net::stubbles::reflection::stubBaseReflectionClass' 
     11                      'net::stubbles::ioc::stubInjector' 
    1312); 
    1413/** 
     
    4948 
    5049    /** 
    51      * returns the object 
     50     * returns the value to provide 
    5251     * 
    53      * @param   stubBaseReflectionClass  $type 
    54      * @return  object 
     52     * @param   string  $type 
     53     * @param   string  $name 
     54     * @return  mixed 
    5555     */ 
    56     public function get(stubBaseReflectionClass $type
     56    public function get($type, $name = null
    5757    { 
    5858        $constructor = $this->impl->getConstructor(); 
  • trunk/src/main/php/net/stubbles/ioc/stubInjectionProvider.php

    r1222 r1370  
    88 * @subpackage  ioc 
    99 */ 
    10 stubClassLoader::load('net::stubbles::reflection::stubBaseReflectionClass'); 
    1110/** 
    1211 * Interface for providers that create objects that are required by the 
     
    1918{ 
    2019    /** 
    21      * returns the object 
     20     * returns the value to provide 
    2221     * 
    23      * @param   stubReflectionClass  $type 
    24      * @return  object 
     22     * @param   string  $type 
     23     * @param   string  $name 
     24     * @return  mixed 
    2525     */ 
    26     public function get(stubBaseReflectionClass $type); 
     26    public function get($type, $name = null); 
    2727} 
    2828?> 
  • trunk/src/main/php/net/stubbles/ioc/stubInjector.php

    r1301 r1370  
    5050    { 
    5151        $binding = $this->getBinding($type, $name); 
    52         if (null !== $binding) { 
    53             return $binding->getInstance(); 
     52        if (null === $binding) { 
     53            throw new stubBindingException('No binding for ' . $type . ' defined'); 
    5454        } 
    5555         
    56         throw new stubBindingException('No binding for ' . $type . ' defined'); 
     56        return $binding->getProvider()->get($type, $name); 
    5757    } 
    5858 
  • trunk/src/main/php/net/stubbles/ipo/session/resourcemanager/stubSessionResourceManager.php

    r1311 r1370  
    7272     * are no two interfaces with the same name. 
    7373     * 
    74      * @param   stubBaseReflectionClass  $type 
     74     * @param   string  $type 
     75     * @param   string  $name 
    7576     * @return  object 
    7677     * @see     net::stubbles::ioc::stubInjectionProvider::get() 
    7778     */ 
    78     public function get(stubBaseReflectionClass $type) 
    79     { 
    80         $typeName = $type->getName(); 
     79    public function get($type, $name = null) 
     80    { 
    8181        foreach (array_keys($this->resourceDefinitions) as $interface) { 
    8282            $localName = stubClassLoader::getNonQualifiedClassName($interface); 
    83             if ($localName === $typeName) { 
     83            if ($localName === $type) { 
    8484                return $this->getResource($interface); 
    8585            } 
  • trunk/src/main/php/net/stubbles/rdbms/stubDatabaseConnectionProvider.php

    r1225 r1370  
    3939     * returns the connection to be injected 
    4040     * 
    41      * @param   stubReflectionClass     $type 
     41     * @param   string                  $type 
     42     * @param   string                  $name 
    4243     * @return  stubDatabaseConnection 
    4344     */ 
    44     public function get(stubBaseReflectionClass $type
     45    public function get($type, $name = null
    4546    { 
    4647        return stubDatabaseConnectionPool::getConnection($this->connectionId);