* // Register a callback for the "start" event
* Event::listen('start', function() {return 'Started!';});
*
* // Register an object instance callback for the given event
* Event::listen('event', array($object, 'method'));
*
*
* @param string $event
* @param mixed $callback
* @return void
*/
public static function listen($event, $callback)
{
static::$events[$event][] = $callback;
}
/**
* Fire an event and return the first response.
*
*
* // Fire the "start" event
* $response = Event::first('start');
*
* // Fire the "start" event passing an array of parameters
* $response = Event::first('start', array('Laravel', 'Framework'));
*
*
* @param string $event
* @param array $parameters
* @return mixed
*/
public static function first($event, $parameters = array())
{
return head(static::fire($event, $parameters));
}
/**
* Fire an event so that all listeners are called.
*
*
* // Fire the "start" event
* $responses = Event::fire('start');
*
* // Fire the "start" event passing an array of parameters
* $responses = Event::fire('start', array('Laravel', 'Framework'));
*
*
* @param string $event
* @param array $parameters
* @return array
*/
public static function fire($event, $parameters = array())
{
$responses = array();
if (static::listeners($event))
{
foreach (static::$events[$event] as $callback)
{
$responses[] = call_user_func_array($callback, $parameters);
}
}
return $responses;
}
}