1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PragmaRX\Health; |
4
|
|
|
|
5
|
|
|
use Event; |
6
|
|
|
use Artisan; |
7
|
|
|
use PragmaRX\Yaml\Package\Yaml; |
8
|
|
|
use PragmaRX\Health\Support\Cache; |
9
|
|
|
use Illuminate\Console\Scheduling\Schedule; |
10
|
|
|
use PragmaRX\Health\Support\ResourceLoader; |
11
|
|
|
use PragmaRX\Health\Support\Traits\Routing; |
12
|
|
|
use PragmaRX\Health\Events\RaiseHealthIssue; |
13
|
|
|
use PragmaRX\Health\Support\ResourceChecker; |
14
|
|
|
use PragmaRX\Health\Listeners\NotifyHealthIssue; |
15
|
|
|
use Illuminate\Support\ServiceProvider as IlluminateServiceProvider; |
16
|
|
|
|
17
|
|
|
class ServiceProvider extends IlluminateServiceProvider |
18
|
|
|
{ |
19
|
|
|
use Routing; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The application instance. |
23
|
|
|
* |
24
|
|
|
* @var \Illuminate\Foundation\Application |
25
|
|
|
*/ |
26
|
|
|
protected $app; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Resource loader instance. |
30
|
|
|
* |
31
|
|
|
* @var |
32
|
|
|
*/ |
33
|
|
|
protected $resourceLoader; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The health service. |
37
|
|
|
* |
38
|
|
|
* @var |
39
|
|
|
*/ |
40
|
|
|
private $healthService; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* All artisan commands. |
44
|
|
|
* |
45
|
|
|
* @var |
46
|
|
|
*/ |
47
|
|
|
private $commands; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The router. |
51
|
|
|
* |
52
|
|
|
* @var |
53
|
|
|
*/ |
54
|
|
|
private $router; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Cache closure. |
58
|
|
|
* |
59
|
|
|
* @var |
60
|
|
|
*/ |
61
|
|
|
private $cacheClosure; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Resource checker closure. |
65
|
|
|
* |
66
|
|
|
* @var |
67
|
|
|
*/ |
68
|
|
|
private $resourceCheckerClosure; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Health service closure. |
72
|
|
|
* |
73
|
|
|
* @var |
74
|
|
|
*/ |
75
|
|
|
private $healthServiceClosure; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Configure package paths. |
79
|
|
|
*/ |
80
|
10 |
|
private function configurePaths() |
81
|
|
|
{ |
82
|
10 |
|
$this->publishes( |
83
|
|
|
[ |
84
|
10 |
|
__DIR__.'/config/health.php' => config_path( |
85
|
10 |
|
'health/config.php' |
86
|
|
|
), |
87
|
10 |
|
__DIR__.'/config/resources/' => config_path( |
88
|
10 |
|
'health/resources/' |
89
|
|
|
), |
90
|
|
|
], |
91
|
10 |
|
'config' |
92
|
|
|
); |
93
|
|
|
|
94
|
10 |
|
$this->publishes( |
95
|
|
|
[ |
96
|
10 |
|
__DIR__.'/resources/views/' => resource_path( |
97
|
10 |
|
'views/vendor/pragmarx/health/' |
98
|
|
|
), |
99
|
|
|
], |
100
|
10 |
|
'views' |
101
|
|
|
); |
102
|
|
|
|
103
|
10 |
|
$this->publishes( |
104
|
|
|
[ |
105
|
10 |
|
__DIR__.'/database/migrations/' => database_path( |
106
|
10 |
|
'migrations' |
107
|
|
|
), |
108
|
|
|
], |
109
|
10 |
|
'migrations' |
110
|
|
|
); |
111
|
10 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Configure package folder views. |
115
|
|
|
*/ |
116
|
10 |
|
private function configureViews() |
117
|
|
|
{ |
118
|
10 |
|
$this->loadViewsFrom( |
119
|
10 |
|
realpath(__DIR__.'/resources/views'), |
120
|
10 |
|
'pragmarx/health' |
121
|
|
|
); |
122
|
10 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Create health service. |
126
|
|
|
*/ |
127
|
10 |
|
private function createHealthService() |
128
|
|
|
{ |
129
|
10 |
|
$resourceChecker = call_user_func($this->resourceCheckerClosure); |
130
|
|
|
|
131
|
10 |
|
$cache = call_user_func($this->cacheClosure); |
132
|
|
|
|
133
|
|
|
$this->healthServiceClosure = function () use ( |
134
|
10 |
|
$resourceChecker, |
135
|
10 |
|
$cache |
136
|
|
|
) { |
137
|
10 |
|
return $this->instantiateService($resourceChecker, $cache); |
138
|
|
|
}; |
139
|
|
|
|
140
|
10 |
|
$this->healthService = call_user_func($this->healthServiceClosure); |
141
|
10 |
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Create resource checker. |
145
|
|
|
*/ |
146
|
10 |
|
private function createResourceChecker() |
147
|
|
|
{ |
148
|
10 |
|
$this->resourceLoader = new ResourceLoader(new Yaml()); |
149
|
|
|
|
150
|
10 |
|
$this->cacheClosure = $this->getCacheClosure(); |
151
|
|
|
|
152
|
10 |
|
$this->resourceCheckerClosure = $this->getResourceCheckerClosure( |
153
|
10 |
|
$this->resourceLoader, |
154
|
10 |
|
call_user_func($this->cacheClosure) |
155
|
|
|
); |
156
|
10 |
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get the cache closure for instantiation. |
160
|
|
|
* |
161
|
|
|
* @return \Closure |
162
|
|
|
*/ |
163
|
10 |
|
private function getCacheClosure() |
164
|
|
|
{ |
165
|
|
|
$cacheClosure = function () { |
166
|
10 |
|
return new Cache(); |
167
|
10 |
|
}; |
168
|
|
|
|
169
|
10 |
|
return $cacheClosure; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Get the resource checker closure for instantiation. |
174
|
|
|
* |
175
|
|
|
* @param $resourceLoader |
176
|
|
|
* @param $cache |
177
|
|
|
* @return \Closure |
178
|
|
|
*/ |
179
|
10 |
|
private function getResourceCheckerClosure($resourceLoader, $cache) |
180
|
|
|
{ |
181
|
|
|
$resourceCheckerInstance = function () use ($resourceLoader, $cache) { |
182
|
10 |
|
return new ResourceChecker($resourceLoader, $cache); |
183
|
10 |
|
}; |
184
|
|
|
|
185
|
10 |
|
return $resourceCheckerInstance; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Get the list of routes. |
190
|
|
|
* |
191
|
|
|
* @return array |
192
|
|
|
*/ |
193
|
10 |
|
private function getRoutes() |
194
|
|
|
{ |
195
|
10 |
|
return config('health.routes.list'); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Instantiate commands. |
200
|
|
|
* |
201
|
|
|
* @return \Illuminate\Foundation\Application|mixed |
202
|
|
|
*/ |
203
|
10 |
|
private function instantiateCommands() |
204
|
|
|
{ |
205
|
10 |
|
return $this->commands = instantiate(Commands::class, [ |
206
|
10 |
|
$this->healthService, |
207
|
|
|
]); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Instantiate the main service. |
212
|
|
|
* |
213
|
|
|
* @param $resourceChecker |
214
|
|
|
* @param $cache |
215
|
|
|
* @return Service |
216
|
|
|
*/ |
217
|
10 |
|
private function instantiateService($resourceChecker, $cache) |
218
|
|
|
{ |
219
|
10 |
|
return $this->healthService = new Service($resourceChecker, $cache); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Merge configuration. |
224
|
|
|
*/ |
225
|
10 |
|
private function mergeConfig() |
226
|
|
|
{ |
227
|
10 |
|
if (file_exists(config_path('/health/config.php'))) { |
228
|
|
|
$this->mergeConfigFrom(config_path('/health/config.php'), 'health'); |
229
|
|
|
} |
230
|
|
|
|
231
|
10 |
|
$this->mergeConfigFrom(__DIR__.'/config/health.php', 'health'); |
232
|
|
|
|
233
|
10 |
|
$this->addDistPathToConfig(); |
234
|
10 |
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Register any application services. |
238
|
|
|
* |
239
|
|
|
* @return void |
240
|
|
|
*/ |
241
|
10 |
|
public function register() |
242
|
|
|
{ |
243
|
10 |
|
$this->mergeConfig(); |
244
|
|
|
|
245
|
10 |
|
$this->configureViews(); |
246
|
|
|
|
247
|
10 |
|
$this->configurePaths(); |
248
|
|
|
|
249
|
10 |
|
$this->registerServices(); |
250
|
|
|
|
251
|
10 |
|
$this->registerRoutes(); |
252
|
|
|
|
253
|
10 |
|
$this->registerTasks(); |
254
|
|
|
|
255
|
10 |
|
$this->registerEventListeners(); |
256
|
|
|
|
257
|
10 |
|
$this->registerConsoleCommands(); |
258
|
10 |
|
} |
259
|
|
|
|
260
|
10 |
|
private function registerResourcesRoutes() |
261
|
|
|
{ |
262
|
|
|
collect($this->resourceLoader->getResources())->each(function ($item) { |
263
|
10 |
|
if (isset($item['routes'])) { |
264
|
|
|
collect($item['routes'])->each(function ($route, $key) { |
265
|
|
|
$this->registerRoute($route, $key); |
266
|
|
|
}); |
267
|
|
|
} |
268
|
10 |
|
}); |
269
|
10 |
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Register console commands. |
273
|
|
|
*/ |
274
|
10 |
|
private function registerConsoleCommands() |
275
|
|
|
{ |
276
|
10 |
|
$commands = $this->commands; |
277
|
|
|
|
278
|
|
|
Artisan::command('health:panel', function () use ($commands) { |
279
|
|
|
$commands->panel($this); |
280
|
10 |
|
})->describe('Show all resources and their current health states.'); |
281
|
|
|
|
282
|
|
|
Artisan::command('health:check', function () use ($commands) { |
283
|
|
|
$commands->check($this); |
284
|
10 |
|
})->describe('Check resources health and send error notifications.'); |
285
|
10 |
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Register event listeners. |
289
|
|
|
*/ |
290
|
10 |
|
private function registerEventListeners() |
291
|
|
|
{ |
292
|
10 |
|
Event::listen(RaiseHealthIssue::class, NotifyHealthIssue::class); |
293
|
10 |
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Register routes. |
297
|
|
|
*/ |
298
|
10 |
|
private function registerRoutes() |
299
|
|
|
{ |
300
|
|
|
collect($routes = $this->getRoutes())->each(function ($route) { |
301
|
10 |
|
$this->registerRoute($route); |
302
|
10 |
|
}); |
303
|
|
|
|
304
|
10 |
|
$this->registerResourcesRoutes(); |
305
|
10 |
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Register service. |
309
|
|
|
*/ |
310
|
10 |
|
private function registerServices() |
311
|
|
|
{ |
312
|
10 |
|
$this->createServices(); |
313
|
|
|
|
314
|
10 |
|
$this->app->singleton('pragmarx.health.cache', $this->cacheClosure); |
315
|
|
|
|
316
|
10 |
|
$this->app->singleton( |
317
|
10 |
|
'pragmarx.health.resource.checker', |
318
|
10 |
|
$this->resourceCheckerClosure |
319
|
|
|
); |
320
|
|
|
|
321
|
10 |
|
$this->app->singleton('pragmarx.health', $this->healthServiceClosure); |
322
|
|
|
|
323
|
10 |
|
$this->app->singleton( |
324
|
10 |
|
'pragmarx.health.commands', |
325
|
10 |
|
$this->instantiateCommands() |
326
|
|
|
); |
327
|
10 |
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Create services. |
331
|
|
|
*/ |
332
|
10 |
|
public function createServices() |
333
|
|
|
{ |
334
|
10 |
|
$this->createResourceChecker(); |
335
|
|
|
|
336
|
10 |
|
$this->createHealthService(); |
337
|
10 |
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Register scheduled tasks. |
341
|
|
|
*/ |
342
|
10 |
|
private function registerTasks() |
343
|
|
|
{ |
344
|
|
|
if ( |
345
|
10 |
|
config('health.scheduler.enabled') && |
346
|
10 |
|
($frequency = config('health.scheduler.frequency')) && |
347
|
10 |
|
config('health.notifications.enabled') |
348
|
|
|
) { |
349
|
|
|
$scheduler = instantiate(Schedule::class); |
350
|
|
|
|
351
|
|
|
$scheduler |
352
|
|
|
->call($this->healthService->getSilentChecker()) |
353
|
|
|
->$frequency(); |
354
|
|
|
} |
355
|
10 |
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Get the services provided by the provider. |
359
|
|
|
* |
360
|
|
|
* @return array |
361
|
|
|
*/ |
362
|
|
|
public function provides() |
363
|
|
|
{ |
364
|
|
|
return [ |
365
|
|
|
'pragmarx.health.cache', |
366
|
|
|
'pragmarx.health.resource.checker', |
367
|
|
|
'pragmarx.health', |
368
|
|
|
'pragmarx.health.commands', |
369
|
|
|
]; |
370
|
|
|
} |
371
|
|
|
|
372
|
10 |
|
public function addDistPathToConfig() |
373
|
|
|
{ |
374
|
10 |
|
config(['health.dist_path' => __DIR__.'/resources/dist']); |
375
|
10 |
|
} |
376
|
|
|
} |
377
|
|
|
|