Skip to content

Commit 393d212

Browse files
authored
Add config option to enable/disable unique token (#212)
* Add config option to enable/disable unique token * Update WebhookClientServiceProvider.php
1 parent 919d8f8 commit 393d212

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

config/webhook-client.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,9 @@
6868
* It deletes all records after 1 week. Set to null if no models should be deleted.
6969
*/
7070
'delete_after_days' => 30,
71+
72+
/*
73+
* Should a unique token be added to the route name
74+
*/
75+
'add_unique_token_to_route_name' => false,
7176
];

src/WebhookClientServiceProvider.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,20 @@ public function packageBooted()
2323
{
2424
Route::macro('webhooks', function (string $url, string $name = 'default', $method = 'post') {
2525

26-
if(! in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) {
26+
if (! in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) {
2727
throw InvalidMethod::make($method);
2828
}
2929

30+
if (config('webhook-client.add_unique_token_to_route_name', false)) {
31+
$name .= '.' . Str::random(8);
32+
}
33+
3034
return Route::{$method}($url, '\Spatie\WebhookClient\Http\Controllers\WebhookController')
31-
->name("webhook-client-{$name}." . Str::random(8));
35+
->name("webhook-client-{$name}");
3236
});
3337

3438
$this->app->scoped(WebhookConfigRepository::class, function () {
35-
$configRepository = new WebhookConfigRepository();
39+
$configRepository = new WebhookConfigRepository;
3640

3741
collect(config('webhook-client.configs'))
3842
->map(fn (array $config) => new WebhookConfig($config))
@@ -44,9 +48,13 @@ public function packageBooted()
4448
$this->app->bind(WebhookConfig::class, function () {
4549
$routeName = Route::currentRouteName() ?? '';
4650

47-
$routeNameSuffix = Str::after($routeName, 'webhook-client-');
51+
$configName = Str::after($routeName, 'webhook-client-');
4852

49-
$configName = Str::before($routeNameSuffix, '.');
53+
if (config('webhook-client.add_unique_token_to_route_name', false)) {
54+
$routeNameSuffix = Str::after($routeName, 'webhook-client-');
55+
56+
$configName = Str::before($routeNameSuffix, '.');
57+
}
5058

5159
$webhookConfig = app(WebhookConfigRepository::class)->getConfig($configName);
5260

tests/WebhookControllerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ public function it_can_store_none_of_the_headers()
207207
/** @test */
208208
public function multiple_routes_can_share_configuration()
209209
{
210+
config()->set('webhook-client.add_unique_token_to_route_name', true);
211+
210212
Route::webhooks('incoming-webhooks-additional');
211213

212214
$this->refreshWebhookConfigRepository();

0 commit comments

Comments
 (0)