1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\WebhookClient; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Route; |
6
|
|
|
use Illuminate\Support\ServiceProvider; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Spatie\WebhookClient\Exceptions\InvalidConfig; |
9
|
|
|
|
10
|
|
|
class WebhookClientServiceProvider extends ServiceProvider |
11
|
|
|
{ |
12
|
|
|
public function boot() |
13
|
|
|
{ |
14
|
|
|
if ($this->app->runningInConsole()) { |
15
|
|
|
$this->publishes([ |
16
|
|
|
__DIR__.'/../config/webhook-client.php' => config_path('webhook-client.php'), |
17
|
|
|
], 'config'); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
if (! class_exists('CreateWebhookCallsTable')) { |
21
|
|
|
$timestamp = date('Y_m_d_His', time()); |
22
|
|
|
|
23
|
|
|
$this->publishes([ |
24
|
|
|
__DIR__.'/../database/migrations/create_webhook_calls_table.php.stub' => database_path("migrations/{$timestamp}_create_webhook_calls_table.php"), |
25
|
|
|
], 'migrations'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
Route::macro('webhooks', fn (string $url, string $name = 'default') => Route::post($url, '\Spatie\WebhookClient\WebhookController')->name("webhook-client-{$name}")); |
|
|
|
|
29
|
|
|
|
30
|
|
|
$this->app->singleton(WebhookConfigRepository::class, function () { |
31
|
|
|
$configRepository = new WebhookConfigRepository(); |
32
|
|
|
|
33
|
|
|
collect(config('webhook-client.configs')) |
34
|
|
|
->map(fn (array $config) => new WebhookConfig($config)) |
35
|
|
|
->each(function (WebhookConfig $webhookConfig) use ($configRepository) { |
36
|
|
|
$configRepository->addConfig($webhookConfig); |
37
|
|
|
}); |
38
|
|
|
|
39
|
|
|
return $configRepository; |
40
|
|
|
}); |
41
|
|
|
|
42
|
|
|
$this->app->bind(WebhookConfig::class, function () { |
43
|
|
|
$routeName = Route::currentRouteName(); |
44
|
|
|
|
45
|
|
|
$configName = Str::after($routeName, 'webhook-client-'); |
46
|
|
|
|
47
|
|
|
$webhookConfig = app(WebhookConfigRepository::class)->getConfig($configName); |
48
|
|
|
|
49
|
|
|
if (is_null($webhookConfig)) { |
50
|
|
|
throw InvalidConfig::couldNotFindConfig($configName); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $webhookConfig; |
54
|
|
|
}); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function register() |
58
|
|
|
{ |
59
|
|
|
$this->mergeConfigFrom(__DIR__.'/../config/webhook-client.php', 'webhook-client'); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|