|
14 | 14 | use CodeIgniter\CLI\BaseCommand; |
15 | 15 | use CodeIgniter\CLI\CLI; |
16 | 16 | use Config\Autoload; |
| 17 | +use Config\Services; |
17 | 18 |
|
18 | 19 | /** |
19 | 20 | * Lists namespaces set in Config\Autoload with their |
@@ -63,33 +64,94 @@ class Namespaces extends BaseCommand |
63 | 64 | * |
64 | 65 | * @var array |
65 | 66 | */ |
66 | | - protected $options = []; |
| 67 | + protected $options = [ |
| 68 | + '-c' => 'Show only CodeIgniter config namespaces.', |
| 69 | + '-r' => 'Show raw path strings.', |
| 70 | + '-m' => 'Specify max length of the path strings to output. Default: 60.', |
| 71 | + ]; |
67 | 72 |
|
68 | 73 | /** |
69 | 74 | * Displays the help for the spark cli script itself. |
70 | 75 | */ |
71 | 76 | public function run(array $params) |
72 | 77 | { |
| 78 | + $params['m'] = (int) ($params['m'] ?? 60); |
| 79 | + |
| 80 | + if (array_key_exists('c', $params)) { |
| 81 | + $tbody = $this->outputCINamespaces($params); |
| 82 | + } else { |
| 83 | + $tbody = $this->outputAllNamespaces($params); |
| 84 | + } |
| 85 | + |
| 86 | + $thead = [ |
| 87 | + 'Namespace', |
| 88 | + 'Path', |
| 89 | + 'Found?', |
| 90 | + ]; |
| 91 | + |
| 92 | + CLI::table($tbody, $thead); |
| 93 | + } |
| 94 | + |
| 95 | + private function outputAllNamespaces(array $params): array |
| 96 | + { |
| 97 | + $maxLength = $params['m']; |
| 98 | + |
| 99 | + $autoloader = Services::autoloader(); |
| 100 | + |
| 101 | + $tbody = []; |
| 102 | + |
| 103 | + foreach ($autoloader->getNamespace() as $ns => $paths) { |
| 104 | + foreach ($paths as $path) { |
| 105 | + if (array_key_exists('r', $params)) { |
| 106 | + $pathOutput = $this->truncate($path, $maxLength); |
| 107 | + } else { |
| 108 | + $pathOutput = $this->truncate(clean_path($path), $maxLength); |
| 109 | + } |
| 110 | + |
| 111 | + $tbody[] = [ |
| 112 | + $ns, |
| 113 | + $pathOutput, |
| 114 | + is_dir($path) ? 'Yes' : 'MISSING', |
| 115 | + ]; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return $tbody; |
| 120 | + } |
| 121 | + |
| 122 | + private function truncate(string $string, int $max): string |
| 123 | + { |
| 124 | + $length = strlen($string); |
| 125 | + |
| 126 | + if ($length > $max) { |
| 127 | + return substr($string, 0, $max - 3) . '...'; |
| 128 | + } |
| 129 | + |
| 130 | + return $string; |
| 131 | + } |
| 132 | + |
| 133 | + private function outputCINamespaces(array $params): array |
| 134 | + { |
| 135 | + $maxLength = $params['m']; |
| 136 | + |
73 | 137 | $config = new Autoload(); |
74 | 138 |
|
75 | 139 | $tbody = []; |
76 | 140 |
|
77 | 141 | foreach ($config->psr4 as $ns => $path) { |
78 | | - $path = realpath($path) ?: $path; |
| 142 | + if (array_key_exists('r', $params)) { |
| 143 | + $pathOutput = $this->truncate($path, $maxLength); |
| 144 | + } else { |
| 145 | + $pathOutput = $this->truncate(clean_path($path), $maxLength); |
| 146 | + } |
79 | 147 |
|
80 | 148 | $tbody[] = [ |
81 | 149 | $ns, |
82 | | - realpath($path) ?: $path, |
| 150 | + $pathOutput, |
83 | 151 | is_dir($path) ? 'Yes' : 'MISSING', |
84 | 152 | ]; |
85 | 153 | } |
86 | 154 |
|
87 | | - $thead = [ |
88 | | - 'Namespace', |
89 | | - 'Path', |
90 | | - 'Found?', |
91 | | - ]; |
92 | | - |
93 | | - CLI::table($tbody, $thead); |
| 155 | + return $tbody; |
94 | 156 | } |
95 | 157 | } |
0 commit comments