diff --git a/Event/EventManager.php b/Event/EventManager.php index 628471df5..9618a288f 100644 --- a/Event/EventManager.php +++ b/Event/EventManager.php @@ -63,13 +63,13 @@ class EventManager implements Mediator /** * {@inheritdoc} */ - public function attach(string $group, \Closure $callback, bool $remove = false) : bool + public function attach(string $group, \Closure $callback, bool $remove = false, bool $reset = false) : bool { if (isset($this->callbacks[$group])) { return false; } - $this->callbacks[$group] = ['remove' => $remove, 'func' => $callback]; + $this->callbacks[$group] = ['remove' => $remove, 'reset' => $reset, 'func' => $callback]; return true; } @@ -77,27 +77,46 @@ class EventManager implements Mediator /** * {@inheritdoc} */ - public function trigger(string $group, string $id = '', bool $reset = false) /* : void */ + public function trigger(string $group, string $id = '') /* : void */ { if (isset($this->groups[$group])) { - unset($this->groups[$group][$id]); + $this->groups[$group][$id] = true; } - if ($this->hasOutstanding($group)) { - $this->callbacks[$group]['func']; + if (!$this->hasOutstanding($group)) { + $this->callbacks[$group]['func'](); if ($this->callbacks[$group]['remove']) { $this->detach($group); + } elseif($this->callbacks[$group]['reset']) { + $this->reset($group); } } } + private function reset(string $group) /* : void */ + { + foreach($this->groups[$group] as $id => $ok) { + $this->groups[$group][$id] = false; + } + } + /** * {@inheritdoc} */ private function hasOutstanding(string $group) : bool { - return empty($this->groups[$group]); + if(!isset($this->groups[$group])) { + return false; + } + + foreach($this->groups[$group] as $id => $ok) { + if(!$ok) { + return true; + } + } + + return false; } /**