mirror of
https://github.com/pelican-dev/panel.git
synced 2025-05-19 22:14:45 +02:00
Listen to more framework webhook events (#728)
* Add new framework events to listen to * Add simple test for framework events * Update app/Models/WebhookConfiguration.php Co-authored-by: Lance Pioch <git@lance.sh> * Update app/Models/WebhookConfiguration.php Co-authored-by: Lance Pioch <git@lance.sh> * Update app/Models/WebhookConfiguration.php --------- Co-authored-by: Vehikl <go@vehikl.com> Co-authored-by: Lance Pioch <git@lance.sh>
This commit is contained in:
parent
914f3dcdbd
commit
7a4c4ce02a
@ -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 @@ class WebhookConfiguration extends Model
|
||||
{
|
||||
return collect(static::discoverCustomEvents())
|
||||
->merge(static::allModelEvents())
|
||||
->merge(static::discoverFrameworkEvents())
|
||||
->unique()
|
||||
->filter(fn ($event) => !in_array($event, static::$eventBlacklist))
|
||||
->all();
|
||||
@ -97,6 +116,7 @@ class WebhookConfiguration extends Model
|
||||
->after('eloquent.')
|
||||
->replace('App\\Models\\', '')
|
||||
->replace('App\\Events\\', 'event: ')
|
||||
->replaceMatches('/Illuminate\\\\([A-z]+)\\\\Events\\\\/', fn (array $matches) => strtolower($matches[1]) . ': ')
|
||||
->toString();
|
||||
}
|
||||
|
||||
@ -133,16 +153,23 @@ class WebhookConfiguration extends Model
|
||||
{
|
||||
$directory = app_path('Events');
|
||||
|
||||
$events = [];
|
||||
foreach (File::allFiles($directory) as $file) {
|
||||
$namespace = str($file->getPath())
|
||||
->after(base_path())
|
||||
->replace(DIRECTORY_SEPARATOR, '\\')
|
||||
->replace('\\app\\', 'App\\')
|
||||
->toString();
|
||||
return self::getEventClassesFromDirectory($directory, base_path());
|
||||
}
|
||||
|
||||
$events[] = $namespace . '\\' . str($file->getFilename())
|
||||
->replace([DIRECTORY_SEPARATOR, '.php'], ['\\', '']);
|
||||
public static function discoverFrameworkEvents(): array
|
||||
{
|
||||
$frameworkDirectory = 'vendor/laravel/framework/src/';
|
||||
|
||||
$eventDirectories = [
|
||||
'Illuminate/Auth/Events',
|
||||
'Illuminate/Queue/Events',
|
||||
];
|
||||
|
||||
$events = [];
|
||||
foreach ($eventDirectories as $eventDirectory) {
|
||||
$directory = base_path("$frameworkDirectory/$eventDirectory");
|
||||
|
||||
$events = array_merge($events, static::getEventClassesFromDirectory($directory, $frameworkDirectory));
|
||||
}
|
||||
|
||||
return $events;
|
||||
|
@ -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],
|
||||
];
|
||||
}
|
||||
|
@ -4,9 +4,12 @@ namespace App\Tests\Feature\Webhooks;
|
||||
|
||||
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 @@ class DispatchWebhooksTest extends TestCase
|
||||
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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user