|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Leankoala\HealthFoundation\Cli\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Leankoala\HealthFoundation\Check\Check; |
|
6
|
|
|
use Leankoala\HealthFoundation\HealthFoundation; |
|
7
|
|
|
use Leankoala\HealthFoundation\Result\Format\Ietf\IetfFormat; |
|
8
|
|
|
use PhmLabs\Components\Init\Init; |
|
9
|
|
|
use Symfony\Component\Console\Command\Command; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
13
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
class RunCommand extends Command |
|
17
|
|
|
{ |
|
18
|
|
|
protected function configure() |
|
19
|
|
|
{ |
|
20
|
|
|
$this |
|
21
|
|
|
->setDefinition([ |
|
22
|
|
|
new InputArgument('config', InputArgument::REQUIRED, 'the configuration file') |
|
23
|
|
|
]) |
|
24
|
|
|
->setDescription('Run health checks.') |
|
25
|
|
|
->setHelp('The <info>run</info> command for the health checker.') |
|
26
|
|
|
->setName('run'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
30
|
|
|
{ |
|
31
|
|
|
$configFile = $input->getArgument('config'); |
|
32
|
|
|
|
|
33
|
|
|
if (!file_exists($configFile)) { |
|
|
|
|
|
|
34
|
|
|
throw new \RuntimeException('Unable to find config file.'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$configArray = Yaml::parse(file_get_contents($configFile)); |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
$format = $this->initFormat($configArray); |
|
40
|
|
|
$messages = $this->initMessages($configArray); |
|
41
|
|
|
|
|
42
|
|
|
$healthFoundation = $this->initHealthFoundation($configArray); |
|
43
|
|
|
|
|
44
|
|
|
$runResult = $healthFoundation->runHealthCheck(); |
|
45
|
|
|
|
|
46
|
|
|
$format->handle($runResult, $messages['success'], $messages['failure']); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param array $configArray |
|
51
|
|
|
* |
|
52
|
|
|
* @return HealthFoundation |
|
53
|
|
|
*/ |
|
54
|
|
|
private function initHealthFoundation($configArray) |
|
55
|
|
|
{ |
|
56
|
|
|
if (!array_key_exists('checks', $configArray)) { |
|
57
|
|
|
throw new \RuntimeException('The mandatory config element "checks" is missing.'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$healthFoundation = new HealthFoundation(); |
|
61
|
|
|
|
|
62
|
|
|
foreach ($configArray['checks'] as $key => $checkArray) { |
|
63
|
|
|
|
|
64
|
|
|
/** @var Check $check */ |
|
65
|
|
|
$check = Init::initialize($checkArray, 'check'); |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
if (array_key_exists('description', $checkArray)) { |
|
68
|
|
|
$description = $checkArray['description']; |
|
69
|
|
|
} else { |
|
70
|
|
|
$description = ""; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if (array_key_exists('identifier', $checkArray)) { |
|
74
|
|
|
$identifier = $checkArray['identifier']; |
|
75
|
|
|
} else { |
|
76
|
|
|
$identifier = $key; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$healthFoundation->registerCheck($check, $identifier, $description); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $healthFoundation; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
private function initMessages($configArray) |
|
86
|
|
|
{ |
|
87
|
|
|
$successMessage = 'The health check was passed.'; |
|
88
|
|
|
$failureMessage = 'The health check failed'; |
|
89
|
|
|
|
|
90
|
|
|
if (array_key_exists('foundation', $configArray)) { |
|
91
|
|
|
if (array_key_exists('messages', $configArray['foundation'])) { |
|
92
|
|
|
if (array_key_exists('success', $configArray['foundation']['messages'])) { |
|
93
|
|
|
$successMessage = $configArray['foundation']['messages']['success']; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if (array_key_exists('failure', $configArray['foundation']['messages'])) { |
|
97
|
|
|
$failureMessage = $configArray['foundation']['messages']['failure']; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return ['success' => $successMessage, 'failure' => $failureMessage]; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
private function initFormat($configArray) |
|
106
|
|
|
{ |
|
107
|
|
|
if (array_key_exists('format', $configArray)) { |
|
108
|
|
|
$format = Init::initialize($configArray['format']); |
|
109
|
|
|
} else { |
|
110
|
|
|
$format = new IetfFormat(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $format; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|