From ed67fdb18eafcebdf67f25fd49afb5471cc67597 Mon Sep 17 00:00:00 2001 From: JSON Date: Sat, 30 Dec 2023 02:48:54 -0800 Subject: [PATCH 1/3] Added event mapping via regular expression --- src/EventEmitterTrait.php | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/EventEmitterTrait.php b/src/EventEmitterTrait.php index 1503429..63b29e8 100644 --- a/src/EventEmitterTrait.php +++ b/src/EventEmitterTrait.php @@ -138,7 +138,7 @@ public function emit($event, array $arguments = []) $onceListeners = array_values($this->onceListeners[$event]); } - if(empty($listeners) === false) { + if(empty($listeners) === false) { foreach ($listeners as $listener) { $listener(...$arguments); } @@ -151,4 +151,30 @@ public function emit($event, array $arguments = []) } } } + + public function emit_regex($pattern, array $arguments = []) + { + if ($pattern === null) { + throw new InvalidArgumentException('event name must not be null'); + } + + $listeners = []; + foreach($this->listeners as $_event => $_listeners) { + if(preg_match($pattern, $_event, $matches)) { + foreach ($_listeners as $listener) { + $listener(...$arguments); + } + } + } + + $onceListeners = []; + foreach($this->onceListeners as $_event => $_listeners) { + if(preg_match($pattern, $_event, $matches)) { + unset($this->onceListeners[$_event]); + foreach ($_listeners as $listener) { + $listener(...$arguments); + } + } + } + } } From ab0a22421771ffec26b7bc3c00c8605608b0fc92 Mon Sep 17 00:00:00 2001 From: JSON Date: Sat, 30 Dec 2023 03:28:22 -0800 Subject: [PATCH 2/3] Added emit_regex 01-api.md Explanation and example of emit_regex() --- doc/01-api.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/doc/01-api.md b/doc/01-api.md index 17ba333..7aa9afb 100644 --- a/doc/01-api.md +++ b/doc/01-api.md @@ -66,6 +66,28 @@ $result = $a + $b; $emitter->emit('numbers_added', [$result, $a, $b]); ``` +## emit_regex($pattern, array $arguments = []) + +Emit an event, which will call all listeners by regular expression. + +Example: + +```php +$events->on('note_a1', function($channel, $volacity) { + echo "paying note a1\n"; +}); + +$events->on('note_a2', function($channel, $volacity) { + echo "paying note a2\n"; +}); + +$events->on('note_b2', function($channel, $volacity) { + echo "paying note b2\n"; +}); + +$events->emit_regex("/note_a/i", [0, 127]); +``` + ## listeners($event) Allows you to inspect the listeners attached to an event. Particularly useful From 29172aaf7a37db95c903cdc67f2b976f8fc0f313 Mon Sep 17 00:00:00 2001 From: JSON Date: Sat, 30 Dec 2023 03:33:40 -0800 Subject: [PATCH 3/3] Update 01-api.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Spelling 🙄 --- doc/01-api.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/01-api.md b/doc/01-api.md index 7aa9afb..f7098dd 100644 --- a/doc/01-api.md +++ b/doc/01-api.md @@ -73,15 +73,15 @@ Emit an event, which will call all listeners by regular expression. Example: ```php -$events->on('note_a1', function($channel, $volacity) { +$events->on('note_a1', function($channel, $velocity) { echo "paying note a1\n"; }); -$events->on('note_a2', function($channel, $volacity) { +$events->on('note_a2', function($channel, $velocity) { echo "paying note a2\n"; }); -$events->on('note_b2', function($channel, $volacity) { +$events->on('note_b2', function($channel, $velocity) { echo "paying note b2\n"; });