This commit is contained in:
Dennis Eichhorn 2017-03-31 14:13:43 +02:00
parent 754db8b6ca
commit 7b57c8e28a

View File

@ -63,13 +63,13 @@ class EventManager implements Mediator
/** /**
* {@inheritdoc} * {@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])) { if (isset($this->callbacks[$group])) {
return false; return false;
} }
$this->callbacks[$group] = ['remove' => $remove, 'func' => $callback]; $this->callbacks[$group] = ['remove' => $remove, 'reset' => $reset, 'func' => $callback];
return true; return true;
} }
@ -77,27 +77,46 @@ class EventManager implements Mediator
/** /**
* {@inheritdoc} * {@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])) { if (isset($this->groups[$group])) {
unset($this->groups[$group][$id]); $this->groups[$group][$id] = true;
} }
if ($this->hasOutstanding($group)) { if (!$this->hasOutstanding($group)) {
$this->callbacks[$group]['func']; $this->callbacks[$group]['func']();
if ($this->callbacks[$group]['remove']) { if ($this->callbacks[$group]['remove']) {
$this->detach($group); $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} * {@inheritdoc}
*/ */
private function hasOutstanding(string $group) : bool 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;
} }
/** /**