Event
[ class tree: Event ] [ index: Event ] [ all elements ]

Source for file EventDispatcher.php

Documentation is available at EventDispatcher.php

  1. <?php
  2. /**
  3.  * MVClasses
  4.  *
  5.  * LICENSE
  6.  *
  7.  * "THE BEER-WARE LICENSE" (Revision 42):
  8.  * "Sven Strittmatter" <ausserirdisch@sven-space.de> wrote this file.
  9.  * As long as you retain this notice you can do whatever you want with
  10.  * this stuff. If we meet some day, and you think this stuff is worth it,
  11.  * you can buy me a beer in return.
  12.  *
  13.  * @category    MVClasses
  14.  * @package     Event
  15.  * @copyright   Copyright (c) 2007 Sven Strittmatter
  16.  */
  17.  
  18. /**
  19.  * @see Event
  20.  */
  21. require_once dirname(__FILE__)."/Event.php";
  22.  
  23. /**
  24.  * Enter description here...
  25.  *
  26.  * Longdesc
  27.  *
  28.  * @todo complete description
  29.  *
  30.  * @category    MVClasses
  31.  * @package     Event
  32.  * @copyright     Copyright (c) 2007 Sven Strittmatter
  33.  * @author    "Sven Strittmatter" <ausserirdisch@sven-space.de>
  34.  * @version     1.0
  35.  * @link        http://www.sven-space.de/code/php/MVClasses
  36.  */
  37. class EventDispatcher {
  38.     /**
  39.      * @static
  40.      * @access private
  41.      * @var EventDispatcher 
  42.      */
  43.     private static $_instance;
  44.     /**
  45.      * @access private
  46.      * @var array 
  47.      */
  48.     private $_handlers;
  49.  
  50.     /**
  51.      * Singleton
  52.      */
  53.     protected function __construct({
  54.         $this->_handlers array();
  55.     }
  56.  
  57.     /**
  58.      * Singleton
  59.      */
  60.     protected function __clone({}
  61.  
  62.     public static function getInstance({
  63.         if (null === self::$_instance{
  64.             self::$_instance new EventDispatcher();
  65.         }
  66.  
  67.         return self::$_instance;
  68.     }
  69.  
  70.     public function addHandler($nameEventHandler $handler{
  71.         if (!isset($this->_handlers[$name])) {
  72.             $this->_handlers[$namearray();
  73.         }
  74.  
  75.         $this->_handlers[$name][$handler;
  76.     }
  77.  
  78.     public function triggerEvent($event$context null$info null{
  79.         if (!$event instanceof Event{
  80.             $event new Event($event$context$info);
  81.         }
  82.  
  83.         $name $event->getName();
  84.  
  85.         if (!isset($this->_handlers[$name])) {
  86.             return $event;
  87.         }
  88.  
  89.         foreach ($this->_handlers[$nameas $handler{
  90.             $handler->handle($event);
  91.  
  92.             if ($event->isCancelled()) {
  93.                 break;
  94.             }
  95.         }
  96.  
  97.         return $event;
  98.     }
  99. }

Documentation generated on Sun, 02 Aug 2009 17:14:54 +0200 by phpDocumentor 1.4.2