1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This file is part of Blitz PHP framework. |
||
5 | * |
||
6 | * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view |
||
9 | * the LICENSE file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace BlitzPHP\Cli\Commands\Utilities; |
||
13 | |||
14 | use Ahc\Cli\Output\Color; |
||
15 | use BlitzPHP\Cli\Console\Command; |
||
16 | use Kint\Kint; |
||
17 | |||
18 | /** |
||
19 | * Verifie les valeurs d'une configuartion. |
||
20 | */ |
||
21 | class ConfigCheck extends Command |
||
22 | { |
||
23 | /** |
||
24 | * @var string Groupe |
||
25 | */ |
||
26 | protected $group = 'BlitzPHP'; |
||
27 | |||
28 | protected $service = 'Service de configuration'; |
||
29 | |||
30 | /** |
||
31 | * @var string Nom |
||
32 | */ |
||
33 | protected $name = 'config:check'; |
||
34 | |||
35 | /** |
||
36 | * @var string Description |
||
37 | */ |
||
38 | protected $description = 'Vérifie les valeurs d\'un fichier de configuration.'; |
||
39 | |||
40 | /** |
||
41 | * Arguments de la commande |
||
42 | * |
||
43 | * @var array<string, string> |
||
44 | */ |
||
45 | protected $arguments = [ |
||
46 | 'config' => 'La configuration dont on souhaite vérifier les valeurs.', |
||
47 | ]; |
||
48 | |||
49 | /** |
||
50 | * {@inheritDoc} |
||
51 | */ |
||
52 | public function execute(array $params) |
||
53 | { |
||
54 | $file = strtolower($this->argument('config', '')); |
||
55 | |||
56 | if ($file === '' || $file === '0') { |
||
57 | $this->fail('Vous devez spécifier la configuration à utiliser pour la vérification.')->eol(); |
||
58 | $this->write(' Usage: ' . $this->usage)->eol(); |
||
59 | $this->write('Exemple: config:check app')->eol(); |
||
60 | $this->write(' config:check \'BlitzPHP\Schild\Config\auth\''); |
||
61 | |||
62 | return EXIT_ERROR; |
||
63 | } |
||
64 | |||
65 | if (null === $config = config()->get($file)) { |
||
66 | $this->fail('Aucune configuration trouvée pour: ' . $file); |
||
67 | } |
||
68 | |||
69 | $this->center('Valeurs de la configuration ' . $this->color->ok($file)); |
||
70 | |||
71 | $this->border()->eol(); |
||
72 | |||
73 | $others = []; |
||
74 | |||
75 | foreach ($config as $key => $val) { |
||
76 | $options = ['fg' => Color::CYAN]; |
||
77 | |||
78 | if (is_scalar($val)) { |
||
79 | if (is_bool($val)) { |
||
80 | if ($val === true) { |
||
81 | $options['fg'] = Color::GREEN; |
||
82 | $val = 'Enabled'; |
||
83 | } else { |
||
84 | $options['fg'] = Color::YELLOW; |
||
85 | $val = 'Disabled'; |
||
86 | } |
||
87 | } elseif ('' === $val) { |
||
88 | $others[$key] = $val; |
||
89 | |||
90 | continue; |
||
91 | } |
||
92 | |||
93 | $this->justify($key, $val, ['second' => $options]); |
||
94 | } else { |
||
95 | if (($val = (array) $val) === []) { |
||
96 | $others[$key] = $val; |
||
97 | |||
98 | continue; |
||
99 | } |
||
100 | if (array_is_list($val)) { |
||
101 | $options = ['fg' => Color::PURPLE]; |
||
102 | $this->justify($key, implode(', ', array_values($val)), ['second' => $options]); |
||
103 | } else { |
||
104 | $others[$key] = $val; |
||
105 | |||
106 | continue; |
||
107 | } |
||
108 | } |
||
109 | } |
||
110 | |||
111 | if ($others !== []) { |
||
112 | $this->eol()->task('Autres configuration')->eol(); |
||
113 | |||
114 | if (defined('KINT_DIR') && Kint::$enabled_mode !== false) { |
||
115 | $this->write($this->getKintDump($others)); |
||
116 | } else { |
||
117 | $this->write( |
||
118 | $this->color->line($this->getVarDump($others), ['fg' => Color::CYAN]) |
||
119 | ); |
||
120 | } |
||
121 | } |
||
122 | |||
123 | return EXIT_SUCCESS; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Obtiens le dump de la config via la function d() de Kint |
||
128 | */ |
||
129 | private function getKintDump(array $config): string |
||
130 | { |
||
131 | ob_start(); |
||
132 | d($config); |
||
133 | $output = ob_get_clean(); |
||
134 | |||
135 | $output = trim($output); |
||
136 | |||
137 | $lines = explode("\n", $output); |
||
138 | array_splice($lines, 0, 3); |
||
139 | array_splice($lines, -3); |
||
140 | |||
141 | return implode("\n", $lines); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Obtiens le dump de la config via la function var_dump() de PHP |
||
146 | */ |
||
147 | private function getVarDump(array $config): string |
||
148 | { |
||
149 | ob_start(); |
||
150 | var_dump($config); |
||
0 ignored issues
–
show
Security
Debugging Code
introduced
by
![]() |
|||
151 | $output = ob_get_clean(); |
||
152 | |||
153 | return preg_replace( |
||
154 | '!.*Commands/Utilities/ConfigCheck.php.*\n!u', |
||
155 | '', |
||
156 | $output |
||
157 | ); |
||
158 | } |
||
159 | } |
||
160 |