forked from NativePHP/laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventWatcher.php
40 lines (30 loc) · 992 Bytes
/
EventWatcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
namespace Native\Laravel\Events;
use Illuminate\Support\Facades\Event;
use Native\Laravel\Client\Client;
class EventWatcher
{
public function __construct(protected Client $client) {}
public function register(): void
{
Event::listen('*', function (string $eventName, array $data) {
$event = $data[0] ?? null;
if (! method_exists($event, 'broadcastOn')) {
return;
}
$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}",
'payload' => $event
]);
});
}
}