public function __invoke($event, $args)
{
$handler = $this->handler;
// Store the original event
$original_event = $this->event;
// Set the triggered event
// This is needed when 'all' events are fired
$this->event = $event;
if ($this->context) {
if (!$handler instanceof \Closure) {
throw new Exception('Handler must be a Closure in order to bind a contaxt to.');
}
if (!($handler = $handler->bindTo($this->context, $this->context))) {
throw new Exception('Context could not be bound to handler.');
}
}
// Prepend self to the arguments array
array_unshift($args, $this);
// Fetch the handler result
$result = call_user_func_array($handler, $args);
// Restore the old event name
$this->event = $original_event;
return $result;
}