Changeset 1035

Show
Ignore:
Timestamp:
11/12/07 23:30:26 (9 months ago)
Author:
mikey
Message:

enabled event configuration via XML, implements enhancement #37
however it is not possible to stack event dispatcher instances, this must be done by hand if required
additionally the callback and the lazy event listener can only handle classes that can be instantiated without arguments required for the constructor

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/events/stubCallbackListener.php

    r1024 r1035  
    99stubClassLoader::load('net.stubbles.events.stubCallbackException', 
    1010                      'net.stubbles.events.stubEvent', 
    11                       'net.stubbles.events.stubEventListener' 
     11                      'net.stubbles.events.stubEventListener', 
     12                      'net.stubbles.lang.exceptions.stubIllegalArgumentException' 
    1213); 
    1314/** 
     
    2122 * @subpackage  events 
    2223 */ 
    23 class stubCallbackListener extends stubBaseObject implements stubEventListener 
     24class stubCallbackListener extends stubSerializableObject implements stubEventListener 
    2425{ 
    2526    /** 
     
    6667     * @param   string         $method      method of class to callback when listener is notified 
    6768     * @param   bool           $autoRemove  optional  whether listener should be removed if it has handled one event or not 
    68      * @throws  stubCallbackException 
    6969     */ 
    7070    public function __construct($instance, $method, $autoRemove = false) 
    7171    { 
    72         if (is_string($instance) == false && is_object($instance) == false) { 
    73             throw new stubCallbackException('Argument $instance must be a string or an instance of an object.'); 
    74         } 
    75          
    76         if (is_string($method) == false) { 
    77             throw new stubCallbackException('Argument $method must be a string.'); 
    78         } 
    79          
    80         if (is_object($instance) == true) { 
     72        if (is_string($instance) === false && is_object($instance) === false) { 
     73            throw new stubIllegalArgumentException('Argument $instance must be a string or an instance of an object.'); 
     74        } 
     75         
     76        if (is_string($method) === false) { 
     77            throw new stubIllegalArgumentException('Argument $method must be a string.'); 
     78        } 
     79         
     80        if (is_object($instance) === true) { 
    8181            $this->instance = $instance; 
    8282            $this->class    = get_class($instance); 
    8383        } else { 
    84             if (class_exists($instance, false) === false) { 
    85                 throw new stubCallbackException('Class ' . $instance . ' does not exist.'); 
    86             } 
    87              
    8884            $this->class = $instance; 
    8985        } 
     
    186182 
    187183    /** 
     184     * returns name of class to call back 
     185     * 
     186     * @return  string 
     187     */ 
     188    public function getCallbackClassName() 
     189    { 
     190        return $this->class; 
     191    } 
     192 
     193    /** 
     194     * returns name of the method to call back 
     195     * 
     196     * @return  string 
     197     */ 
     198    public function getCallbackMethodName() 
     199    { 
     200        return $this->method; 
     201    } 
     202 
     203    /** 
    188204     * return whether the callback was notified or not 
    189205     * 
     
    192208    public function isNotified() 
    193209    { 
    194         return ((null == $this->event) ? (false) : (true)); 
     210        return ((null === $this->event) ? (false) : (true)); 
    195211    } 
    196212 
     
    211227        } 
    212228         
    213         $refClass = new ReflectionClass($this->class); 
     229        if (class_exists(stubClassLoader::getNonQualifiedClassName($this->class), false) === false) { 
     230            stubClassLoader::load($this->class); 
     231        } 
     232         
     233        $refClass = new ReflectionClass(stubClassLoader::getNonQualifiedClassName($this->class)); 
    214234         
    215235        try { 
     
    246266        } 
    247267    } 
     268 
     269    /** 
     270     * template method to hook into __sleep() 
     271     * 
     272     * @return  array<string>  list of property names that should not be serialized 
     273     */ 
     274    protected function __doSleep() 
     275    { 
     276        return array('event', 'instance', 'refMethod'); 
     277    } 
     278 
    248279} 
    249280?> 
  • trunk/src/main/php/net/stubbles/events/stubEventDispatcher.php

    r984 r1035  
    8383        return self::$instances[$name]; 
    8484    } 
    85      
     85 
    8686    /** 
    8787     * destroy an event dispatcher 
     
    103103        } 
    104104    } 
    105      
     105 
     106    /** 
     107     * returns a list of all instance names 
     108     * 
     109     * @return  array<string> 
     110     */ 
     111    public static function getInstanceNames() 
     112    { 
     113        return array_keys(self::$instances); 
     114    } 
     115 
    106116    /** 
    107117     * nests this as a dispatcher of the parent dispatcher 
     
    113123        $this->parentDispatcher = $eventDispatcher; 
    114124    } 
    115      
     125 
    116126    /** 
    117127     * removes the parent dispatcher 
     
    121131        $this->parentDispatcher = null; 
    122132    } 
    123      
     133 
    124134    /** 
    125135     * checks whether this dispatcher has a parent dispatcher 
     
    131141        return (null !== $this->parentDispatcher); 
    132142    } 
    133      
     143 
    134144    /** 
    135145     * returns the parent dispatcher 
     
    202212         
    203213        return isset($this->listeners[$eventName][get_class($eventListener)]); 
     214    } 
     215 
     216    /** 
     217     * returns the list of event listeners registered for given event 
     218     * 
     219     * @param   string  $eventName 
     220     * @return  array<stubEventListener> 
     221     */ 
     222    public function getListenerForEvent($eventName) 
     223    { 
     224        if (isset($this->listeners[$eventName]) === true) { 
     225            return $this->listeners[$eventName]; 
     226        } 
     227         
     228        return array(); 
     229    } 
     230 
     231    /** 
     232     * returns a list of all event names known to the dispatcher 
     233     * 
     234     * @return  array<string> 
     235     */ 
     236    public function getEventNames() 
     237    { 
     238        return array_keys($this->listeners); 
    204239    } 
    205240 
  • trunk/src/main/php/net/stubbles/events/stubLazyEventListener.php

    r887 r1035  
    1414 * @subpackage  events 
    1515 */ 
    16 class stubLazyEventListener extends stubBaseObject implements stubEventListener 
     16class stubLazyEventListener extends stubSerializableObject implements stubEventListener 
    1717{ 
    1818    /** 
     
    3131    { 
    3232        $this->fqClassName = $fqClassName; 
     33    } 
     34 
     35    /** 
     36     * returns the class name of the lazy loaded class 
     37     * 
     38     * @return  string 
     39     */ 
     40    public function getLazyClassName() 
     41    { 
     42        return $this->fqClassName; 
    3343    } 
    3444 
  • trunk/src/test/php/net/stubbles/events/stubCallbackListenerTestCase.php

    r98 r1035  
    3434        $this->assertEqual($instance->getInvocations(), 1); 
    3535        $this->assertTrue($callbacklistener->isNotified()); 
    36          
    37         $this->expectException('stubCallbackException'); 
    38         $callbacklistener = new stubCallbackListener('NonExistingClass', 'nonExistingMethod'); 
    3936    } 
    4037 
     
    105102    public function testNonExistingCallbackClass() 
    106103    { 
    107         $this->expectException('stubCallbackException'); 
    108104        $callbacklistener = new stubCallbackListener('NotExisting', 'notExisting'); 
     105        $event            = new stubEvent('eventName'); 
     106        $this->expectException('stubClassNotFoundException'); 
     107        $callbacklistener->handleEvent($event); 
    109108    } 
    110109 
     
    112111     * assure that a failed instantiation of the callback class throws a stubCallbackException 
    113112     */ 
    114     public function testFailingInstantiation() 
     113    public function testFailingInvocationWrongMethod() 
    115114    { 
    116115        $callbacklistener = new stubCallbackListener('TestStubCallback2', 'notExisting'); 
     
    136135    public function testIllegalConstructorArgumentInstance() 
    137136    { 
    138         $this->expectException('stubCallbackException'); 
     137        $this->expectException('stubIllegalArgumentException'); 
    139138        $callbacklistener = new stubCallbackListener(6100, 'pimp'); 
    140139    } 
     
    145144    public function testIllegalConstructorArgumentMethod() 
    146145    { 
    147         $this->expectException('stubCallbackException'); 
     146        $this->expectException('stubIllegalArgumentException'); 
    148147        $callbacklistener = new stubCallbackListener('Binford', 6100); 
    149148    } 
  • trunk/src/test/php/net/stubbles/events/stubEventDispatcherTestCase.php

    r176 r1035  
    3636     */ 
    3737    private $eventDispatcher; 
    38      
     38 
    3939    /** 
    4040     * set up the test environment 
     
    4848        $this->eventDispatcher->register($this->onAfter, 'onAfterTestEvent'); 
    4949    } 
    50      
     50 
    5151    /** 
    5252     * restore environment 
     
    5656        stubEventDispatcher::destroyInstance('__test'); 
    5757    } 
    58      
     58 
     59    /** 
     60     * test that all instance names are returned 
     61     */ 
     62    public function testInstanceNames() 
     63    { 
     64        $this->assertEqual(stubEventDispatcher::getInstanceNames(), array('__test')); 
     65    } 
     66 
     67    /** 
     68     * test that all event names are returned 
     69     */ 
     70    public function testRegisteredEventNames() 
     71    { 
     72        $this->assertEqual($this->eventDispatcher->getEventNames(), array('onBeforeTestEvent', 
     73                                                                          'onAfterTestEvent' 
     74                                                                    ) 
     75        ); 
     76    } 
    5977    /** 
    6078     * assure that registered listeners are recognized 
     
    6886        $this->assertFalse($this->eventDispatcher->has($this->onAfter, 'onEvent')); 
    6987    } 
    70      
     88 
    7189    /** 
    7290     * assure that event and data are in correct places 
     
    83101        $this->assertNull($this->onAfter->getEvent()); 
    84102    } 
    85      
     103 
    86104    /** 
    87105     * assure that event and data are in correct places 
     
    115133        $this->assertReference($testEvent, $event2); 
    116134    } 
    117      
     135 
    118136    /** 
    119137     * assure that event injected from outside leads to same results 
     
    128146        $this->assertEqual($event2->getInfo(), array(1)); 
    129147    } 
    130      
     148 
    131149    /** 
    132150     * assure that a canceled event is not propagated to the following listeners 
     
    143161        $this->assertReference($event2, $testEvent); 
    144162    } 
    145      
     163 
    146164    /** 
    147165     * assure that removed event listeners are recognized by has() and 
     
    157175        $this->eventDispatcher->trigger('onRemoveTest'); 
    158176    } 
    159      
     177 
    160178    /** 
    161179     * assure that event listeners registered with autoremove are only notified once 
     
    170188        $this->eventDispatcher->trigger('onAutoRemoveTest'); 
    171189    } 
    172      
     190 
    173191    /** 
    174192     * assure that queue methods work correct 
     
    186204        $this->assertEqual($this->eventDispatcher->countQueuedEvents(), 0); 
    187205    } 
    188      
     206 
    189207    /** 
    190208     * assert that a queued event will be propagated to an event listener 
     
    201219        $this->assertEqual($this->eventDispatcher->countQueuedEvents(), 1); 
    202220    } 
    203      
     221 
    204222    /** 
    205223     * assert that the queued event will not be propagated to 
     
    213231        $this->assertNull($listener->getEvent()); 
    214232    } 
    215      
     233 
    216234    /** 
    217235     * we register a listener with autoRemove which means 
     
    229247        $this->assertFalse($this->eventDispatcher->has($executeOnceListener, 'onQueuedEvent')); 
    230248    } 
    231      
     249 
    232250    /** 
    233251     * assert that a cancelled event will be removed from the queue 
     
    247265        $this->assertNull($anotherListener->getEvent()); 
    248266    } 
    249      
     267 
    250268    /** 
    251269     * assert that a canceled event will not be added to the queue 
     
    261279        $this->assertEqual($this->eventDispatcher->countQueuedEvents(), 0); 
    262280    } 
    263      
     281 
    264282    /** 
    265283     * assert that bubbling works as expected 
     
    268286    { 
    269287        $nestedDispatcher = stubEventDispatcher::getInstance('__nested'); 
     288        $this->assertEqual(stubEventDispatcher::getInstanceNames(), array('__test', '__nested')); 
    270289        $this->assertFalse($nestedDispatcher->hasParentDispatcher()); 
    271290        $nestedDispatcher->setParentDispatcher($this->eventDispatcher); 
     
    279298        stubEventDispatcher::destroyInstance('__nested'); 
    280299    } 
    281      
     300 
    282301    /** 
    283302     * assert that bubbling works as expected 
     
    294313        stubEventDispatcher::destroyInstance('__nested'); 
    295314    } 
    296      
     315 
    297316    /** 
    298317     * assert that bubbling works as expected 
     
    310329        stubEventDispatcher::destroyInstance('__nested'); 
    311330    } 
    312       
     331 
    313332    /** 
    314333     * assert that bubbling works as expected 
     
    325344        stubEventDispatcher::destroyInstance('__nested'); 
    326345    } 
    327      
     346 
    328347    public function testBubblingWithDestroyedInstance() 
    329348    { 
  • trunk/src/test/runIntegration.php

    r960 r1035  
    3535        $testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/integration/DatabaseTestCase.php'); 
    3636        $testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/integration/EncoderXJConfTestCase.php'); 
     37        $testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/integration/EventsTestCase.php'); 
    3738        $testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/integration/InterceptorTestCase.php'); 
    3839        $testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/integration/LoggerTestCase.php');