|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mpociot\CaptainHook; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use GuzzleHttp\ClientInterface; |
|
7
|
|
|
use Illuminate\Support\Facades\Event; |
|
8
|
|
|
use Illuminate\Support\Facades\Schema; |
|
9
|
|
|
use Illuminate\Support\ServiceProvider; |
|
10
|
|
|
use Mpociot\CaptainHook\Commands\AddWebhook; |
|
11
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs; |
|
12
|
|
|
use Mpociot\CaptainHook\Commands\ListWebhooks; |
|
13
|
|
|
use Mpociot\CaptainHook\Commands\DeleteWebhook; |
|
14
|
|
|
use Mpociot\CaptainHook\Jobs\TriggerWebhooksJob; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* This file is part of CaptainHook arrrrr. |
|
18
|
|
|
* |
|
19
|
|
|
* @license MIT |
|
20
|
|
|
*/ |
|
21
|
|
|
class CaptainHookServiceProvider extends ServiceProvider |
|
22
|
|
|
{ |
|
23
|
|
|
use DispatchesJobs; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The registered event listeners. |
|
27
|
|
|
* |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $listeners; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* All registered webhooks. |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $webhooks = []; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var Client |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $client; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var \Illuminate\Contracts\Cache\Repository |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $cache; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var \Illuminate\Contracts\Config\Repository |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $config; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Bootstrap. |
|
55
|
|
|
*/ |
|
56
|
|
|
public function boot() |
|
57
|
|
|
{ |
|
58
|
|
|
$this->client = new Client(); |
|
59
|
|
|
$this->cache = app('Illuminate\Contracts\Cache\Repository'); |
|
60
|
|
|
$this->config = app('Illuminate\Contracts\Config\Repository'); |
|
61
|
|
|
$this->publishMigration(); |
|
62
|
|
|
$this->publishConfig(); |
|
63
|
|
|
$this->publishSparkResources(); |
|
64
|
|
|
$this->listeners = collect($this->config->get('captain_hook.listeners', []))->values(); |
|
|
|
|
|
|
65
|
|
|
$this->registerEventListeners(); |
|
66
|
|
|
$this->registerRoutes(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Register the service provider. |
|
71
|
|
|
* |
|
72
|
|
|
* @return void |
|
73
|
|
|
*/ |
|
74
|
|
|
public function register() |
|
75
|
|
|
{ |
|
76
|
|
|
$this->registerCommands(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Publish migration. |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function publishMigration() |
|
83
|
|
|
{ |
|
84
|
|
|
$migrations = [ |
|
85
|
|
|
__DIR__.'/../../database/2015_10_29_000000_captain_hook_setup_table.php' => |
|
86
|
|
|
database_path('/migrations/2015_10_29_000000_captain_hook_setup_table.php'), |
|
87
|
|
|
__DIR__.'/../../database/2015_10_29_000001_captain_hook_setup_logs_table.php' => |
|
88
|
|
|
database_path('/migrations/2015_10_29_000001_captain_hook_setup_logs_table.php'), |
|
89
|
|
|
]; |
|
90
|
|
|
|
|
91
|
|
|
// To be backwards compatible |
|
92
|
|
|
foreach ($migrations as $migration => $toPath) { |
|
93
|
|
|
preg_match('/_captain_hook_.*\.php/', $migration, $match); |
|
94
|
|
|
$published_migration = glob(database_path('/migrations/*'.$match[0])); |
|
95
|
|
|
if (count($published_migration) !== 0) { |
|
96
|
|
|
unset($migrations[$migration]); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$this->publishes($migrations, 'migrations'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Publish configuration file. |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function publishConfig() |
|
107
|
|
|
{ |
|
108
|
|
|
$this->publishes([ |
|
109
|
|
|
__DIR__.'/../../config/config.php' => config_path('captain_hook.php'), |
|
110
|
|
|
]); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
protected function publishSparkResources() |
|
114
|
|
|
{ |
|
115
|
|
|
$this->loadViewsFrom(__DIR__.'/../../resources/views/', 'captainhook'); |
|
116
|
|
|
|
|
117
|
|
|
$this->publishes([ |
|
118
|
|
|
__DIR__.'/../../resources/assets/js/' => base_path('resources/assets/js/components/'), |
|
119
|
|
|
__DIR__.'/../../resources/views/' => base_path('resources/views/vendor/captainhook/settings/'), |
|
120
|
|
|
], 'spark-resources'); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Register all active event listeners. |
|
125
|
|
|
*/ |
|
126
|
|
|
protected function registerEventListeners() |
|
127
|
|
|
{ |
|
128
|
|
|
foreach ($this->listeners as $eventName) { |
|
129
|
|
|
$this->app['events']->listen($eventName, [$this, 'handleEvent']); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param array $listeners |
|
135
|
|
|
*/ |
|
136
|
|
|
public function setListeners($listeners) |
|
137
|
|
|
{ |
|
138
|
|
|
$this->listeners = $listeners; |
|
139
|
|
|
|
|
140
|
|
|
$this->registerEventListeners(); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param array $webhooks |
|
145
|
|
|
*/ |
|
146
|
|
|
public function setWebhooks($webhooks) |
|
147
|
|
|
{ |
|
148
|
|
|
$this->webhooks = $webhooks; |
|
149
|
|
|
$this->getCache()->rememberForever(Webhook::CACHE_KEY, function () { |
|
150
|
|
|
return $this->webhooks; |
|
151
|
|
|
}); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @return \Illuminate\Support\Collection |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getWebhooks() |
|
158
|
|
|
{ |
|
159
|
|
|
// Check if migration ran |
|
160
|
|
|
if (Schema::hasTable((new Webhook)->getTable())) { |
|
161
|
|
|
return collect($this->getCache()->rememberForever(Webhook::CACHE_KEY, function () { |
|
162
|
|
|
return Webhook::all(); |
|
163
|
|
|
})); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
return collect(); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @return \Illuminate\Contracts\Cache\Repository |
|
171
|
|
|
*/ |
|
172
|
|
|
public function getCache() |
|
173
|
|
|
{ |
|
174
|
|
|
return $this->cache; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @param \Illuminate\Contracts\Cache\Repository $cache |
|
179
|
|
|
*/ |
|
180
|
|
|
public function setCache($cache) |
|
181
|
|
|
{ |
|
182
|
|
|
$this->cache = $cache; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @param ClientInterface $client |
|
187
|
|
|
*/ |
|
188
|
|
|
public function setClient($client) |
|
189
|
|
|
{ |
|
190
|
|
|
$this->client = $client; |
|
|
|
|
|
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @param \Illuminate\Contracts\Config\Repository $config |
|
195
|
|
|
*/ |
|
196
|
|
|
public function setConfig($config) |
|
197
|
|
|
{ |
|
198
|
|
|
$this->config = $config; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Event listener. |
|
203
|
|
|
* |
|
204
|
|
|
* @param $eventData |
|
205
|
|
|
*/ |
|
206
|
|
|
public function handleEvent($eventData) |
|
207
|
|
|
{ |
|
208
|
|
|
$eventName = Event::firing(); |
|
209
|
|
|
$webhooks = $this->getWebhooks()->where('event', $eventName); |
|
210
|
|
|
$webhooks = $webhooks->filter($this->config->get('captain_hook.filter', null)); |
|
211
|
|
|
|
|
212
|
|
|
if (! $webhooks->isEmpty()) { |
|
213
|
|
|
$this->dispatch(new TriggerWebhooksJob($webhooks, $eventData)); |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Register the artisan commands. |
|
219
|
|
|
*/ |
|
220
|
|
|
protected function registerCommands() |
|
221
|
|
|
{ |
|
222
|
|
|
$this->commands( |
|
223
|
|
|
ListWebhooks::class, |
|
224
|
|
|
AddWebhook::class, |
|
225
|
|
|
DeleteWebhook::class |
|
226
|
|
|
); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Register predefined routes used for Spark. |
|
231
|
|
|
*/ |
|
232
|
|
|
protected function registerRoutes() |
|
233
|
|
|
{ |
|
234
|
|
|
if (class_exists('Laravel\Spark\Providers\AppServiceProvider')) { |
|
235
|
|
|
include __DIR__.'/../../routes.php'; |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..