Fix event bug

This commit is contained in:
Dennis Eichhorn 2018-12-02 16:45:22 +01:00
parent c126670789
commit 4bd0a0f1b3
3 changed files with 21 additions and 9 deletions

View File

@ -1730,6 +1730,8 @@ class DataMapperAbstract implements DataMapperInterface
}
if (!isset($obj)) {
// todo: implement solution for classes with constructor arguments
// maybe implement a factory pattern for every datamapper model
$obj = new $class();
}

View File

@ -62,9 +62,13 @@ final class EventManager
public function __construct(Dispatcher $dispatcher = null)
{
$this->dispatcher = $dispatcher ?? new class {
function dispatch($func, array $data)
public function dispatch($func, $data)
{
$func(...$data);
if (\is_array($data)) {
$func(...$data);
} else {
$func($data);
}
}
};
}
@ -88,10 +92,8 @@ final class EventManager
$hooks = include $path;
foreach ($hooks as $group => $hook) {
foreach ($hook['callback'] as $callbacks) {
foreach ($callbacks as $callback) {
$this->attach($group, $callback, $hook['remove'] ?? false, $hook['reset'] ?? true);
}
foreach ($hook['callback'] as $callback) {
$this->attach($group, $callback, $hook['remove'] ?? false, $hook['reset'] ?? true);
}
}
@ -142,7 +144,7 @@ final class EventManager
$result = false;
foreach ($allGroups as $match) {
if (\preg_match('~^' . $group . '$~', $match) === 1) {
if (\preg_match('~^' . $match . '$~', $group) === 1) {
$result = $result || $this->triggerSingleEvent($match, $id, $data);
}
}
@ -169,7 +171,11 @@ final class EventManager
if (!$this->hasOutstanding($group)) {
foreach ($this->callbacks[$group]['callbacks'] as $func) {
$this->dispatcher->disptach($func, ...$data);
if (\is_array($data)) {
$this->dispatcher->dispatch($func, ...$data);
} else {
$this->dispatcher->dispatch($func, $data);
}
}
if ($this->callbacks[$group]['remove']) {
@ -195,6 +201,10 @@ final class EventManager
*/
private function reset(string $group) : void
{
if (!isset($this->groups[$group])) {
return;
}
foreach ($this->groups[$group] as $id => $ok) {
$this->groups[$group][$id] = false;
}

View File

@ -40,7 +40,7 @@ class EventManagerTest extends \PHPUnit\Framework\TestCase
$event = new EventManager();
self::assertTrue($event->attach('group', function() { return true; }, false, false));
self::assertFalse($event->attach('group', function() { return true; }, false, false));
self::assertTrue($event->attach('group', function() { return true; }, false, false));
self::assertEquals(1, $event->count());
}