Changeset 1181

Show
Ignore:
Timestamp:
12/23/07 12:00:33 (8 months ago)
Author:
mikey
Message:

fixed bug #102

Files:

Legend:

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

    r1180 r1181  
    5252            return $binding->getInstance(); 
    5353        } 
    54  
    55         // check for default implementation 
    56         $clazz = new stubReflectionClass($type); 
    57         if ($clazz->hasAnnotation('ImplementedBy') === true) { 
    58             $implementedBy = $clazz->getAnnotation('ImplementedBy'); 
    59             $tmp = new stubClassBinding($this, $type); 
    60             $tmp->to($implementedBy->getDefaultImplementation()); 
    61             $this->addBinding($tmp); 
    62             return $tmp->getInstance(); 
    63         } 
    64  
    65         // try implicit binding 
    66         // TODO: Find a useful location for this code 
    67         if ($clazz->isInterface() === false) { 
    68             $tmp = new stubClassBinding($this, $type); 
    69             $this->addBinding($tmp); 
    70             return $tmp->getInstance(); 
    71         } 
    7254         
    7355        throw new stubBindingException('No binding for ' . $type . ' defined'); 
     
    9072     * @param   string       $name 
    9173     * @return  stubBinding 
    92      * @todo    This method should also take care of implicit bindings 
    9374     */ 
    9475    protected function getBinding($type, $name = null) 
     
    10384        if (isset($bindingIndex[$type]) === true) { 
    10485            return $bindingIndex[$type]; 
     86        } 
     87         
     88        // check for default implementation 
     89        $typeClass = new stubReflectionClass($type); 
     90        if ($typeClass->hasAnnotation('ImplementedBy') === true) { 
     91            $implementedBy = $typeClass->getAnnotation('ImplementedBy'); 
     92            $binding       = new stubClassBinding($this, $type); 
     93            $binding->to($implementedBy->getDefaultImplementation()); 
     94            $this->addBinding($binding); 
     95            return $binding; 
     96        } 
     97 
     98        // try implicit binding 
     99        if ($typeClass->isInterface() === false) { 
     100            $binding = new stubClassBinding($this, $type); 
     101            $binding->to($typeClass); 
     102            $this->addBinding($binding); 
     103            return $binding; 
    105104        } 
    106105         
  • trunk/src/test/php/net/stubbles/ioc/stubInjectorBasicTestCase.php

    r1005 r1181  
    11<?php 
    22/** 
    3  * Test for net.stubbles.ioc.stubInjector 
     3 * Test for net::stubbles::ioc::stubInjector. 
    44 * 
    55 * @author      Stephan Schmidt <schst@stubbles.net> 
    6  * @package     stubbles 
    7  * @subpackage  ioc_injection_test 
     6 * @author      Frank Kleine <mikey@stubbles.net> 
     7 * @package     stubbles 
     8 * @subpackage  ioc_test 
    89 */ 
    910stubClassLoader::load('net.stubbles.ioc.stubBinder'); 
    10  
    11 interface stubInjectorTestCase_Tire { 
     11/** 
     12 * Helper interface for injection and binding tests. 
     13 * 
     14 * @package     stubbles 
     15 * @subpackage  ioc_test 
     16 */ 
     17interface stubInjectorTestCase_Tire 
     18
     19    /** 
     20     * rotates the tires 
     21     * 
     22     * @return  string 
     23     */ 
    1224    public function rotate(); 
    1325} 
    14  
    15 class stubInjectorTestCase_Goodyear implements stubInjectorTestCase_Tire { 
    16     public function rotate() { 
     26/** 
     27 * Helper class for injection and binding tests. 
     28 * 
     29 * @package     stubbles 
     30 * @subpackage  ioc_test 
     31 */ 
     32class stubInjectorTestCase_Goodyear implements stubInjectorTestCase_Tire 
     33
     34    /** 
     35     * rotates the tires 
     36     * 
     37     * @return  string 
     38     */ 
     39    public function rotate() 
     40    { 
    1741        return "I'm driving with Goodyear tires."; 
    1842    } 
    1943} 
    20  
    21 class stubInjectorTestCase_ImplicitDependency { 
    22     protected $goodyear; 
    23  
    24     /** 
    25      * @Inject 
    26      */ 
    27     public function __construct(stubInjectorTestCase_Goodyear $goodyear) { 
    28         $this->goodyear = $goodyear; 
    29     } 
    30  
    31     public function getGoodyear() { 
    32         return $this->goodyear; 
    33     } 
    34 
    35  
    36  
    37 interface stubInjectorTestCase_Vehicle { 
     44/** 
     45 * Helper class to test implicit binding with concrete class names. 
     46 * 
     47 * @package     stubbles 
     48 * @subpackage  ioc_test 
     49 */ 
     50class stubInjectorTestCase_ImplicitDependency 
     51
     52    /** 
     53     * instance from constructor injection 
     54     * 
     55     * @var  stubInjectorTestCase_Goodyear 
     56     */ 
     57    protected $goodyearByConstructor; 
     58    /** 
     59     * instance from setter injection 
     60     * 
     61     * @var  stubInjectorTestCase_Goodyear 
     62     */ 
     63    protected $goodyearBySetter; 
     64 
     65    /** 
     66     * constructor 
     67     * 
     68     * @param  stubInjectorTestCase_Goodyear  $goodyear 
     69     * @Inject 
     70     */ 
     71    public function __construct(stubInjectorTestCase_Goodyear $goodyear) 
     72    { 
     73        $this->goodyearByConstructor = $goodyear; 
     74    } 
     75 
     76    /** 
     77     * setter 
     78     * 
     79     * @param  stubInjectorTestCase_Goodyear  $goodyear 
     80     * @Inject 
     81     */ 
     82    public function setGoodyear(stubInjectorTestCase_Goodyear $goodyear) 
     83    { 
     84        $this->goodyearBySetter = $goodyear; 
     85    } 
     86 
     87    /** 
     88     * returns the instance from constructor injection 
     89     * 
     90     * @return  stubInjectorTestCase_Goodyear 
     91     */ 
     92    public function getGoodyearByConstructor() 
     93    { 
     94        return $this->goodyearByConstructor; 
     95    } 
     96 
     97    /** 
     98     * returns the instance from setter injection 
     99     * 
     100     * @return  stubInjectorTestCase_Goodyear 
     101     */ 
     102    public function getGoodyearBySetter() 
     103    { 
     104        return $this->goodyearBySetter; 
     105    } 
     106
     107/** 
     108 * Helper class to test implicit binding related to bug #102. 
     109 * 
     110 * @package     stubbles 
     111 * @subpackage  ioc_test 
     112 * @link        http://stubbles.net/ticket/102 
     113 */ 
     114class stubInjectorTestCase_ImplicitDependencyBug102 
     115
     116    /** 
     117     * instance from setter injection 
     118     * 
     119     * @var  stubInjectorTestCase_Goodyear 
     120     */ 
     121    protected $goodyearBySetter; 
     122 
     123    /** 
     124     * setter 
     125     * 
     126     * @param  stubInjectorTestCase_Goodyear  $goodyear 
     127     * @Inject 
     128     */ 
     129    public function setGoodyear(stubInjectorTestCase_Goodyear $goodyear) 
     130    { 
     131        $this->goodyearBySetter = $goodyear; 
     132    } 
     133 
     134    /** 
     135     * returns the instance from setter injection 
     136     * 
     137     * @return  stubInjectorTestCase_Goodyear 
     138     */ 
     139    public function getGoodyearBySetter() 
     140    { 
     141        return $this->goodyearBySetter; 
     142    } 
     143
     144/** 
     145 * Another helper interface for injection and binding tests. 
     146 * 
     147 * @package     stubbles 
     148 * @subpackage  ioc_test 
     149 */ 
     150interface stubInjectorTestCase_Vehicle 
     151
     152    /** 
     153     * moves the vehicle forward 
     154     * 
     155     * @return  string 
     156     */ 
    38157    public function moveForward(); 
    39158} 
    40  
    41 class stubInjectorTestCase_Car implements stubInjectorTestCase_Vehicle { 
     159/** 
     160 * Another helper class for injection and binding tests. 
     161 * 
     162 * @package     stubbles 
     163 * @subpackage  ioc_test 
     164 */ 
     165class stubInjectorTestCase_Car implements stubInjectorTestCase_Vehicle 
     166
     167    /** 
     168     * injected tire instance 
     169     * 
     170     * @var  stubInjectorTestCase_Tire 
     171     */ 
    42172    public $tire; 
    43173 
     
    45175     * Create a new car 
    46176     * 
     177     * @param  stubInjectorTestCase_Tire  $tire 
     178     * @Inject 
     179     */ 
     180    public function __construct(stubInjectorTestCase_Tire $tire) 
     181    { 
     182        $this->tire = $tire; 
     183    } 
     184 
     185    /** 
     186     * moves the vehicle forward 
     187     * 
     188     * @return  string 
     189     */ 
     190    public function moveForward() 
     191    { 
     192        return $this->tire->rotate(); 
     193    } 
     194} 
     195/** 
     196 * Another helper class for injection and binding tests. 
     197 * 
     198 * @package     stubbles 
     199 * @subpackage  ioc_test 
     200 */ 
     201class stubInjectorTestCase_Bike implements stubInjectorTestCase_Vehicle 
     202{ 
     203    /** 
     204     * injected tire instance 
     205     * 
     206     * @var  stubInjectorTestCase_Tire 
     207     */ 
     208    public $tire; 
     209 
     210    /** 
     211     * sets the tire 
     212     * 
     213     * @param  stubInjectorTestCase_Tire  $tire 
     214     * @Inject 
     215     */ 
     216    public function setTire(stubInjectorTestCase_Tire $tire) 
     217    { 
     218        $this->tire = $tire; 
     219    } 
     220 
     221    /** 
     222     * moves the vehicle forward 
     223     * 
     224     * @return  string 
     225     */ 
     226    public function moveForward() 
     227    { 
     228        return $this->tire->rotate(); 
     229    } 
     230} 
     231/** 
     232 * Another helper interface for injection and binding tests. 
     233 * 
     234 * @package     stubbles 
     235 * @subpackage  ioc_test 
     236 */ 
     237interface stubInjectorTestCase_Roof 
     238{ 
     239    /** 
     240     * method to open the roof 
     241     */ 
     242    public function open(); 
     243    /** 
     244     * method to close the roof 
     245     */ 
     246    public function close(); 
     247} 
     248/** 
     249 * Another helper class for injection and binding tests. 
     250 * 
     251 * @package     stubbles 
     252 * @subpackage  ioc_test 
     253 */ 
     254class stubInjectorTestCase_Convertible implements stubInjectorTestCase_Vehicle 
     255{ 
     256    /** 
     257     * injected tire instance 
     258     * 
     259     * @var  stubInjectorTestCase_Tire 
     260     */ 
     261    public $tire; 
     262    /** 
     263     * injected roof instance 
     264     * 
     265     * @var   stubInjectorTestCase_Roof 
     266     */ 
     267    public $roof; 
     268 
     269    /** 
     270     * sets the tire 
     271     * 
    47272     * @param Tire $tire 
    48273     * @Inject 
    49274     */ 
    50     public function __construct(stubInjectorTestCase_Tire $tire) { 
     275    public function setTire(stubInjectorTestCase_Tire $tire) 
     276    { 
    51277        $this->tire = $tire; 
    52278    } 
    53279 
    54     public function moveForward() { 
     280    /** 
     281     * sets the root 
     282     * 
     283     * @param  stubInjectorTestCase_Roof  $roof 
     284     * @Inject(optinal=true) 
     285     */ 
     286    public function setRoof(stubInjectorTestCase_Roof $roof) 
     287    { 
     288        $this->roof = $roof; 
     289    } 
     290 
     291    /** 
     292     * moves the vehicle forward 
     293     * 
     294     * @return  string 
     295     */ 
     296    public function moveForward() 
     297    { 
    55298        return $this->tire->rotate(); 
    56299    } 
    57300} 
    58301 
    59  
    60 class stubInjectorTestCase_Bike implements stubInjectorTestCase_Vehicle { 
    61  
    62     public $tire; 
    63  
    64     /** 
    65      * Set the tire 
    66      * 
    67      * @param Tire $tire 
    68      * @Inject 
    69      */ 
    70     public function setTire(stubInjectorTestCase_Tire $tire) { 
    71         $this->tire = $tire; 
    72     } 
    73  
    74     public function moveForward() { 
    75         return $this->tire->rotate(); 
    76     } 
    77 
    78  
    79 interface stubInjectorTestCase_Roof { 
    80     public function open(); 
    81     public function close(); 
    82 
    83  
    84 class stubInjectorTestCase_Convertible implements stubInjectorTestCase_Vehicle { 
    85  
    86     public $tire; 
    87     public $roof; 
    88  
    89     /** 
    90      * Set the tire 
    91      * 
    92      * @param Tire $tire 
    93      * @Inject 
    94      */ 
    95     public function setTire(stubInjectorTestCase_Tire $tire) { 
    96         $this->tire = $tire; 
    97     } 
    98  
    99     /** 
    100      * Set the root 
    101      * 
    102      * @param Roof $roof 
    103      * @Inject(optinal=true) 
    104      */ 
    105     public function setRoof(stubInjectorTestCase_Roof $roof) { 
    106         $this->roof = $roof; 
    107     } 
    108  
    109     public function moveForward() { 
    110         return $this->tire->rotate(); 
    111     } 
    112 
    113  
    114 /** 
    115  * Test for net.stubbles.ioc.stubInjector 
    116  * 
    117  * @package     stubbles 
    118  * @subpackage  ioc_injection_test 
     302/** 
     303 * Test for net::stubbles::ioc::stubInjector. 
     304 * 
     305 * @package     stubbles 
     306 * @subpackage  ioc_test 
    119307 */ 
    120308class stubInjectorBasicTestCase extends UnitTestCase 
     
    252440    public function testImplicitBinding() 
    253441    { 
    254         $binder = new stubBinder(); 
    255         $injector = $binder->getInjector(); 
    256  
     442        $binder   = new stubBinder(); 
     443        $injector = $binder->getInjector(); 
    257444        $goodyear = $injector->getInstance('stubInjectorTestCase_Goodyear'); 
    258445        $this->assertIsA($goodyear, 'stubInjectorTestCase_Goodyear'); 
     
    264451    public function testImplicitBindingAsDependency() 
    265452    { 
    266         $binder = new stubBinder(); 
    267         $injector = $binder->getInjector(); 
    268  
    269         $obj = $injector->getInstance('stubInjectorTestCase_ImplicitDependency'); 
     453        $binder   = new stubBinder(); 
     454        $injector = $binder->getInjector(); 
     455        $obj      = $injector->getInstance('stubInjectorTestCase_ImplicitDependency'); 
    270456        $this->assertIsA($obj, 'stubInjectorTestCase_ImplicitDependency'); 
    271         $this->assertIsA($obj->getGoodyear(), 'stubInjectorTestCase_Goodyear'); 
     457        $this->assertIsA($obj->getGoodyearByConstructor(), 'stubInjectorTestCase_Goodyear'); 
     458        $this->assertIsA($obj->getGoodyearBySetter(), 'stubInjectorTestCase_Goodyear'); 
     459    } 
     460 
     461    /** 
     462     * test method for bug #102 
     463     * 
     464     * @link  http://stubbles.net/ticket/102 
     465     */ 
     466    public function testBug102() 
     467    { 
     468        $obj      = new stubInjectorTestCase_ImplicitDependencyBug102(); 
     469        $binder   = new stubBinder(); 
     470        $injector = $binder->getInjector(); 
     471        $injector->handleInjections($obj); 
     472        $this->assertIsA($obj->getGoodyearBySetter(), 'stubInjectorTestCase_Goodyear'); 
    272473    } 
    273474}