From 3b3e6ce6f0cd075b18565d124966803b38272531 Mon Sep 17 00:00:00 2001 From: Miguel Muscat Date: Wed, 6 Jul 2016 21:39:18 +0200 Subject: [PATCH 1/2] Created Event class --- src/Event.php | 159 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 src/Event.php diff --git a/src/Event.php b/src/Event.php new file mode 100644 index 0000000..02d4d17 --- /dev/null +++ b/src/Event.php @@ -0,0 +1,159 @@ + + */ +class Event implements EventInterface +{ + + /** + * The event name. + * + * @var string + */ + protected $name; + + /** + * The parameters. + * + * @var array + */ + protected $params; + + /** + * The target context object. + * + * @var mixed + */ + protected $target; + + /** + * The propagation flag. + * + * @var booleans + */ + protected $propagation; + + /** + * Constructs a new instance. + * + * @param string $name The event name. + * @param array $params The event parameters. + * @param mixed $target The target object. Used for context. + * @param boolean $propagation True to propagate the event, false to not. + */ + public function __construct($name, array $params = array(), $target = null, $propagation = true) + { + $this->setName($name) + ->setParams($params) + ->setTarget($target) + ->setPropagation($propagation); + } + + /** + * {@inheritdoc} + */ + public function getName() + { + return $this->name; + } + + /** + * {@inheritdoc} + */ + public function getParam($name) + { + return isset($this->params[$name]) + ? $this->params[$name] + : null; + } + + /** + * {@inheritdoc} + */ + public function getParams() + { + return $this->params; + } + + /** + * {@inheritdoc} + */ + public function getTarget() + { + return $this->target; + } + + /** + * Gets whether or not the event propagates. + * + * @return boolean True if the event propagate, false if not. + */ + public function getPropagation() + { + return $this->propagation; + } + + /** + * {@inheritdoc} + */ + public function isPropagationStopped() + { + return !$this->getPropagation(); + } + + /** + * {@inheritdoc} + */ + public function setName($name) + { + $this->name = $name; + return $this; + } + + /** + * {@inheritdoc} + */ + public function setParams(array $params) + { + $this->params = $params; + return $this; + } + + /** + * {@inheritdoc} + */ + public function setTarget($target) + { + $this->target = $target; + return $this; + } + + /** + * Sets the event propagation. + * + * @param boolean $propagation True to propagate, false to not. + * @return Event This instance. + */ + public function setPropagation($propagation) + { + $this->propagation = $propagation; + return $this; + } + + /** + * {@inheritdoc} + */ + public function stopPropagation($flag) + { + $this->setPropagation(!$flag); + return $this; + } + +} From a86ac66c8a6b620a67803df9ef39a6a0bff4770e Mon Sep 17 00:00:00 2001 From: Miguel Muscat Date: Wed, 6 Jul 2016 21:39:25 +0200 Subject: [PATCH 2/2] Created EventManager class --- src/EventManager.php | 102 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/EventManager.php diff --git a/src/EventManager.php b/src/EventManager.php new file mode 100644 index 0000000..e70d9a9 --- /dev/null +++ b/src/EventManager.php @@ -0,0 +1,102 @@ + + */ +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); + } + +}