fix event triggering with regex

This commit is contained in:
Dennis Eichhorn 2020-10-25 23:06:28 +01:00
parent d7be58a4fe
commit 553a17edaf
2 changed files with 22 additions and 17 deletions

View File

@ -139,6 +139,7 @@ final class EventManager implements \Countable
} }
$this->callbacks[$group]['callbacks'][] = $callback; $this->callbacks[$group]['callbacks'][] = $callback;
$this->addGroup($group, '');
return true; return true;
} }
@ -160,24 +161,28 @@ final class EventManager implements \Countable
$idIsRegex = \stripos($id, '/') === 0; $idIsRegex = \stripos($id, '/') === 0;
$groups = []; $groups = [];
if ($groupIsRegex) { foreach ($this->groups as $groupName => $value) {
foreach ($this->groups as $groupName => $value) { if ($groupIsRegex) {
if (\preg_match($group, $groupName) === 1) { if (\preg_match($group, $groupName) === 1) {
$groups[$groupName] = []; $groups[$groupName] = [];
} }
} elseif (\preg_match($groupName, $group) === 1) {
$groups[$groupName] = [];
} }
} else {
$groups[$group] = [];
} }
foreach ($groups as $groupName => $groupValues) { foreach ($groups as $groupName => $groupValues) {
if ($idIsRegex) { foreach ($this->groups[$groupName] as $idName => $value) {
foreach ($this->groups[$groupName] as $idName => $value) { if ($idIsRegex) {
if (\preg_match($id, $idName) === 1) { if (\preg_match($id, $idName) === 1) {
$groups[$groupName][] = $idName; $groups[$groupName][] = $idName;
} }
} elseif ($idName !== '' && \preg_match($idName, $id) === 1) {
$groups[$groupName][] = $id;
} }
} else { }
if (empty($groups[$groupName])) {
$groups[$groupName][] = $id; $groups[$groupName][] = $id;
} }
} }

View File

@ -262,9 +262,9 @@ abstract class ModuleAbstract
*/ */
protected function createModel(int $account, $obj, string $mapper, string $trigger, string $ip) : void protected function createModel(int $account, $obj, string $mapper, string $trigger, string $ip) : void
{ {
$this->app->eventManager->trigger('PRE:Module:' . static::MODULE_NAME . '-' . $trigger . '-create', '', $obj); $this->app->eventManager->triggerSimilar('PRE:Module:' . static::MODULE_NAME . '-' . $trigger . '-create', '', $obj);
$id = $mapper::create($obj); $id = $mapper::create($obj);
$this->app->eventManager->trigger('POST:Module:' . static::MODULE_NAME . '-' . $trigger . '-create', '', [ $this->app->eventManager->triggerSimilar('POST:Module:' . static::MODULE_NAME . '-' . $trigger . '-create', '', [
$account, $account,
null, $obj, null, $obj,
StringUtils::intHash($mapper), 0, StringUtils::intHash($mapper), 0,
@ -291,9 +291,9 @@ abstract class ModuleAbstract
protected function createModels(int $account, array $objs, string $mapper, string $trigger, string $ip) : void protected function createModels(int $account, array $objs, string $mapper, string $trigger, string $ip) : void
{ {
foreach ($objs as $obj) { foreach ($objs as $obj) {
$this->app->eventManager->trigger('PRE:Module:' . static::MODULE_NAME . '-' . $trigger . '-create', '', $obj); $this->app->eventManager->triggerSimilar('PRE:Module:' . static::MODULE_NAME . '-' . $trigger . '-create', '', $obj);
$id = $mapper::create($obj); $id = $mapper::create($obj);
$this->app->eventManager->trigger('POST:Module:' . static::MODULE_NAME . '-' . $trigger . '-create', '', [ $this->app->eventManager->triggerSimilar('POST:Module:' . static::MODULE_NAME . '-' . $trigger . '-create', '', [
$account, $account,
null, $obj, null, $obj,
StringUtils::intHash($mapper), 0, StringUtils::intHash($mapper), 0,
@ -321,14 +321,14 @@ abstract class ModuleAbstract
*/ */
protected function updateModel(int $account, $old, $new, $mapper, string $trigger, string $ip) : void protected function updateModel(int $account, $old, $new, $mapper, string $trigger, string $ip) : void
{ {
$this->app->eventManager->trigger('PRE:Module:' . static::MODULE_NAME . '-' . $trigger . '-update', '', $old); $this->app->eventManager->triggerSimilar('PRE:Module:' . static::MODULE_NAME . '-' . $trigger . '-update', '', $old);
$id = 0; $id = 0;
if (\is_string($mapper)) { if (\is_string($mapper)) {
$id = $mapper::update($new); $id = $mapper::update($new);
} elseif ($mapper instanceof \Closure) { } elseif ($mapper instanceof \Closure) {
$mapper(); $mapper();
} }
$this->app->eventManager->trigger('POST:Module:' . static::MODULE_NAME . '-' . $trigger . '-update', '', [ $this->app->eventManager->triggerSimilar('POST:Module:' . static::MODULE_NAME . '-' . $trigger . '-update', '', [
$account, $account,
$old, $new, $old, $new,
StringUtils::intHash(\is_string($mapper) ? $mapper : \get_class($mapper)), 0, StringUtils::intHash(\is_string($mapper) ? $mapper : \get_class($mapper)), 0,
@ -354,9 +354,9 @@ abstract class ModuleAbstract
*/ */
protected function deleteModel(int $account, $obj, string $mapper, string $trigger, string $ip) : void protected function deleteModel(int $account, $obj, string $mapper, string $trigger, string $ip) : void
{ {
$this->app->eventManager->trigger('PRE:Module:' . static::MODULE_NAME . '-' . $trigger . '-delete', '', $obj); $this->app->eventManager->triggerSimilar('PRE:Module:' . static::MODULE_NAME . '-' . $trigger . '-delete', '', $obj);
$id = $mapper::delete($obj); $id = $mapper::delete($obj);
$this->app->eventManager->trigger('POST:Module:' . static::MODULE_NAME . '-' . $trigger . '-delete', '', [ $this->app->eventManager->triggerSimilar('POST:Module:' . static::MODULE_NAME . '-' . $trigger . '-delete', '', [
$account, $account,
$obj, null, $obj, null,
StringUtils::intHash($mapper), 0, StringUtils::intHash($mapper), 0,
@ -384,9 +384,9 @@ abstract class ModuleAbstract
*/ */
protected function createModelRelation(int $account, $rel1, $rel2, string $mapper, string $field, string $trigger, string $ip) : void protected function createModelRelation(int $account, $rel1, $rel2, string $mapper, string $field, string $trigger, string $ip) : void
{ {
$this->app->eventManager->trigger('PRE:Module:' . static::MODULE_NAME . '-' . $trigger . '-relation', '', $rel1); $this->app->eventManager->triggerSimilar('PRE:Module:' . static::MODULE_NAME . '-' . $trigger . '-relation', '', $rel1);
$mapper::createRelation($field, $rel1, $rel2); $mapper::createRelation($field, $rel1, $rel2);
$this->app->eventManager->trigger('POST:Module:' . static::MODULE_NAME . '-' . $trigger . '-relation', '', [ $this->app->eventManager->triggerSimilar('POST:Module:' . static::MODULE_NAME . '-' . $trigger . '-relation', '', [
$account, $account,
$rel1, $rel2, $rel1, $rel2,
StringUtils::intHash($mapper), 0, StringUtils::intHash($mapper), 0,