Source for file jEvent.class.php

Documentation is available at jEvent.class.php

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

Documentation generated on Wed, 07 Sep 2011 13:47:16 +0200 by phpDocumentor 1.4.3