Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Listen to more framework webhook events #728

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
47 changes: 39 additions & 8 deletions app/Models/WebhookConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down Expand Up @@ -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();
Expand 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();
}

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
];
}
15 changes: 15 additions & 0 deletions tests/Feature/Webhooks/DispatchWebhooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down