From afa0d3e1255a8c7da5bd024b8246c971ca0a214e Mon Sep 17 00:00:00 2001 From: gwleuverink Date: Tue, 10 Sep 2024 00:06:01 +0200 Subject: [PATCH 1/3] Add EventWatcher --- src/Events/EventWatcher.php | 31 +++++++++++++++++++++++++++++++ src/NativeServiceProvider.php | 3 +++ 2 files changed, 34 insertions(+) create mode 100644 src/Events/EventWatcher.php diff --git a/src/Events/EventWatcher.php b/src/Events/EventWatcher.php new file mode 100644 index 0000000..c0e324c --- /dev/null +++ b/src/Events/EventWatcher.php @@ -0,0 +1,31 @@ +broadcastOn(); + + if(! in_array('nativephp', $channels)) { + return; + } + + $this->client->post('debug/broadcast', ['event' => $event]); + }); + } +} diff --git a/src/NativeServiceProvider.php b/src/NativeServiceProvider.php index 609cf42..2f618ee 100644 --- a/src/NativeServiceProvider.php +++ b/src/NativeServiceProvider.php @@ -11,6 +11,7 @@ use Native\Laravel\Commands\MinifyApplicationCommand; use Native\Laravel\Commands\SeedDatabaseCommand; use Native\Laravel\Logging\LogWatcher; +use Native\Laravel\Events\EventWatcher; use Spatie\LaravelPackageTools\Package; use Spatie\LaravelPackageTools\PackageServiceProvider; @@ -61,6 +62,8 @@ protected function configureApp() app(LogWatcher::class)->register(); } + app(EventWatcher::class)->register(); + $this->rewriteStoragePath(); $this->rewriteDatabase(); From 16d90dee0395613a61ced135552ef8ce9b0557d4 Mon Sep 17 00:00:00 2001 From: gwleuverink Date: Tue, 10 Sep 2024 01:25:00 +0200 Subject: [PATCH 2/3] Add event name to client request --- src/Events/EventWatcher.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Events/EventWatcher.php b/src/Events/EventWatcher.php index c0e324c..5bfa6ee 100644 --- a/src/Events/EventWatcher.php +++ b/src/Events/EventWatcher.php @@ -25,7 +25,10 @@ public function register(): void return; } - $this->client->post('debug/broadcast', ['event' => $event]); + $this->client->post('broadcast', [ + 'event' => $eventName, + 'payload' => $event + ]); }); } } From 11508f7afbd431092362b59c5b68891f690a7ccf Mon Sep 17 00:00:00 2001 From: gwleuverink Date: Tue, 10 Sep 2024 11:32:32 +0200 Subject: [PATCH 3/3] Filter only custom events & fix event fqcn --- src/Events/EventWatcher.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Events/EventWatcher.php b/src/Events/EventWatcher.php index 5bfa6ee..edc53de 100644 --- a/src/Events/EventWatcher.php +++ b/src/Events/EventWatcher.php @@ -21,12 +21,18 @@ public function register(): void $channels = $event->broadcastOn(); + // Only events dispatched on the nativephp channel if(! in_array('nativephp', $channels)) { return; } + // Only post custom events to broadcasting endpoint + if(str_starts_with($eventName ,'Native\\Laravel\\Events')) { + return; + } + $this->client->post('broadcast', [ - 'event' => $eventName, + 'event' => "\\{$eventName}", 'payload' => $event ]); });