Skip to content
This repository has been archived by the owner on Jun 28, 2023. It is now read-only.

Commit

Permalink
Created EventManager class
Browse files Browse the repository at this point in the history
  • Loading branch information
mecha committed Jul 6, 2016
1 parent 3b3e6ce commit a86ac66
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions src/EventManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Dhii\WpEvents;

use \Psr\EventManager\EventInterface;
use \Psr\EventManager\EventManagerInterface;

/**
* Event Manager.
*
* @author Miguel Muscat <[email protected]>
*/
class EventManager implements EventManagerInterface
{

/**
* Constructs a new instance.
*/
public function __construct()
{

}

/**
* {@inheritdoc}
*/
public function attach($event, $callback, $priority = 10)
{
$eventObject = $this->normalizeEvent($event);
$numArgsToPass = $this->getCallableNumParams($callback);
\add_filter($eventObject->getName(), $callback, $priority, $numArgsToPass + 1);
}

/**
* {@inheritdoc}
*/
public function detach($event, $callback)
{
$eventObject = $this->normalizeEvent($event);
\remove_filter($eventObject->getName(), $callback);
}

/**
* {@inheritdoc}
*/
public function clearListeners($event)
{
$eventObject = $this->normalizeEvent($event);
\remove_all_filters($eventObject->getName());
}

/**
* {@inheritdoc}
*/
public function trigger($event, $target = null, $argv = array())
{
$eventObject = $this->normalizeEvent($event);
$args = empty($argv)
? array(null)
: $argv;
array_push($args, $target);
\apply_filters_ref_array($eventObject->getName(), $args);
}

/**
* Normalizes the given event into an Event instance.
*
* @param EventInterfaceevent string or Event instance.
* @return EventInterface The event instance.
*/
protected function normalizeEvent($event)
{
return ($event instanceof EventInterfacevent)
? $event
: new Event($event);
}

/**
* Gets the number of parameters for a callable.
*
* @param callable $callable The callable.
* @return integer The number of parameters.
*/
protected function getCallableNumParams($callable)
{
return $this->getCallableReflection($callable)->getNumberOfParameter();
}

/**
* Gets the reflection instance for a callable.
*
* @param callable $callable The callable.
* @return ReflectionFunction|ReflectionMethod The reflection instance.
*/
protected function getCallableReflection($callable)
{
return is_array($callable) ?
new ReflectionMethod($callable[0], $callable[1]) :
new ReflectionFunction($callable);
}

}

0 comments on commit a86ac66

Please sign in to comment.