From c2905ecf92db7c88960c2d74a9861a3dcb14b27c Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 6 Aug 2016 14:54:45 +0200 Subject: [PATCH] Fixes #39 --- Event/EventManager.php | 110 ++++++++++++++--------------------------- Pattern/Mediator.php | 43 ++++++++++------ 2 files changed, 66 insertions(+), 87 deletions(-) diff --git a/Event/EventManager.php b/Event/EventManager.php index 631454d6c..bf94c3adf 100644 --- a/Event/EventManager.php +++ b/Event/EventManager.php @@ -18,7 +18,7 @@ namespace phpOMS\Event; use phpOMS\Pattern\Mediator; /** - * Dispatcher class. + * EventManager class. * * @category Framework * @package phpOMS\Event @@ -40,7 +40,15 @@ class EventManager implements Mediator * @var array * @since 1.0.0 */ - private $events = []; + private $groups = []; + + /** + * Callbacks. + * + * @var array + * @since 1.0.0 + */ + private $callbacks = []; /** * Constructor. @@ -55,102 +63,60 @@ class EventManager implements Mediator /** * {@inheritdoc} */ - public function attach(string $event, \Closure $callback, string $listener) : string + public function attach(string $group, \Closure $callback, bool $remove = false) { - $this->events[$event][$listener] = $callback; - - return $event . '/' . $listener; + $this->callbacks[$group] = ['remove' => $remove, 'func' => $callback]; } /** * {@inheritdoc} */ - public function trigger(string $event, string $source, \Closure $callback = null) : int + public function detach(string $group) { - $count = 0; + unset($this->callbacks[$group]); + unset($this->groups[$group]); + } - if (isset($this->events[$event])) { - foreach ($this->events[$event] as $listener) { - foreach ($listener as $closure) { - $closure($source); - $count++; - } + /** + * {@inheritdoc} + */ + public function trigger(string $id, string $group) + { + if(isset($this->groups[$group])) { + unset($this->groups[$group][$id]); + } + + if ($this->hasOutstanding($group)) { + $this->callbacks[$group]['func']; + + if($this->callbacks[$group]['remove']) { + $this->detach($group); } } - - if (isset($callback)) { - /** @var $callback Callable */ - $callback($count); - } - - return $count; - } - - /** - * Trigger event. - * - * An object fires an event until it's callback returns false - * - * @param string $event Event ID - * @param string $source What class is invoking this event - * @param \Closure $callback Callback function of the event. This will get triggered after firering all listener callbacks. - * - * @return int - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public function triggerUntil(string $event, string $source, \Closure $callback = null) : int - { - $run = true; - $count = 0; - - if (isset($this->events[$event])) { - do { - foreach ($this->events[$event] as $eventClosure) { - $run = $eventClosure($source); - $count++; - } - } while ($run); - } - - if ($callback !== null) { - $callback($count); - } - - return $count; } /** * {@inheritdoc} */ - public function detachListener(string $event, string $listener) : bool + private function hasOutstanding(string $group) : bool { - if (isset($this->events[$event][$listener])) { - unset($this->events[$event][$listener]); - - return true; - } - - return false; + return empty($this->groups[$group]); } /** * {@inheritdoc} */ - public function detachEvent(string $event) : bool + public function addGroup(string $id, string $group) { - if (isset($this->events[$event])) { - unset($this->events[$event]); - - return true; + if(!isset($this->groups[$group])) { + $this->groups[$group] = []; } - return false; + $this->groups[$group][$id] = false; } /** - * Count event listenings. + * Count events. * * @return int * @@ -159,7 +125,7 @@ class EventManager implements Mediator */ public function count() : int { - return count($this->events); + return count($this->callbacks); } } diff --git a/Pattern/Mediator.php b/Pattern/Mediator.php index 68f4f7178..e011598b0 100644 --- a/Pattern/Mediator.php +++ b/Pattern/Mediator.php @@ -34,55 +34,68 @@ interface Mediator extends \Countable * * Listeners will get called if a certain event gets triggered * - * @param string $event Event ID + * @param string $group Group * @param \Closure $callback Function to call if the event gets triggered - * @param string $listener What class is attaching this listener + * @param bool $remove Remove event after execution * - * @return string UID for the listener + * @return void * * @since 1.0.0 * @author Dennis Eichhorn */ - public function attach(string $event, \Closure $callback, string $listener) : string; + public function attach(string $group, \Closure $callback, bool $remove = false); /** * Removing a event. * - * @param string $event ID of the event + * @param string $group Group * * @return bool * * @since 1.0.0 * @author Dennis Eichhorn */ - public function detachEvent(string $event) : bool; + public function detach(string $group) : bool; /** - * Removing a listener. + * Has outstanding. * - * @param string $event ID of the event - * @param string $listener ID of the listener + * @param string $group Group * * @return bool * * @since 1.0.0 * @author Dennis Eichhorn */ - public function detachListener(string $event, string $listener) : bool; + private function hasOutstanding(string $group) : bool; + + /** + * Add group. + * + * Add new element to group + * + * @param string $id Event ID + * @param string $group Group + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function addGroup(string $id, string $group); /** * Trigger event. * * An object fires an event * - * @param string $event Event ID - * @param string $source What class is invoking this event - * @param \Closure $callback Callback function of the event. This will get triggered after firering all listener callbacks. + * @param string $id Event ID + * @param string $group Group * - * @return int + * @return void * * @since 1.0.0 * @author Dennis Eichhorn */ - public function trigger(string $event, string $source, \Closure $callback = null) : int; + public function trigger(string $id, string $group); }