Passed
Push — main ( 93c2a6...737a66 )
by Dimitri
03:08
created

ConfigCheck::execute()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 15
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 25
ccs 0
cts 11
cp 0
crap 30
rs 9.4555
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 BlitzPHP\Cli\Console\Command;
15
use Kint\Kint;
16
17
/**
18
 * Verifie les valeurs d'une configuartion.
19
 */
20
class ConfigCheck extends Command
21
{
22
    /**
23
     * @var string Groupe
24
     */
25
    protected $group = 'BlitzPHP';
26
27
    protected $service = 'Service de configuration';
28
29
    /**
30
     * @var string Nom
31
     */
32
    protected $name = 'config:check';
33
34
    /**
35
     * @var string Description
36
     */
37
    protected $description = 'Vérifie les valeurs d\'un fichier de configuration.';
38
39
    /**
40
     * Arguments de la commande
41
     *
42
     * @var array<string, string>
43
     */
44
    protected $arguments = [
45
        'config' => 'La configuration dont on souhaite vérifier les valeurs.',
46
    ];
47
48
    /**
49
     * {@inheritDoc}
50
     */
51
    public function execute(array $params)
52
    {
53
        if (empty($file = strtolower($this->argument('config', '')))) {
54
            $this->fail('Vous devez spécifier la configuration à utiliser pour la vérification.')->eol();
55
            $this->write('  Usage: ' . $this->usage)->eol();
56
            $this->write('Exemple: config:check app')->eol();
57
            $this->write('         config:check \'BlitzPHP\Schild\Config\auth\'');
58
59
            return EXIT_ERROR;
60
        }
61
62
        if (null === $config = config()->get($file)) {
63
            $this->fail('Aucune configuration trouvée pour: ' . $file);
64
        }
65
66
        $this->writer->warn('Valeurs de la configuration ' . $this->color->ok($file));
67
        $this->eol()->border()->eol();
68
69
        if (defined('KINT_DIR') && Kint::$enabled_mode !== false) {
70
            $this->write($this->getKintDump($config));
71
        } else {
72
            $this->colorize($this->getVarDump($config), 'cyan');
73
        }
74
75
        return EXIT_SUCCESS;
76
    }
77
    
78
    /**
79
     * Obtiens le dump de la config via la function d() de Kint
80
     */
81
    private function getKintDump(array $config): string
82
    {
83
        ob_start();
84
        d($config);
85
        $output = ob_get_clean();
86
87
        $output = trim($output);
88
89
        $lines = explode("\n", $output);
90
        array_splice($lines, 0, 3);
91
        array_splice($lines, -3);
92
93
        return implode("\n", $lines);
94
    }
95
96
    /**
97
     * Obtiens le dump de la config via la function var_dump() de PHP
98
     */
99
    private function getVarDump(array $config): string
100
    {
101
        ob_start();
102
        var_dump($config);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($config) looks like debug code. Are you sure you do not want to remove it?
Loading history...
103
        $output = ob_get_clean();
104
105
        return preg_replace(
106
            '!.*Commands/Utilities/ConfigCheck.php.*\n!u',
107
            '',
108
            $output
109
        );
110
    }
111
}
112