Created
March 16, 2011 14:45
-
-
Save nergal/872592 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Событийный класс | |
* | |
* @package Fan | |
* @class Fan_Event | |
* @author Nergal | |
*/ | |
class Fan_Event | |
{ | |
/** | |
* @static | |
* @access protected | |
* @var array | |
*/ | |
protected static $_listeners_pool = array(); | |
/** | |
* Добавление события | |
* | |
* @static | |
* @params string $event_name | |
* @params callback $callback | |
* @return void | |
*/ | |
public static function connect($event_name, $callback) | |
{ | |
if (!in_array($event_name, array_keys(self::$_listeners_pool))) { | |
self::$_listeners_pool[$event_name] = array(); | |
} | |
$callbacks = &self::$_listeners_pool[$event_name]; | |
if (!in_array($callback, $callbacks)) { | |
$callbacks[] = $callback; | |
} | |
} | |
/** | |
* Вызов событий с параметрами | |
* | |
* @static | |
* @params string|array $event_names | |
* @params mixed $params | |
* @return void | |
*/ | |
public static function emit($event_names, $params = NULL) { | |
$event_names = (array) $event_names; | |
foreach ($event_names as $event_name) { | |
if (in_array($event_name, array_keys(self::$_listeners_pool))) { | |
foreach(self::$_listeners_pool[$event_name] as $event) { | |
if ($event !== NULL) { | |
call_user_func_array($event, $params); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment