Changeset 307

Show
Ignore:
Timestamp:
02/28/07 17:26:33 (2 years ago)
Author:
mikey
Message:

reworked net.stubbles.ioc.injection

Files:

Legend:

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

    r251 r307  
    55 * @author      Frank Kleine <mikey@stubbles.net> 
    66 * @package     stubbles 
    7  * @subpackage  ioc 
     7 * @subpackage  ioc_injection 
    88 */ 
    99stubClassLoader::load('net.stubbles.reflection.reflection', 
    10                       'net.stubbles.ioc.injection.InjectionException', 
    11                       'net.stubbles.ioc.injection.InjectionMap' 
     10                      'net.stubbles.ioc.injection.stubInjectionException', 
     11                      'net.stubbles.ioc.injection.stubInjectionMap' 
    1212); 
    1313/** 
     
    1515 * 
    1616 * @package     stubbles 
    17  * @subpackage  ioc 
     17 * @subpackage  ioc_injection 
    1818 */ 
    19 class Inject extends stubAbstractAnnotation 
     19class stubInjectAnnotation extends stubAbstractAnnotation 
    2020{ 
    2121    /** 
     
    6060     * 
    6161     * @param   stubReflectionClass  $refClass      the reflection class for $injectible 
    62      * @param   InjectionMap         $injectionMap  map of values to inject 
     62     * @param   stubInjectionMap     $injectionMap  map of values to inject 
    6363     * @param   object               $injectible    the class to inject into 
    6464     * @throws  InjectionException 
    6565     */ 
    66     protected function handleInjections(stubReflectionClass $refClass, InjectionMap $injectionMap, $injectible) 
     66    protected function handleInjections(stubReflectionClass $refClass, stubInjectionMap $injectionMap, $injectible) 
    6767    { 
    6868        foreach ($this->injections as $injection) { 
    69             if (isset($injectionMap[$injection]) == false) { 
    70                 throw new InjectionException('Injectible needs ' . $injection . ' but it was not given in list of injected classes.'); 
     69            if ($injectionMap->hasInjection($injection) == false) { 
     70                throw new stubInjectionException('Injectible needs ' . $injection . ' but it was not given in list of injected classes.'); 
    7171            } 
    7272             
     
    7474            if ($refClass->hasMethod($methodName) == true && $refClass->getMethod($methodName)->isPublic() == true) { 
    7575                try { 
    76                     $refClass->getMethod($methodName)->invoke($injectible, $injectionMap[$injection]); 
     76                    $refClass->getMethod($methodName)->invoke($injectible, $injectionMap->getInjection($injection)); 
    7777                } catch (ReflectionException $re) { 
    78                     throw new InjectionException('Could not inject ' . $injection . ': ' . $re->getMessage()); 
     78                    throw new stubInjectionException('Could not inject ' . $injection . ': ' . $re->getMessage()); 
    7979                } 
    8080            } elseif ($refClass->hasProperty($injection) == true && $refClass->getProperty($injection)->isPublic() == true) { 
    81                 $refClass->getProperty($injection)->setValue($injectible, $injectionMap[$injection]); 
     81                $refClass->getProperty($injection)->setValue($injectible, $injectionMap->getInjection($injection)); 
    8282            } elseif ($refClass->hasMethod('__set') == true && $refClass->getMethod('__set')->isPublic() == true) { 
    83                 $refClass->getMethod('__set')->invoke($injectible, $injection, $injectionMap[$injection]); 
     83                $refClass->getMethod('__set')->invoke($injectible, $injection, $injectionMap->getInjection($injection)); 
    8484            } else { 
    85                 throw new InjectionException('Given injectible has no method or property that accepts injection ' . $injection); 
     85                throw new stubInjectionException('Given injectible has no method or property that accepts injection ' . $injection); 
    8686            } 
    8787        } 
     
    9595     * @throws  InjectionException 
    9696     */ 
    97     public static function factory(InjectionMap $injectionMap, $injectible) 
     97    public static function factory(stubInjectionMap $injectionMap, $injectible) 
    9898    { 
    9999        if (is_object($injectible) == false) { 
    100             throw new InjectionException('Given injectible is not an object. Can only inject into objects.'); 
     100            throw new stubInjectionException('Given injectible is not an object. Can only inject into objects.'); 
    101101        } 
    102102         
  • trunk/src/main/php/net/stubbles/ioc/injection/stubInjectionException.php

    r66 r307  
    1313 * @subpackage  ioc 
    1414 */ 
    15 class InjectionException extends stubException 
     15class stubInjectionException extends stubException 
    1616{ 
    1717     
  • trunk/src/main/php/net/stubbles/ioc/injection/stubInjectionMap.php

    r65 r307  
    55 * @author      Frank Kleine <mikey@stubbles.net> 
    66 * @package     stubbles 
    7  * @subpackage  ioc 
     7 * @subpackage  ioc_injection 
    88 */ 
    99/** 
     
    1111 * 
    1212 * @package     stubbles 
    13  * @subpackage  ioc 
     13 * @subpackage  ioc_injection 
    1414 */ 
    15 class InjectionMap extends stubBaseObject implements ArrayAccess, Countable, IteratorAggregate 
     15class stubInjectionMap extends stubBaseObject 
    1616{ 
    1717    /** 
    1818     * list of injections with their keywords 
    1919     * 
    20      * @var  array<string,mixed
     20     * @var  array<string,stubObject
    2121     */ 
    2222    protected $map = array(); 
     
    2525     * Add an injection 
    2626     * 
    27      * @param  string  $offset  keyword for injection 
    28      * @param  mixed   $value   value to inject 
     27     * @param  string      $key    keyword for injection 
     28     * @param  stubObject  $class  class to inject 
    2929     */ 
    30     public function offsetSet($offset, $value
     30    public function addInjection($key, stubObject $class
    3131    { 
    32         $this->map[$offset] = $value
     32        $this->map[$key] = $class
    3333    } 
    3434     
     
    3636     * Remove the injection 
    3737     * 
    38      * @param  string  $offset  keyword under which injection is stored 
     38     * @param  string  $key  keyword under which injection is stored 
    3939     */ 
    40     public function offsetUnset($offset
     40    public function removeInjection($key
    4141    { 
    42         if (isset($this->map[$offset]) == true) { 
    43             unset($this->map[$offset]); 
     42        if (isset($this->map[$key]) == true) { 
     43            unset($this->map[$key]); 
    4444        } 
    4545    } 
     
    4848     * Returns the injection for the given keyword 
    4949     * 
    50      * @param   string  $offset  keyword under which injection is stored 
    51      * @return  mixed 
     50     * @param   string      $key  keyword under which injection is stored 
     51     * @return  stubObject 
    5252     */ 
    53     public function offsetGet($offset
     53    public function getInjection($key
    5454    { 
    55         if (isset($this->map[$offset]) == true) { 
    56             return $this->map[$offset]; 
     55        if (isset($this->map[$key]) == true) { 
     56            return $this->map[$key]; 
    5757        } 
    5858         
     
    6363     * Check whether an injection for the given keyword exists 
    6464     * 
    65      * @param   string  $offset  keyword under which injection is stored 
     65     * @param   string  $key  keyword under which injection is stored 
    6666     * @return  bool    true if an injection for given keyword exists 
    6767     */ 
    68     public function offsetExists ($offset
     68    public function hasInjection($key
    6969    { 
    70         return isset($this->map[$offset]); 
    71     } 
    72      
    73     /** 
    74      * count the amount of injections 
    75      * 
    76      * @return  int 
    77      */ 
    78     public function count() 
    79     { 
    80         return count($this->map); 
    81     } 
    82      
    83     /** 
    84      * Returns an iterator for the map 
    85      * 
    86      * @return  ArrayIterator 
    87      */ 
    88     public function getIterator() 
    89     { 
    90         return new ArrayIterator($this->map); 
     70        return isset($this->map[$key]); 
    9171    } 
    9272} 
  • trunk/src/test/php/net/stubbles/ioc/IOCTestSuite.php

    r30 r307  
    2121    { 
    2222        $this->TestSuite('All ioc tests'); 
    23         $this->addTestFile(dirname(__FILE__) . '/injection/InjectTestCase.php'); 
    24         $this->addTestFile(dirname(__FILE__) . '/injection/InjectionMapTestCase.php'); 
     23        $this->addTestFile(dirname(__FILE__) . '/injection/stubInjectTestCase.php'); 
     24        $this->addTestFile(dirname(__FILE__) . '/injection/stubInjectionMapTestCase.php'); 
    2525    } 
    2626} 
  • trunk/src/test/php/net/stubbles/ioc/injection/stubInjectTestCase.php

    r98 r307  
    11<?php 
    22/** 
    3  * Test for ioc.injection.Inject 
     3 * Test for net.stubbles.ioc.injection.stubInjectAnnotation 
    44 * 
    55 * @author      Frank Kleine <mikey@stubbles.net> 
    66 * @package     stubbles 
    7  * @subpackage  reflection_test 
     7 * @subpackage  ioc_injection_test 
    88 */ 
    9 stubClassLoader::load('net.stubbles.ioc.injection.Inject'); 
    10 class TestInjection1 {} 
    11 class TestInjection2 {} 
    12 class TestInjection3 {} 
     9stubClassLoader::load('net.stubbles.ioc.injection.stubInjectAnnotation'); 
     10class TestInjection1 extends stubBaseObject {} 
     11class TestInjection2 extends stubBaseObject {} 
     12class TestInjection3 extends stubBaseObject {} 
    1313/** 
    1414 * class for testing injections 
     
    4545} 
    4646/** 
    47  * Test for ioc.injection.Inject 
     47 * Test for net.stubbles.ioc.injection.stubInjectAnnotation 
    4848 * 
    4949 * @package     stubbles 
    50  * @subpackage  reflection_test 
     50 * @subpackage  ioc_injection_test 
    5151 */ 
    52 class InjectTestCase extends UnitTestCase 
     52class stubInjectTestCase extends UnitTestCase 
    5353{ 
    5454    /** 
    5555     * injection map to use for tests 
    5656     * 
    57      * @var  InjectionMap 
     57     * @var  stubInjectionMap 
    5858     */ 
    5959    protected $injectionMap; 
     
    6464    public function setUp() 
    6565    { 
    66         $this->injectionMap = new InjectionMap(); 
    67         $this->injectionMap['TestInjection1'] = new TestInjection1(); 
    68         $this->injectionMap['TestInjection2'] = new TestInjection2(); 
    69         $this->injectionMap['TestInjection3'] = new TestInjection3(); 
     66        $this->injectionMap = new stubInjectionMap(); 
     67        $this->injectionMap->addInjection('TestInjection1', new TestInjection1()); 
     68        $this->injectionMap->addInjection('TestInjection2', new TestInjection2()); 
     69        $this->injectionMap->addInjection('TestInjection3', new TestInjection3()); 
    7070    } 
    7171    /** 
     
    7474    public function testInjections() 
    7575    { 
    76         $inject = new Inject(); 
     76        $inject = new stubInjectAnnotation(); 
    7777        $inject->setValue('TestInjection1:TestInjection2:TestInjection3'); 
    7878        $this->assertEqual($inject->getInjections(), array('TestInjection1', 'TestInjection2', 'TestInjection3')); 
     
    8585    { 
    8686        $testInjectible = new TestInjectible1(); 
    87         Inject::factory($this->injectionMap, $testInjectible); 
    88         $this->assertReference($this->injectionMap['TestInjection1'], $testInjectible->getTestInjection1()); 
    89         $this->assertReference($this->injectionMap['TestInjection2'], $testInjectible->TestInjection2); 
    90         $this->assertReference($this->injectionMap['TestInjection3'], $testInjectible->TestInjection3); 
     87        stubInjectAnnotation::factory($this->injectionMap, $testInjectible); 
     88        $test1a = $this->injectionMap->getInjection('TestInjection1'); 
     89        $test1b = $testInjectible->getTestInjection1(); 
     90        $this->assertReference($test1a, $test1b); 
    9191    } 
    9292     
     
    9797    { 
    9898        $testInjectible = new TestInjectible1(); 
    99         $this->expectException('InjectionException'); 
    100         Inject::factory(new InjectionMap(), $testInjectible); 
     99        $this->expectException('stubInjectionException'); 
     100        stubInjectAnnotation::factory(new stubInjectionMap(), $testInjectible); 
    101101    } 
    102102     
     
    107107    { 
    108108        $testInjectible = new TestInjectible2(); 
    109         $this->expectException('InjectionException'); 
    110         Inject::factory($this->injectionMap, $testInjectible); 
     109        $this->expectException('stubInjectionException'); 
     110        stubInjectAnnotation::factory($this->injectionMap, $testInjectible); 
    111111    } 
    112112     
     
    120120        $testInjectible->expectNever('setTestInjection1'); 
    121121        $testInjectible->expectNever('__set'); 
    122         Inject::factory($this->injectionMap, $testInjectible); 
     122        stubInjectAnnotation::factory($this->injectionMap, $testInjectible); 
    123123    } 
    124124     
     
    128128    public function testHandlingWrongInjectible() 
    129129    { 
    130         $this->expectException('InjectionException'); 
    131         Inject::factory($this->injectionMap, 'foo'); 
     130        $this->expectException('stubInjectionException'); 
     131        stubInjectAnnotation::factory($this->injectionMap, 'foo'); 
    132132    } 
    133133} 
  • trunk/src/test/php/net/stubbles/ioc/injection/stubInjectionMapTestCase.php

    r98 r307  
    11<?php 
    22/** 
    3  * Test for ioc.injection.InjectionMap 
     3 * Test for net.stubbles.ioc.injection.stubInjectionMap 
    44 * 
    55 * @author      Frank Kleine <mikey@stubbles.net> 
    66 * @package     stubbles 
    7  * @subpackage  reflection_test 
     7 * @subpackage  ioc_injection_test 
    88 */ 
    9 stubClassLoader::load('net.stubbles.ioc.injection.InjectionMap'); 
     9stubClassLoader::load('net.stubbles.ioc.injection.stubInjectionMap'); 
    1010/** 
    11  * Test for ioc.injection.InjectionMap 
     11 * Test for net.stubbles.ioc.injection.stubInjectionMap 
    1212 * 
    1313 * @package     stubbles 
    14  * @subpackage  reflection_test 
     14 * @subpackage  ioc_injection_test 
    1515 */ 
    16 class InjectionMapTestCase extends UnitTestCase 
     16class stubInjectionMapTestCase extends UnitTestCase 
    1717{ 
    1818    /** 
    1919     * instance to test 
    2020     * 
    21      * @var  InjectionMap 
     21     * @var  stubInjectionMap 
    2222     */ 
    2323    protected $injectionMap; 
     24    /** 
     25     * the base object used for putting into the map 
     26     * 
     27     * @var  stubBaseObject 
     28     */ 
     29    protected $baseObject; 
    2430     
    2531    /** 
     
    2834    public function setUp() 
    2935    { 
    30         $this->injectionMap = new InjectionMap(); 
    31         $this->injectionMap['foo'] = 'baz'; 
     36        $this->injectionMap = new stubInjectionMap(); 
     37        $this->baseObject   = new stubBaseObject(); 
     38        $this->injectionMap->addInjection('foo', $this->baseObject); 
    3239    } 
    3340     
     
    3744    public function testSetUnset() 
    3845    { 
    39         $this->assertEqual($this->injectionMap['foo'], 'baz'); 
    40         $this->assertTrue(isset($this->injectionMap['foo'])); 
    41         $this->assertEqual(count($this->injectionMap['foo']), 1); 
    42         unset($this->injectionMap['foo']); 
    43         $this->assertNull($this->injectionMap['foo']); 
    44         $this->assertFalse(isset($this->injectionMap['foo'])); 
    45         $this->assertEqual(count($this->injectionMap['foo']), 0); 
    46     } 
    47      
    48     /** 
    49      * test that you can iterator over the map 
    50      */ 
    51     public function testIterator() 
    52     { 
    53         foreach ($this->injectionMap as $key => $value) { 
    54             $this->assertEqual($key, 'foo'); 
    55             $this->assertEqual($value, 'baz'); 
    56         } 
     46        $this->assertNull($this->injectionMap->getInjection('bar')); 
     47        $this->assertFalse($this->injectionMap->hasInjection('bar')); 
     48        $this->assertEqual($this->injectionMap->getInjection('foo'), $this->baseObject); 
     49        $this->assertTrue($this->injectionMap->hasInjection('foo')); 
     50        $this->injectionMap->removeInjection('foo'); 
     51        $this->assertNull($this->injectionMap->getInjection('foo')); 
     52        $this->assertFalse($this->injectionMap->hasInjection('foo')); 
    5753    } 
    5854}