diff --git a/config/webhook-client.php b/config/webhook-client.php index 4d477af..8630c54 100644 --- a/config/webhook-client.php +++ b/config/webhook-client.php @@ -68,4 +68,9 @@ * It deletes all records after 1 week. Set to null if no models should be deleted. */ 'delete_after_days' => 30, + + /* + * Should a unique token be added to the route name + */ + 'add_unique_token_to_route_name' => false, ]; diff --git a/src/WebhookClientServiceProvider.php b/src/WebhookClientServiceProvider.php index a5aa688..852a89a 100644 --- a/src/WebhookClientServiceProvider.php +++ b/src/WebhookClientServiceProvider.php @@ -23,16 +23,20 @@ public function packageBooted() { Route::macro('webhooks', function (string $url, string $name = 'default', $method = 'post') { - if(! in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) { + if (! in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) { throw InvalidMethod::make($method); } + if (config('webhook-client.add_unique_token_to_route_name', false)) { + $name .= '.' . Str::random(8); + } + return Route::{$method}($url, '\Spatie\WebhookClient\Http\Controllers\WebhookController') - ->name("webhook-client-{$name}." . Str::random(8)); + ->name("webhook-client-{$name}"); }); $this->app->scoped(WebhookConfigRepository::class, function () { - $configRepository = new WebhookConfigRepository(); + $configRepository = new WebhookConfigRepository; collect(config('webhook-client.configs')) ->map(fn (array $config) => new WebhookConfig($config)) @@ -44,9 +48,13 @@ public function packageBooted() $this->app->bind(WebhookConfig::class, function () { $routeName = Route::currentRouteName() ?? ''; - $routeNameSuffix = Str::after($routeName, 'webhook-client-'); + $configName = Str::after($routeName, 'webhook-client-'); - $configName = Str::before($routeNameSuffix, '.'); + if (config('webhook-client.add_unique_token_to_route_name', false)) { + $routeNameSuffix = Str::after($routeName, 'webhook-client-'); + + $configName = Str::before($routeNameSuffix, '.'); + } $webhookConfig = app(WebhookConfigRepository::class)->getConfig($configName); diff --git a/tests/WebhookControllerTest.php b/tests/WebhookControllerTest.php index 27a84e0..6b75ec1 100644 --- a/tests/WebhookControllerTest.php +++ b/tests/WebhookControllerTest.php @@ -207,6 +207,8 @@ public function it_can_store_none_of_the_headers() /** @test */ public function multiple_routes_can_share_configuration() { + config()->set('webhook-client.add_unique_token_to_route_name', true); + Route::webhooks('incoming-webhooks-additional'); $this->refreshWebhookConfigRepository();