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