1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Actuator\Silex\Provider; |
4
|
|
|
|
5
|
|
|
use Actuator\Silex\Controller\HealthController; |
6
|
|
|
use Actuator\Health\Health; |
7
|
|
|
use Actuator\Health\OrderedHealthAggregator; |
8
|
|
|
use Silex\Application; |
9
|
|
|
use Silex\ServiceProviderInterface; |
10
|
|
|
|
11
|
|
|
class HealthServiceProvider implements ServiceProviderInterface |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Registers services on the given app. |
16
|
|
|
* |
17
|
|
|
* This method should only be used to configure services and parameters. |
18
|
|
|
* It should not get services. |
19
|
|
|
* @param Application $app |
20
|
|
|
*/ |
21
|
6 |
|
public function register(Application $app) |
22
|
|
|
{ |
23
|
|
|
$app->view(function (Health $healthResult) use ($app) { |
24
|
6 |
|
$healthDetails = array(); |
25
|
3 |
|
foreach ($healthResult->getDetails() as $key => $healthDetail) { |
26
|
3 |
|
$healthDetails[$key] = array_merge( |
27
|
3 |
|
array('status' => $healthDetail->getStatus()->getCode()), |
28
|
3 |
|
$healthDetail->getDetails() |
29
|
3 |
|
); |
30
|
3 |
|
} |
31
|
3 |
|
$healthDetails = array_merge( |
32
|
3 |
|
array('status' => $healthResult->getStatus()->getCode()), |
33
|
|
|
$healthDetails |
34
|
3 |
|
); |
35
|
|
|
|
36
|
3 |
|
return $app->json($healthDetails); |
37
|
6 |
|
}); |
38
|
|
|
|
39
|
6 |
|
$app['health.aggregator'] = $app->share(function () { |
40
|
6 |
|
return new OrderedHealthAggregator(); |
41
|
6 |
|
}); |
42
|
6 |
|
$app['health.indicators'] = array(); |
43
|
6 |
|
$app['health.endpoint'] = '/health'; |
44
|
6 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Bootstraps the application. |
48
|
|
|
* |
49
|
|
|
* This method is called after all services are registered |
50
|
|
|
* and should be used for "dynamic" configuration (whenever |
51
|
|
|
* a service must be requested). |
52
|
|
|
* @param Application $app |
53
|
|
|
*/ |
54
|
3 |
|
public function boot(Application $app) |
55
|
|
|
{ |
56
|
3 |
|
$app->get($app['health.endpoint'], new HealthController()); |
57
|
3 |
|
} |
58
|
|
|
} |
59
|
|
|
|