1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeyondCode\SelfDiagnosis; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use BeyondCode\SelfDiagnosis\Checks\Check; |
8
|
|
|
use Illuminate\Contracts\Foundation\Application; |
9
|
|
|
|
10
|
|
|
class SelfDiagnosisCommand extends Command |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The console command name. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $signature = 'self-diagnosis'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The console command description. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $description = 'Perform application self diagnosis.'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Messages stack. |
28
|
|
|
* |
29
|
|
|
* @var string[] |
30
|
|
|
*/ |
31
|
|
|
private $messages = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Execute the console command. |
35
|
|
|
* |
36
|
|
|
* @param Application $app |
37
|
|
|
* |
38
|
|
|
* @return int |
39
|
|
|
*/ |
40
|
|
|
public function handle(Application $app) |
41
|
|
|
{ |
42
|
|
|
$this->runChecks( |
43
|
|
|
config('self-diagnosis.checks', []), |
44
|
|
|
trans('self-diagnosis::commands.self_diagnosis.common_checks') |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
$environmentChecks = config('self-diagnosis.environment_checks.' . $app->environment(), []); |
48
|
|
|
if (empty($environmentChecks) && array_key_exists($app->environment(), config('self-diagnosis.environment_aliases'))) { |
49
|
|
|
$environment = config('self-diagnosis.environment_aliases.' . $app->environment()); |
50
|
|
|
$environmentChecks = config('self-diagnosis.environment_checks.' . $environment, []); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->runChecks($environmentChecks, trans('self-diagnosis::commands.self_diagnosis.environment_specific_checks', ['environment' => $app->environment()])); |
54
|
|
|
|
55
|
|
|
if (\count($this->messages)) { |
56
|
|
|
$this->error(trans('self-diagnosis::commands.self_diagnosis.failed_checks')); |
57
|
|
|
|
58
|
|
|
foreach ($this->messages as $message) { |
59
|
|
|
$this->output->writeln('<fg=red>'.$message.'</fg=red>' . PHP_EOL); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return 1; // Exit WITH error (EXIT_CODE != 0) |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->info(trans('self-diagnosis::commands.self_diagnosis.success')); |
66
|
|
|
|
67
|
|
|
return 0; // Exit without error (EXIT_CODE = 0) |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Run checks. |
72
|
|
|
* |
73
|
|
|
* @param array $checks |
74
|
|
|
* @param string $title |
75
|
|
|
*/ |
76
|
|
|
protected function runChecks(array $checks, string $title) |
77
|
|
|
{ |
78
|
|
|
$max = \count($checks); |
79
|
|
|
$current = 1; |
80
|
|
|
|
81
|
|
|
$this->drawBox($title); |
82
|
|
|
|
83
|
|
|
foreach ($checks as $check => $config) { |
84
|
|
|
if (is_numeric($check)) { |
85
|
|
|
$check = $config; |
86
|
|
|
$config = []; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$checkClass = app($check); |
90
|
|
|
|
91
|
|
|
$this->output->write(trans('self-diagnosis::commands.self_diagnosis.running_check', [ |
92
|
|
|
'current' => $current, |
93
|
|
|
'max' => $max, |
94
|
|
|
'name' => $checkClass->name($config), |
95
|
|
|
])); |
96
|
|
|
|
97
|
|
|
$this->runCheck($checkClass, $config); |
98
|
|
|
|
99
|
|
|
$current++; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$this->output->writeln(''); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Draw message in a box. |
107
|
|
|
* |
108
|
|
|
* @param string $title |
109
|
|
|
*/ |
110
|
|
|
protected function drawBox(string $title) |
111
|
|
|
{ |
112
|
|
|
$length = Str::length(strip_tags($title)) + 8; |
113
|
|
|
|
114
|
|
|
$this->output->writeln('|' . str_repeat('-', $length) . '|'); |
115
|
|
|
$this->output->writeln('| ' . $title . ' |'); |
116
|
|
|
$this->output->writeln('|' . str_repeat('-', $length) . '|'); |
117
|
|
|
|
118
|
|
|
$this->output->newLine(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Make check run. |
123
|
|
|
* |
124
|
|
|
* @param Check $check |
125
|
|
|
* @param array $config |
126
|
|
|
*/ |
127
|
|
|
protected function runCheck(Check $check, array $config) |
128
|
|
|
{ |
129
|
|
|
if ($check->check($config)) { |
130
|
|
|
$this->output->write('<fg=green>✔</fg=green>'); |
131
|
|
|
} else { |
132
|
|
|
$this->output->write('<fg=red>✘</fg=red>'); |
133
|
|
|
|
134
|
|
|
$this->messages[] = $check->message($config); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$this->output->write(PHP_EOL); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|