Changeset 636

Show
Ignore:
Timestamp:
11/09/07 12:45:14 (1 year ago)
Author:
laurentj
Message:

optimization of jEvent : increased performance, and removed jEventListenerFactory class

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/build/manifests/jelix-no-opt.mn

    r477 r636  
    1919cd lib/jelix/events 
    2020  jEventListener.class.php 
    21 * jEventListenerFactory.class.php 
    2221 
    2322cd lib/jelix/dao 
  • trunk/build/manifests/jelix-no-opt.mn

    r477 r636  
    1919cd lib/jelix/events 
    2020  jEventListener.class.php 
    21 * jEventListenerFactory.class.php 
    2221 
    2322cd lib/jelix/dao 
  • trunk/lib/jelix/events/jEvent.class.php

    r582 r636  
    66* @contributor Laurent Jouanneau 
    77* @copyright 2001-2005 CopixTeam, 2005-2007 Laurent Jouanneau 
    8 #if ENABLE_OPTIMIZED_SOURCE 
    98* This classes were get originally from the Copix project 
    10 * (CopixEvent*, CopixListener*, Copix 2.3dev20050901, http://www.copix.org) 
     9* (CopixEvent*, CopixListener* from Copix 2.3dev20050901, http://www.copix.org) 
    1110* Some lines of code are copyrighted 2001-2005 CopixTeam (LGPL licence). 
    1211* Initial authors of this Copix classes are Gerald Croes and  Patrice Ferlet, 
     
    1615* @licence  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file 
    1716*/ 
     17 
     18#if ENABLE_OPTIMIZED_SOURCE 
    1819#includephp jEventListener.class.php 
    19 #includephp jEventListenerFactory.class.php 
    20  
    2120#else 
    22 * This class was get originally from the Copix project 
    23 * (CopixEvent, CopixEventNotifier, CopixEventResponse, Copix 2.3dev20050901, http://www.copix.org) 
    24 * Some lines of code are copyrighted 2001-2005 CopixTeam (LGPL licence). 
    25 * Initial authors of this Copix class are Gerald Croes and  Patrice Ferlet, 
    26 * and this class was adapted/improved for Jelix by Laurent Jouanneau 
    27 * 
    28 * @link        http://www.jelix.org 
    29 * @licence  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file 
    30 */ 
    31  
    3221/** 
    3322 * 
    3423 */ 
    3524require_once (JELIX_LIB_EVENTS_PATH . 'jEventListener.class.php'); 
    36 require_once (JELIX_LIB_EVENTS_PATH . 'jEventListenerFactory.class.php'); 
    3725#endif 
    3826 
     
    4432*/ 
    4533class jEvent { 
    46    /** 
    47    * The name of the event. 
    48    * @var string name 
    49    */ 
    50    protected $_name = null; 
     34    /** 
     35    * The name of the event. 
     36    * @var string name 
     37    */ 
     38    protected $_name = null; 
    5139 
    52    /** 
    53    * the event parameters 
    54    */ 
    55    protected $_params = null; 
     40    /** 
     41    * the event parameters 
     42    */ 
     43    protected $_params = null; 
    5644 
    57    /** 
    58    * the listeners list. 
    59    */ 
    60    protected static $_listeners = array (); 
    61  
    62    /** 
    63    * New event. 
    64    */ 
    65    function __construct ($name, $params=array()){ 
    66       $this->_name   = $name; 
    67       $this->_params = & $params; 
    68    } 
    69  
    70    /** 
    71    * gets the name of the event 
    72    *    will be used internally for optimisations 
    73    */ 
    74    public function getName (){ 
    75       return $this->_name; 
    76    } 
    77  
    78    /** 
    79    * gets the given param 
    80    * @param string $name the param name 
    81    */ 
    82    public function getParam ($name){ 
    83       if (isset ($this->_params[$name])){ 
    84          $ret = $this->_params[$name]; 
    85       }else{ 
    86          $ret = null; 
    87       } 
    88       return $ret; 
    89    } 
    90  
    91    /** 
    92    * send a notification to all modules 
    93    * @param $event string   the event name 
    94    * @return jEvent 
    95    */ 
    96    public static function notify ($eventname, $params=array()) { 
    97  
    98       $event = new jEvent($eventname, $params); 
    99  
    100       if(!isset(jEvent::$_listeners[$eventname])){ 
    101           jEvent::$_listeners[$eventname] = jEventListenerFactory::getListenersOf ($eventname); 
    102       } 
    103  
    104       if (isset (jEvent::$_listeners[$eventname])){ 
    105          foreach (array_keys (jEvent::$_listeners[$eventname]) as $key) { 
    106             jEvent::$_listeners[$eventname][$key]->performEvent ($event); 
    107          } 
    108       } 
    109       return $event; 
    110    } 
    111  
    112  
    113    /** 
     45    /** 
    11446    * @var array of array 
    11547    */ 
    116    protected $_responses = array (); 
     48    protected $_responses = array (); 
    11749 
    118    /** 
    119     * add a response in the list 
    120     * @param array response a single response 
     50    /** 
     51    * New event. 
     52    * @param string $name  the event name 
     53    * @param array $params an associative array which contains parameters for the listeners 
    12154    */ 
    122    public function add ($response) { 
    123       $this->_responses[] = & $response; 
    124    } 
     55    function __construct ($name, $params=array()){ 
     56        $this->_name   = $name; 
     57        $this->_params = & $params; 
     58    } 
    12559 
    126    /** 
     60    /** 
     61    * gets the name of the event 
     62    *    will be used internally for optimisations 
     63    */ 
     64    public function getName (){ 
     65        return $this->_name; 
     66    } 
     67 
     68    /** 
     69    * gets the given param 
     70    * @param string $name the param name 
     71    */ 
     72    public function getParam ($name){ 
     73        if (isset ($this->_params[$name])){ 
     74            $ret = $this->_params[$name]; 
     75        }else{ 
     76            $ret = null; 
     77        } 
     78        return $ret; 
     79    } 
     80 
     81    /** 
     82    * adds datas in the responses list 
     83    * @param array $response a single response 
     84    */ 
     85    public function add ($response) { 
     86        $this->_responses[] = & $response; 
     87    } 
     88 
     89    /** 
    12790    * look in all the responses if we have a parameter having value as its answer 
    12891    * eg, we want to know if we have failed = true, we do 
     
    13295    * @return boolean wether or not we have founded the response value 
    13396    */ 
    134    public function inResponse ($responseName, $value, & $response){ 
    135       $founded  = false; 
    136       $response = array (); 
     97    public function inResponse ($responseName, $value, & $response){ 
     98        $founded  = false; 
     99        $response = array (); 
    137100 
    138       foreach ($this->_responses as $key=>$listenerResponse){ 
    139          if (isset ($listenerResponse[$responseName]) && $listenerResponse[$responseName] == $value){ 
    140             $founded = true; 
    141             $response[] = & $this->_responses[$key]; 
    142          
    143      
     101        foreach ($this->_responses as $key=>$listenerResponse){ 
     102            if (isset ($listenerResponse[$responseName]) && $listenerResponse[$responseName] == $value){ 
     103                $founded = true; 
     104                $response[] = & $this->_responses[$key]; 
     105            
     106       
    144107 
    145       return $founded; 
    146    
     108        return $founded; 
     109   
    147110 
    148    /** 
     111    /** 
    149112    * gets all the responses 
    150113    * @return array of associative array 
    151114    */ 
    152    public function getResponse () { 
    153       return $this->_responses; 
     115    public function getResponse () { 
     116        return $this->_responses; 
     117    } 
     118 
     119 
     120   //------------------------------------- static methods 
     121 
     122 
     123    /** 
     124    * send a notification to all modules 
     125    * @param $event string   the event name 
     126    * @return jEvent 
     127    */ 
     128    public static function notify ($eventname, $params=array()) { 
     129 
     130        $event = new jEvent($eventname, $params); 
     131 
     132        if(!isset(self::$hashListened[$eventname])){ 
     133            self::loadListenersFor ($eventname); 
     134        } 
     135 
     136        $methodName = 'on'.$event->getName (); 
     137        $list = & self::$hashListened[$eventname]; 
     138        foreach (array_keys ($list) as $key) { 
     139            $list[$key]->$methodName ($event); 
     140        } 
     141 
     142        return $event; 
    154143   } 
     144 
     145    protected static $compilerDatas = array('jEventCompiler', 
     146                    'events/jEventCompiler.class.php', 
     147                    'events.xml', 
     148                    'events.php' 
     149                    ); 
     150 
     151    /** 
     152    * because a listener can listen several events, we should 
     153    * create only one instancy of a listener for performance, and  
     154    * $hashListened will contains only reference to this listener. 
     155    * @var array of jEventListener 
     156    */ 
     157    protected static $listenersSingleton = array (); 
     158 
     159    /** 
     160    * hash table for event listened. 
     161    * $_hash['eventName'] = array of events (by reference) 
     162    * @var associative array of object 
     163    */ 
     164    protected static $hashListened = array (); 
     165 
     166    /** 
     167    * return the list of all listener corresponding to an event 
     168    * @param string $eventName the event name we wants the listeners for. 
     169    * @return array of objects 
     170    */ 
     171    protected static function loadListenersFor ($eventName) { 
     172        if (!isset($GLOBALS['JELIX_EVENTS'])) { 
     173            jIncluder::incAll(self::$compilerDatas); 
     174        } 
     175 
     176        $inf = & $GLOBALS['JELIX_EVENTS']; 
     177        self::$hashListened[$eventName] = array(); 
     178        if(isset($inf[$eventName])){ 
     179            foreach ($inf[$eventName] as $listener){ 
     180                list($module,$listenerName) = $listener; 
     181                if (! isset (self::$listenersSingleton[$module][$listenerName])){ 
     182                    require_once ($GLOBALS['gJConfig']->_modulesPathList[$module].'classes/'.strtolower ($listenerName).'.listener.php'); 
     183                    $className = $listenerName.'Listener'; 
     184        #if ENABLE_OLD_CLASS_NAMING 
     185                    if(!class_exists($className,false)){ 
     186                        $className = 'Listener'.$listenerName; 
     187                    } 
     188        #endif 
     189                    self::$listenersSingleton[$module][$listenerName] =  new $className (); 
     190                } 
     191                self::$hashListened[$eventName][] = self::$listenersSingleton[$module][$listenerName]; 
     192            } 
     193        } 
     194    } 
    155195} 
    156196?> 
  • trunk/lib/jelix/events/jEvent.class.php

    r582 r636  
    66* @contributor Laurent Jouanneau 
    77* @copyright 2001-2005 CopixTeam, 2005-2007 Laurent Jouanneau 
    8 #if ENABLE_OPTIMIZED_SOURCE 
    98* This classes were get originally from the Copix project 
    10 * (CopixEvent*, CopixListener*, Copix 2.3dev20050901, http://www.copix.org) 
     9* (CopixEvent*, CopixListener* from Copix 2.3dev20050901, http://www.copix.org) 
    1110* Some lines of code are copyrighted 2001-2005 CopixTeam (LGPL licence). 
    1211* Initial authors of this Copix classes are Gerald Croes and  Patrice Ferlet, 
     
    1615* @licence  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file 
    1716*/ 
     17 
     18#if ENABLE_OPTIMIZED_SOURCE 
    1819#includephp jEventListener.class.php 
    19 #includephp jEventListenerFactory.class.php 
    20  
    2120#else 
    22 * This class was get originally from the Copix project 
    23 * (CopixEvent, CopixEventNotifier, CopixEventResponse, Copix 2.3dev20050901, http://www.copix.org) 
    24 * Some lines of code are copyrighted 2001-2005 CopixTeam (LGPL licence). 
    25 * Initial authors of this Copix class are Gerald Croes and  Patrice Ferlet, 
    26 * and this class was adapted/improved for Jelix by Laurent Jouanneau 
    27 * 
    28 * @link        http://www.jelix.org 
    29 * @licence  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file 
    30 */ 
    31  
    3221/** 
    3322 * 
    3423 */ 
    3524require_once (JELIX_LIB_EVENTS_PATH . 'jEventListener.class.php'); 
    36 require_once (JELIX_LIB_EVENTS_PATH . 'jEventListenerFactory.class.php'); 
    3725#endif 
    3826 
     
    4432*/ 
    4533class jEvent { 
    46    /** 
    47    * The name of the event. 
    48    * @var string name 
    49    */ 
    50    protected $_name = null; 
     34    /** 
     35    * The name of the event. 
     36    * @var string name 
     37    */ 
     38    protected $_name = null; 
    5139 
    52    /** 
    53    * the event parameters 
    54    */ 
    55    protected $_params = null; 
     40    /** 
     41    * the event parameters 
     42    */ 
     43    protected $_params = null; 
    5644 
    57    /** 
    58    * the listeners list. 
    59    */ 
    60    protected static $_listeners = array (); 
    61  
    62    /** 
    63    * New event. 
    64    */ 
    65    function __construct ($name, $params=array()){ 
    66       $this->_name   = $name; 
    67       $this->_params = & $params; 
    68    } 
    69  
    70    /** 
    71    * gets the name of the event 
    72    *    will be used internally for optimisations 
    73    */ 
    74    public function getName (){ 
    75       return $this->_name; 
    76    } 
    77  
    78    /** 
    79    * gets the given param 
    80    * @param string $name the param name 
    81    */ 
    82    public function getParam ($name){ 
    83       if (isset ($this->_params[$name])){ 
    84          $ret = $this->_params[$name]; 
    85       }else{ 
    86          $ret = null; 
    87       } 
    88       return $ret; 
    89    } 
    90  
    91    /** 
    92    * send a notification to all modules 
    93    * @param $event string   the event name 
    94    * @return jEvent 
    95    */ 
    96    public static function notify ($eventname, $params=array()) { 
    97  
    98       $event = new jEvent($eventname, $params); 
    99  
    100       if(!isset(jEvent::$_listeners[$eventname])){ 
    101           jEvent::$_listeners[$eventname] = jEventListenerFactory::getListenersOf ($eventname); 
    102       } 
    103  
    104       if (isset (jEvent::$_listeners[$eventname])){ 
    105          foreach (array_keys (jEvent::$_listeners[$eventname]) as $key) { 
    106             jEvent::$_listeners[$eventname][$key]->performEvent ($event); 
    107          } 
    108       } 
    109       return $event; 
    110    } 
    111  
    112  
    113    /** 
     45    /** 
    11446    * @var array of array 
    11547    */ 
    116    protected $_responses = array (); 
     48    protected $_responses = array (); 
    11749 
    118    /** 
    119     * add a response in the list 
    120     * @param array response a single response 
     50    /** 
     51    * New event. 
     52    * @param string $name  the event name 
     53    * @param array $params an associative array which contains parameters for the listeners 
    12154    */ 
    122    public function add ($response) { 
    123       $this->_responses[] = & $response; 
    124    } 
     55    function __construct ($name, $params=array()){ 
     56        $this->_name   = $name; 
     57        $this->_params = & $params; 
     58    } 
    12559 
    126    /** 
     60    /** 
     61    * gets the name of the event 
     62    *    will be used internally for optimisations 
     63    */ 
     64    public function getName (){ 
     65        return $this->_name; 
     66    } 
     67 
     68    /** 
     69    * gets the given param 
     70    * @param string $name the param name 
     71    */ 
     72    public function getParam ($name){ 
     73        if (isset ($this->_params[$name])){ 
     74            $ret = $this->_params[$name]; 
     75        }else{ 
     76            $ret = null; 
     77        } 
     78        return $ret; 
     79    } 
     80 
     81    /** 
     82    * adds datas in the responses list 
     83    * @param array $response a single response 
     84    */ 
     85    public function add ($response) { 
     86        $this->_responses[] = & $response; 
     87    } 
     88 
     89    /** 
    12790    * look in all the responses if we have a parameter having value as its answer 
    12891    * eg, we want to know if we have failed = true, we do 
     
    13295    * @return boolean wether or not we have founded the response value 
    13396    */ 
    134    public function inResponse ($responseName, $value, & $response){ 
    135       $founded  = false; 
    136       $response = array (); 
     97    public function inResponse ($responseName, $value, & $response){ 
     98        $founded  = false; 
     99        $response = array (); 
    137100 
    138       foreach ($this->_responses as $key=>$listenerResponse){ 
    139          if (isset ($listenerResponse[$responseName]) && $listenerResponse[$responseName] == $value){ 
    140             $founded = true; 
    141             $response[] = & $this->_responses[$key]; 
    142          
    143      
     101        foreach ($this->_responses as $key=>$listenerResponse){ 
     102            if (isset ($listenerResponse[$responseName]) && $listenerResponse[$responseName] == $value){ 
     103                $founded = true; 
     104                $response[] = & $this->_responses[$key]; 
     105            
     106       
    144107 
    145       return $founded; 
    146    
     108        return $founded; 
     109   
    147110 
    148    /** 
     111    /** 
    149112    * gets all the responses 
    150113    * @return array of associative array 
    151114    */ 
    152    public function getResponse () { 
    153       return $this->_responses; 
     115    public function getResponse () { 
     116        return $this->_responses; 
     117    } 
     118 
     119 
     120   //------------------------------------- static methods 
     121 
     122 
     123    /** 
     124    * send a notification to all modules 
     125    * @param $event string   the event name 
     126    * @return jEvent 
     127    */ 
     128    public static function notify ($eventname, $params=array()) { 
     129 
     130        $event = new jEvent($eventname, $params); 
     131 
     132        if(!isset(self::$hashListened[$eventname])){ 
     133            self::loadListenersFor ($eventname); 
     134        } 
     135 
     136        $methodName = 'on'.$event->getName (); 
     137        $list = & self::$hashListened[$eventname]; 
     138        foreach (array_keys ($list) as $key) { 
     139            $list[$key]->$methodName ($event); 
     140        } 
     141 
     142        return $event; 
    154143   } 
     144 
     145    protected static $compilerDatas = array('jEventCompiler', 
     146                    'events/jEventCompiler.class.php', 
     147                    'events.xml', 
     148                    'events.php' 
     149                    ); 
     150 
     151    /** 
     152    * because a listener can listen several events, we should 
     153    * create only one instancy of a listener for performance, and  
     154    * $hashListened will contains only reference to this listener. 
     155    * @var array of jEventListener 
     156    */ 
     157    protected static $listenersSingleton = array (); 
     158 
     159    /** 
     160    * hash table for event listened. 
     161    * $_hash['eventName'] = array of events (by reference) 
     162    * @var associative array of object 
     163    */ 
     164    protected static $hashListened = array (); 
     165 
     166    /** 
     167    * return the list of all listener corresponding to an event 
     168    * @param string $eventName the event name we wants the listeners for. 
     169    * @return array of objects 
     170    */ 
     171    protected static function loadListenersFor ($eventName) { 
     172        if (!isset($GLOBALS['JELIX_EVENTS'])) { 
     173            jIncluder::incAll(self::$compilerDatas); 
     174        } 
     175 
     176        $inf = & $GLOBALS['JELIX_EVENTS']; 
     177        self::$hashListened[$eventName] = array(); 
     178        if(isset($inf[$eventName])){ 
     179            foreach ($inf[$eventName] as $listener){ 
     180                list($module,$listenerName) = $listener; 
     181                if (! isset (self::$listenersSingleton[$module][$listenerName])){ 
     182                    require_once ($GLOBALS['gJConfig']->_modulesPathList[$module].'classes/'.strtolower ($listenerName).'.listener.php'); 
     183                    $className = $listenerName.'Listener'; 
     184        #if ENABLE_OLD_CLASS_NAMING 
     185                    if(!class_exists($className,false)){ 
     186                        $className = 'Listener'.$listenerName; 
     187                    } 
     188        #endif 
     189                    self::$listenersSingleton[$module][$listenerName] =  new $className (); 
     190                } 
     191                self::$hashListened[$eventName][] = self::$listenersSingleton[$module][$listenerName]; 
     192            } 
     193        } 
     194    } 
    155195} 
    156196?> 
Download in other formats: Unified Diff Zip Archive