tangra logo
   
[ class tree: tangra_lib ] [ index: tangra_lib ] [ all elements ]
 

Source for file web_events_dispatcher.class.php

Documentation is available at web_events_dispatcher.class.php

  1. <?php
  2. // *** Tangra (Application Framework and Tools for PHP)
  3. //  $Id$
  4. //
  5.  
  6. /**
  7.  * Contains class Web_Events_Dispatcher
  8.  *
  9.  * @package tangra_lib
  10.  * @subpackage  web_site
  11.  */
  12.  
  13. /**
  14.  *
  15.  */
  16. require_once(TANGRA_MAIN_DIR.'exceptions/te_key_already_exists.class.php');
  17. /**
  18.  *
  19.  */
  20. require_once(TANGRA_MAIN_DIR.'web_site/web_event_composite.class.php');
  21.  
  22. /**
  23.  * Web_Event_Dispatcher is responsible for monitoring occurence of
  24.  * Web_Events and dispatching of program flow
  25.  *
  26.  * @package tangra_lib
  27.  * @subpackage  web_site
  28.  */
  29.     /**
  30.      * Reference to Web_page object
  31.      *
  32.      * @var Web_page 
  33.      * @internal
  34.      */
  35.     private $page_pointer;
  36.     /**
  37.      * Default event-action pair
  38.      *
  39.      * @var unknown_type 
  40.      * @internal
  41.      */
  42.     private $default_event_action_pair;
  43.     /**
  44.      * Array containing event-action pairs
  45.      *
  46.      * @var array 
  47.      * @internal
  48.      */
  49.     private $event_action_pairs = array();
  50.     /**
  51.      * Array that contains prioritetes of events
  52.      *
  53.      * @var array 
  54.      * @internal
  55.      */
  56.     private $order_arr = array();
  57.  
  58.     /**
  59.      * Constructor
  60.      *
  61.      * @param Web_Page $page_pointer Page that instantiates this object
  62.      */
  63.     function __construct(Web_Page $page_pointer{
  64.         $this->page_pointer = $page_pointer;
  65.     }
  66.  
  67.  
  68.     /**
  69.      * Sets default event-action pair
  70.      *
  71.      * @param Web_Event $we 
  72.      * @param string $target_method Name of the Web_Page method that will be triggered if event occures
  73.      */
  74.     function set_default_event_action_pair(Web_Event $we$target_method{
  75.         $this->default_event_action_pair['event'$we;
  76.         $this->default_event_action_pair['target'$target_method;
  77.     }
  78.  
  79.  
  80.     /**
  81.      * Adds new event-action pair
  82.      *
  83.      * @param Web_Event $we 
  84.      * @param unknown_type $target_method Name of the Web_Page method that will be triggered if event occures
  85.      * @param integer $order Order of this event. Events will be evaluated  ordered by their $order.
  86.      * @throws Tangra_Exception, TE_Key_Already_Exists
  87.      */
  88.     public function add_wed_event_action_pair(Web_Event $we$target_method$order 10{
  89.         tangra_if_not_int_throw_e($order);
  90.         if (!array_key_exists($we->get_name()$this->event_action_pairs)) {
  91.             if (method_exists($this->page_pointer$target_method)) {
  92.                 $this->event_action_pairs[$we->get_name()array('event' => $we'target' => $target_method);
  93.                 $this->order_arr[$we->get_name()$order;
  94.                 asort($this->order_arr);
  95.             else {
  96.                 throw new Tangra_Exception('Method not callable: '.$target_method);
  97.             }
  98.         else {
  99.             throw new TE_Key_Already_Exists($we->get_name());
  100.         }
  101.     }
  102.  
  103.     /**
  104.      * Returns array dispatch direction
  105.      *
  106.      * Returns array:
  107.      * ['target'] - action of the event, method that will be triggered
  108.      * ['param'] - captured value/array of values
  109.      *
  110.      * @param Web_Context $context 
  111.      * @return array 
  112.      */
  113.     public function get_dispatch(Web_Context $context{
  114.  
  115.         $ret false;
  116.  
  117.         foreach($this->order_arr as $ev_name => $order{
  118.             $ea_pair $this->event_action_pairs[$ev_name];
  119.             $param $ea_pair['event']->is_it_me($context);
  120.             if ($param !== false{
  121.                 $ret['target'$ea_pair['target'];
  122.                 $ret['param'$param;
  123.                 break;
  124.             }
  125.         }
  126.  
  127.         if (!$ret{
  128.             $ret['target'$this->default_event_action_pair['target'];
  129.             $ret['param'NULL;
  130.         }
  131.  
  132.         return $ret;
  133.     }
  134.  
  135.  
  136.     /**
  137.      * Checks if there are duplicate captures
  138.      *
  139.      * @return string Capture label
  140.      */
  141.     public function check_for_duplicate_captures({
  142.         $ret false;
  143.  
  144.         $tmp_arr array();
  145.  
  146.         foreach($this->event_action_pairs as $eap{
  147.             $tmp_capture $eap['event']->get_capture();
  148.             if (is_array($tmp_capture)) //composite event
  149.                 foreach($tmp_capture as $capture{
  150.                     if (!array_key_exists($capture$tmp_arr)) {
  151.                         $tmp_arr[$capture$capture;
  152.                     else {
  153.                         $ret $capture;
  154.                         break 2;
  155.                     }
  156.                 }
  157.             else //simple event
  158.                 if (!array_key_exists($tmp_capture$tmp_arr)) {
  159.                     $tmp_arr[$tmp_capture$tmp_capture;
  160.                 else {
  161.                     $ret $tmp_capture;
  162.                     break;
  163.                 }
  164.             }
  165.         }
  166.  
  167.         return $ret;
  168.     }
  169.  
  170.  
  171.     /**
  172.      * Checks if event with name <var>$event_name</var> is already registered
  173.      *
  174.      * @param unknown_type $event_name 
  175.      * @return boolean 
  176.      */
  177.     public function is_event_registered($event_name{
  178.         return array_key_exists($event_name$this->event_action_pairs);
  179.     }
  180.  
  181.  
  182.     /**
  183.      * Returns event action pairs
  184.      *
  185.      * @return array 
  186.      */
  187.     public function get_event_action_pairs({
  188.         return $this->event_action_pairs;
  189.     }
  190. }