From bba78c5b53ea5f0a181cea447c45bd46c885c886 Mon Sep 17 00:00:00 2001 From: Vehikl Date: Tue, 26 Nov 2024 16:27:20 -0500 Subject: [PATCH 1/5] Add new framework events to listen to --- app/Models/WebhookConfiguration.php | 47 +++++++++++++++++++++----- app/Providers/EventServiceProvider.php | 2 ++ 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/app/Models/WebhookConfiguration.php b/app/Models/WebhookConfiguration.php index cf792da195..f8508b1961 100644 --- a/app/Models/WebhookConfiguration.php +++ b/app/Models/WebhookConfiguration.php @@ -34,6 +34,24 @@ class WebhookConfiguration extends Model 'events', ]; + public static function getEventClassesFromDirectory(string $directory, string $after): array + { + $events = []; + foreach (File::allFiles($directory) as $file) { + $namespace = str($file->getPath()) + ->after($after) + ->replace(DIRECTORY_SEPARATOR, '\\') + ->after('\\') + ->replaceFirst('app', 'App') + ->toString(); + + $events[] = $namespace.'\\'.str($file->getFilename()) + ->replace([DIRECTORY_SEPARATOR, '.php'], ['\\', '']); + } + + return $events; + } + protected function casts(): array { return [ @@ -75,6 +93,7 @@ public static function allPossibleEvents(): array { return collect(static::discoverCustomEvents()) ->merge(static::allModelEvents()) + ->merge(static::discoverFrameworkEvents()) ->unique() ->filter(fn ($event) => !in_array($event, static::$eventBlacklist)) ->all(); @@ -97,6 +116,11 @@ public static function transformClassName(string $event): string ->after('eloquent.') ->replace('App\\Models\\', '') ->replace('App\\Events\\', 'event: ') +// ->replace('Illuminate\\', 'framework: ') + ->replaceMatches('/Illuminate\\\\([A-z]+)\\\\Events\\\\/', function (array $matches) { + return strtolower($matches[1]) . ': '; + }) + // ->replace('Illuminate\\(capture)\\Events', '(capture): ') ->toString(); } @@ -133,16 +157,23 @@ public static function discoverCustomEvents(): array { $directory = app_path('Events'); + return self::getEventClassesFromDirectory($directory, base_path()); + } + + public static function discoverFrameworkEvents(): array + { + $frameworkDirectory = 'vendor/laravel/framework/src/'; + + $eventDirectories = [ + 'Illuminate/Auth/Events', + 'Illuminate/Queue/Events', + ]; + $events = []; - foreach (File::allFiles($directory) as $file) { - $namespace = str($file->getPath()) - ->after(base_path()) - ->replace(DIRECTORY_SEPARATOR, '\\') - ->replace('\\app\\', 'App\\') - ->toString(); + foreach ($eventDirectories as $eventDirectory) { + $directory = base_path("$frameworkDirectory/$eventDirectory"); - $events[] = $namespace . '\\' . str($file->getFilename()) - ->replace([DIRECTORY_SEPARATOR, '.php'], ['\\', '']); + $events = array_merge($events, static::getEventClassesFromDirectory($directory, $frameworkDirectory)); } return $events; diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 50a6e42cdd..284718d70c 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -15,5 +15,7 @@ class EventServiceProvider extends ServiceProvider 'eloquent.created*' => [DispatchWebhooks::class], 'eloquent.deleted*' => [DispatchWebhooks::class], 'eloquent.updated*' => [DispatchWebhooks::class], + 'Illuminate\\Auth\\Events\\*' => [DispatchWebhooks::class], + 'Illuminate\\Queue\\Events\\*' => [DispatchWebhooks::class], ]; } From ce1ad0fa14acef508e8627b3a09613d9a3fbd83a Mon Sep 17 00:00:00 2001 From: Vehikl Date: Tue, 26 Nov 2024 16:30:25 -0500 Subject: [PATCH 2/5] Add simple test for framework events --- tests/Feature/Webhooks/DispatchWebhooksTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/Feature/Webhooks/DispatchWebhooksTest.php b/tests/Feature/Webhooks/DispatchWebhooksTest.php index 7fcb1e326d..89b89f99c6 100644 --- a/tests/Feature/Webhooks/DispatchWebhooksTest.php +++ b/tests/Feature/Webhooks/DispatchWebhooksTest.php @@ -4,9 +4,12 @@ use App\Jobs\ProcessWebhook; use App\Models\Server; +use App\Models\User; use App\Models\WebhookConfiguration; use App\Tests\TestCase; +use Illuminate\Auth\Events\Authenticated; use Illuminate\Foundation\Testing\LazilyRefreshDatabase; +use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Queue; class DispatchWebhooksTest extends TestCase @@ -88,6 +91,18 @@ public function test_it_does_not_call_deleted_webhooks() Queue::assertNothingPushed(); } + public function test_it_listens_to_framework_events() + { + WebhookConfiguration::factory()->create([ + 'events' => [Authenticated::class], + ]); + + $user = User::factory()->create(); + Auth::login($user); + + Queue::assertPushed(ProcessWebhook::class, 1); + } + public function createServer(): Server { return Server::factory()->withNode()->create(); From 87d8066bd4f390cf7ca6d8a646fbfe83dcc7b69b Mon Sep 17 00:00:00 2001 From: pelican-vehikl Date: Thu, 12 Dec 2024 16:19:40 -0500 Subject: [PATCH 3/5] Update app/Models/WebhookConfiguration.php Co-authored-by: Lance Pioch --- app/Models/WebhookConfiguration.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/Models/WebhookConfiguration.php b/app/Models/WebhookConfiguration.php index f8508b1961..7a53111eff 100644 --- a/app/Models/WebhookConfiguration.php +++ b/app/Models/WebhookConfiguration.php @@ -116,7 +116,6 @@ public static function transformClassName(string $event): string ->after('eloquent.') ->replace('App\\Models\\', '') ->replace('App\\Events\\', 'event: ') -// ->replace('Illuminate\\', 'framework: ') ->replaceMatches('/Illuminate\\\\([A-z]+)\\\\Events\\\\/', function (array $matches) { return strtolower($matches[1]) . ': '; }) From d150c2107f1f651ef88843714414b5a81161a00a Mon Sep 17 00:00:00 2001 From: pelican-vehikl Date: Thu, 12 Dec 2024 16:20:19 -0500 Subject: [PATCH 4/5] Update app/Models/WebhookConfiguration.php Co-authored-by: Lance Pioch --- app/Models/WebhookConfiguration.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/Models/WebhookConfiguration.php b/app/Models/WebhookConfiguration.php index 7a53111eff..9f04a242d1 100644 --- a/app/Models/WebhookConfiguration.php +++ b/app/Models/WebhookConfiguration.php @@ -119,7 +119,6 @@ public static function transformClassName(string $event): string ->replaceMatches('/Illuminate\\\\([A-z]+)\\\\Events\\\\/', function (array $matches) { return strtolower($matches[1]) . ': '; }) - // ->replace('Illuminate\\(capture)\\Events', '(capture): ') ->toString(); } From 0dc3904239bd85a1c0a4b46061db9c62ee46d020 Mon Sep 17 00:00:00 2001 From: pelican-vehikl Date: Thu, 12 Dec 2024 16:22:07 -0500 Subject: [PATCH 5/5] Update app/Models/WebhookConfiguration.php --- app/Models/WebhookConfiguration.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/Models/WebhookConfiguration.php b/app/Models/WebhookConfiguration.php index 9f04a242d1..309565bc62 100644 --- a/app/Models/WebhookConfiguration.php +++ b/app/Models/WebhookConfiguration.php @@ -116,9 +116,7 @@ public static function transformClassName(string $event): string ->after('eloquent.') ->replace('App\\Models\\', '') ->replace('App\\Events\\', 'event: ') - ->replaceMatches('/Illuminate\\\\([A-z]+)\\\\Events\\\\/', function (array $matches) { - return strtolower($matches[1]) . ': '; - }) + ->replaceMatches('/Illuminate\\\\([A-z]+)\\\\Events\\\\/', fn (array $matches) => strtolower($matches[1]) . ': ') ->toString(); }