Skip to content

Instantly share code, notes, and snippets.

@nergal
Created March 16, 2011 14:45
Show Gist options
  • Save nergal/872592 to your computer and use it in GitHub Desktop.
Save nergal/872592 to your computer and use it in GitHub Desktop.
<?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