root/trunk/src/main/php/net/stubbles/events/stubLazyEventListener.php

Revision 1307, 2.4 kB (checked in by mikey, 4 months ago)

fixed several class loading issues

Line 
1 <?php
2 /**
3  * Class for lazy loading of concrete event listeners.
4  *
5  * @author      Frank Kleine <mikey@stubbles.net>
6  * @package     stubbles
7  * @subpackage  events
8  */
9 stubClassLoader::load('net::stubbles::events::stubEvent',
10                       'net::stubbles::events::stubEventListener'
11 );
12 /**
13  * Class for lazy loading of concrete event listeners.
14  *
15  * @package     stubbles
16  * @subpackage  events
17  */
18 class stubLazyEventListener extends stubSerializableObject implements stubEventListener
19 {
20     /**
21      * the full qualified name of the class to execute in case the event happens
22      *
23      * @var  string
24      */
25     protected $fqClassName;
26
27     /**
28      * constructor
29      *
30      * @param  string  $fqClassName  full qualified name of the class to execute
31      */
32     public function __construct($fqClassName)
33     {
34         $this->fqClassName = $fqClassName;
35     }
36
37     /**
38      * returns the class name of the lazy loaded class
39      *
40      * @return  string
41      */
42     public function getLazyClassName()
43     {
44         return $this->fqClassName;
45     }
46
47     /**
48      * handles an event
49      *
50      * @param   stubEvent  $event  event that triggered this event listener
51      * @throws  stubException
52      */
53     public function handleEvent(stubEvent $event)
54     {
55         $this->createInstance()->handleEvent($event);
56     }
57
58     /**
59      * returns true if listener should be removed after first notification or not
60      *
61      * @return  bool
62      * @throws  stubException
63      */
64     public function autoremove()
65     {
66         return $this->createInstance()->autoremove();
67     }
68
69     /**
70      * helper method to create the concrete event listener instance
71      *
72      * @return  stubEventListener
73      * @throws  stubRuntimeException
74      */
75     protected function createInstance()
76     {
77         static $instance;
78         if (null === $instance) {
79             $className = stubClassLoader::getNonQualifiedClassName($this->fqClassName);
80             if (class_exists($className, false) == false) {
81                 stubClassLoader::load($this->fqClassName);
82             }
83             
84             $instance = new $className();
85         }
86         
87         if (($instance instanceof stubEventListener) == false) {
88             throw new stubRuntimeException('Lazy loaded event listener ' . $this->fqClassName . ' is not an instance of ' . $this->getPackageName() . '.stubEventListener.');
89         }
90         
91         return $instance;
92     }
93 }
94 ?>
Note: See TracBrowser for help on using the browser.