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