Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/Events/EventWatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
]);
});
}
}
3 changes: 3 additions & 0 deletions src/NativeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -61,6 +62,8 @@ protected function configureApp()
app(LogWatcher::class)->register();
}

app(EventWatcher::class)->register();

$this->rewriteStoragePath();

$this->rewriteDatabase();
Expand Down