Changeset 353

Show
Ignore:
Timestamp:
03/09/07 20:05:28 (2 years ago)
Author:
mikey
Message:

added class name of injectible to exception message
allow injections with prefix stub but do not use this prefix on setter methods and properties

Files:

Legend:

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

    r313 r353  
    1616 * @package     stubbles 
    1717 * @subpackage  ioc_injection 
     18 * @todo        think about a public static addPrefix() method that will  
     19 *              discard prefixes from injections for method and property names 
    1820 */ 
    1921class stubInjectAnnotation extends stubAbstractAnnotation 
     
    6870        foreach ($this->injections as $injection) { 
    6971            if ($injectionProvider->hasInjection($injection) == false) { 
    70                 throw new stubInjectionException('Injectible needs ' . $injection . ' but it was not given in list of injected classes.'); 
     72                $className = ($injectible instanceof stubObject) ? ($injectible->getClassName()) : (get_class($injectible)); 
     73                throw new stubInjectionException('Injectible ' . $className . ' needs ' . $injection . ' but it was not given in list of injected classes.'); 
    7174            } 
    7275             
    73             $methodName = 'set' . ucfirst($injection); 
     76            if (substr($injection, 0, 4) == 'stub') { 
     77                $injectionShort = substr($injection, 4); 
     78            } else { 
     79                $injectionShort = $injection; 
     80            } 
     81             
     82            $methodName = 'set' . ucfirst($injectionShort); 
    7483            if ($refClass->hasMethod($methodName) == true && $refClass->getMethod($methodName)->isPublic() == true) { 
    7584                try { 
     
    7887                    throw new stubInjectionException('Could not inject ' . $injection . ': ' . $re->getMessage()); 
    7988                } 
    80             } elseif ($refClass->hasProperty($injection) == true && $refClass->getProperty($injection)->isPublic() == true) { 
    81                 $refClass->getProperty($injection)->setValue($injectible, $injectionProvider->getInjection($injection)); 
     89            } elseif ($refClass->hasProperty($injectionShort) == true && $refClass->getProperty($injectionShort)->isPublic() == true) { 
     90                $refClass->getProperty($injectionShort)->setValue($injectible, $injectionProvider->getInjection($injection)); 
    8291            } elseif ($refClass->hasMethod('__set') == true && $refClass->getMethod('__set')->isPublic() == true) { 
    8392                $refClass->getMethod('__set')->invoke($injectible, $injection, $injectionProvider->getInjection($injection)); 
    8493            } else { 
    85                 throw new stubInjectionException('Given injectible has no method or property that accepts injection ' . $injection); 
     94                $className = ($injectible instanceof stubObject) ? ($injectible->getClassName()) : (get_class($injectible)); 
     95                throw new stubInjectionException('Injectible ' . $className . ' has no method or property that accepts injection ' . $injection); 
    8696            } 
    8797        } 
  • trunk/src/test/php/net/stubbles/ioc/injection/stubInjectAnnotationTestCase.php

    r308 r353  
    1111class TestInjection2 extends stubBaseObject {} 
    1212class TestInjection3 extends stubBaseObject {} 
     13class stubTestInjection4 extends stubBaseObject {} 
    1314/** 
    1415 * class for testing injections 
     
    4546} 
    4647/** 
     48 * class for testing injections 
     49 * 
     50 * @Inject(stubTestInjection4:stubTestInjection5) 
     51 */ 
     52class TestInjectible3 
     53{ 
     54    protected $TestInjection4; 
     55    public $TestInjection5; 
     56     
     57    public function setTestInjection4($i) 
     58    { 
     59        $this->TestInjection4 = $i; 
     60    } 
     61     
     62    public function getTestInjection4() 
     63    { 
     64        return $this->TestInjection4; 
     65    } 
     66} 
     67/** 
    4768 * Test for net.stubbles.ioc.injection.stubInjectAnnotation 
    4869 * 
     
    6889        $this->injectionMap->addInjection('TestInjection2', new TestInjection2()); 
    6990        $this->injectionMap->addInjection('TestInjection3', new TestInjection3()); 
     91        $this->injectionMap->addInjection('stubTestInjection4', new stubTestInjection4()); 
     92        $this->injectionMap->addInjection('stubTestInjection5', new stubTestInjection4()); 
    7093    } 
    7194    /** 
     
    131154        stubInjectAnnotation::factory($this->injectionMap, 'foo'); 
    132155    } 
     156     
     157    public function testStubPrefix() 
     158    { 
     159        $testInjectible = new TestInjectible3(); 
     160        stubInjectAnnotation::factory($this->injectionMap, $testInjectible); 
     161        $test4a = $this->injectionMap->getInjection('stubTestInjection4'); 
     162        $test4b = $testInjectible->getTestInjection4(); 
     163        $this->assertReference($test4a, $test4b); 
     164        $test5a = $this->injectionMap->getInjection('stubTestInjection5'); 
     165        $test5b = $testInjectible->TestInjection5; 
     166        $this->assertReference($test5a, $test5b); 
     167    } 
    133168} 
    134169?>