Passed
Push — main ( c6deb1...79ccdf )
by Dimitri
12:23
created

Namespaces::outputBlitzNamespaces()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 14
nc 5
nop 1
dl 0
loc 25
rs 9.4888
c 1
b 0
f 0
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 BlitzPHP\Container\Services;
16
17
/**
18
 * Répertorie les namespace dans Config\autoload.php avec le chemin d'accès du serveur complet. 
19
 * Vous aide à vérifier que vous avez la configuration des namespaces correctement. 
20
 */
21
class Namespaces extends Command
22
{
23
    /**
24
     * @var string Groupe
25
     */
26
    protected $group = 'BlitzPHP';
27
28
	/**
29
     * @var string Nom
30
     */
31
    protected $name = 'namespaces';
32
33
    /**
34
     * @var string Description
35
     */
36
    protected $description = 'Vérifie que vos namespaces sont correctement configurés.';
37
38
    /**
39
     * @var array Options de la commande
40
     */
41
    protected $options = [
42
        '-b' => 'Afficher uniquement les namespaces de la config de BlitzPHP.',
43
        '-r' => 'Afficher chaînes brutes du chemin.',
44
        '-m' => 'Spécifiez la longueur maximale des chaînes de chemin d\'accès à la sortie. Defaut: 60.',
45
    ];
46
47
    /**
48
     * {@inheritDoc}
49
     */
50
    public function execute(array $params)
51
    {
52
        $m = (int) $this->option('m', 60);
53
54
        $tbody = true === $this->option('b') 
55
			? $this->outputBlitzNamespaces($m) 
56
			: $this->outputAllNamespaces($m);
57
58
		$table = [];
59
60
		foreach ($tbody as $namespace) {
61
			$table[] = [
62
				'Namespace' => $namespace[0],
63
				'Chemin'    => $namespace[1],
64
				'Trouvé?'   => $namespace[2] ? 'Oui' : 'Manque',
65
			];
66
		}
67
68
		$this->table($table);
69
    }
70
71
    private function outputAllNamespaces(int $maxLength): array
72
    {
73
        $autoloader = Services::autoloader();
74
75
        $tbody = [];
76
77
        foreach ($autoloader->getNamespace() as $ns => $paths) {
78
            foreach ($paths as $path) {
79
                if (null !== $this->option('r')) {
80
                    $pathOutput = $this->truncate($path, $maxLength);
81
                } else {
82
                    $pathOutput = $this->truncate(clean_path($path), $maxLength);
83
                }
84
85
                $tbody[] = [
86
                    $ns,
87
                    $pathOutput,
88
                    is_dir($path)
89
                ];
90
            }
91
        }
92
93
        return $tbody;
94
    }
95
96
    private function truncate(string $string, int $max): string
97
    {
98
        $length = strlen($string);
99
100
        if ($length > $max) {
101
            return substr($string, 0, $max - 3) . '...';
102
        }
103
104
        return $string;
105
    }
106
107
    private function outputBlitzNamespaces(int $maxLength): array
108
    {
109
        $config = (object) config('autoload');
110
111
        $tbody = [];
112
113
        foreach ($config->psr4 as $ns => $paths) {
0 ignored issues
show
Bug introduced by
The property psr4 does not seem to exist on BlitzPHP\Config\Config.
Loading history...
114
            if (null !== $this->option('r')) {
115
                $pathOutput = $this->truncate($paths, $maxLength);
116
            } else {
117
                $pathOutput = $this->truncate(clean_path($paths), $maxLength);
118
            }
119
120
            foreach ((array) $paths as $path) {
121
                $path = realpath($path) ?: $path;
122
123
                $tbody[] = [
124
                    $ns,
125
                    $pathOutput,
126
                    is_dir($path),
127
                ];
128
            }
129
        }
130
131
        return $tbody;
132
    }
133
}
134