Skip to content

Instantly share code, notes, and snippets.

@carlosocarvalho
Last active September 13, 2016 13:52
Show Gist options
  • Save carlosocarvalho/ee6ab64ca2109422dc38821ca5e55648 to your computer and use it in GitHub Desktop.
Save carlosocarvalho/ee6ab64ca2109422dc38821ca5e55648 to your computer and use it in GitHub Desktop.
FileEve
<?php
class Events {
/**
* @var array An array of listeners
*/
protected static $_listeners = array();
// ------------------------------------------------------------------------
/**
* Register
*
* Registers a Callback for a given event
*
* @access public
* @param string The name of the event
* @param array The callback for the Event
* @return void
*/
public static function register($event, array $callback)
{
$key = get_class($callback[0]).'::'.$callback[1];
self::$_listeners[$event][$key] = $callback;
self::log_message('debug', 'Events::register() - Registered "'.$key.' with event "'.$event.'"');
}
// ------------------------------------------------------------------------
/**
* Trigger
*
* Triggers an event and returns the results. The results can be returned
* in the following formats:
*
* 'array'
* 'json'
* 'serialized'
* 'string'
*
* @access public
* @param string The name of the event
* @param mixed Any data that is to be passed to the listener
* @param string The return type
* @return mixed The return of the listeners, in the return type
*/
public static function trigger($event, $data = '', $return_type = 'string')
{
self::log_message('debug', 'Events::trigger() - Triggering event "'.$event.'"');
$calls = array();
if (self::hasListeners($event))
{
foreach (self::$_listeners[$event] as $listener)
{
if (is_callable($listener))
{
$calls[] = call_user_func($listener, $data);
}
}
}
return self::_format_return($calls, $return_type);
}
// ------------------------------------------------------------------------
/**
* Format Return
*
* Formats the return in the given type
*
* @access protected
* @param array The array of returns
* @param string The return type
* @return mixed The formatted return
*/
protected static function _format_return(array $calls, $return_type)
{
self::log_message('debug', 'Events::_format_return() - Formating calls in type "'.$return_type.'"');
switch ($return_type)
{
case 'json':
return json_encode($calls);
break;
case 'serialized':
return serialize($calls);
break;
case 'string':
$str = '';
foreach ($calls as $call)
{
$str .= $call;
}
return $str;
break;
default:
return $calls;
break;
}
return FALSE;
}
// ------------------------------------------------------------------------
/**
* Has Listeners
*
* Checks if the event has listeners
*
* @access public
* @param string The name of the event
* @return bool Whether the event has listeners
*/
public static function hasListeners($event)
{
self::log_message('debug', 'Events::hasListeners() - Checking if event "'.$event.'" has listeners.');
if (isset(self::$_listeners[$event]) AND count(self::$_listeners[$event]) > 0)
{
return TRUE;
}
return FALSE;
}
// ------------------------------------------------------------------------
/**
* Log Message
*
* Pulled out for unit testing
*
* @param string $type
* @param string $message
* @return void
*/
public static function log_message($type = 'debug', $message = '')
{
if (function_exists('log_message'))
{
log_message($type, $message);
}
}
}
//** libraries/Events
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment