Skip to content

Commit bf78a96

Browse files
committed
feat: improve namespaces command
1 parent 6dbe633 commit bf78a96

2 files changed

Lines changed: 155 additions & 10 deletions

File tree

system/Commands/Utilities/Namespaces.php

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use CodeIgniter\CLI\BaseCommand;
1515
use CodeIgniter\CLI\CLI;
1616
use Config\Autoload;
17+
use Config\Services;
1718

1819
/**
1920
* Lists namespaces set in Config\Autoload with their
@@ -63,33 +64,94 @@ class Namespaces extends BaseCommand
6364
*
6465
* @var array
6566
*/
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+
];
6772

6873
/**
6974
* Displays the help for the spark cli script itself.
7075
*/
7176
public function run(array $params)
7277
{
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+
73137
$config = new Autoload();
74138

75139
$tbody = [];
76140

77141
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+
}
79147

80148
$tbody[] = [
81149
$ns,
82-
realpath($path) ?: $path,
150+
$pathOutput,
83151
is_dir($path) ? 'Yes' : 'MISSING',
84152
];
85153
}
86154

87-
$thead = [
88-
'Namespace',
89-
'Path',
90-
'Found?',
91-
];
92-
93-
CLI::table($tbody, $thead);
155+
return $tbody;
94156
}
95157
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
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 CodeIgniter\Commands\Utilities;
13+
14+
use CodeIgniter\Test\CIUnitTestCase;
15+
use CodeIgniter\Test\Filters\CITestStreamFilter;
16+
17+
/**
18+
* @internal
19+
*/
20+
final class NamespacesTest extends CIUnitTestCase
21+
{
22+
private $streamFilter;
23+
24+
protected function setUp(): void
25+
{
26+
$this->resetServices();
27+
28+
parent::setUp();
29+
30+
CITestStreamFilter::$buffer = '';
31+
32+
$this->streamFilter = stream_filter_append(STDOUT, 'CITestStreamFilter');
33+
$this->streamFilter = stream_filter_append(STDERR, 'CITestStreamFilter');
34+
}
35+
36+
protected function tearDown(): void
37+
{
38+
stream_filter_remove($this->streamFilter);
39+
40+
$this->resetServices();
41+
}
42+
43+
protected function getBuffer()
44+
{
45+
return CITestStreamFilter::$buffer;
46+
}
47+
48+
public function testNamespacesCommandCodeIgniterOnly()
49+
{
50+
command('namespaces -c');
51+
52+
$expected = <<<'EOL'
53+
+---------------+-------------------------+--------+
54+
| Namespace | Path | Found? |
55+
+---------------+-------------------------+--------+
56+
| CodeIgniter | ROOTPATH/system | Yes |
57+
| App | ROOTPATH/app | Yes |
58+
| Config | APPPATH/Config | Yes |
59+
| Tests\Support | ROOTPATH/tests/_support | Yes |
60+
+---------------+-------------------------+--------+
61+
EOL;
62+
63+
$this->assertStringContainsString($expected, $this->getBuffer());
64+
}
65+
66+
public function testNamespacesCommandAllNamespaces()
67+
{
68+
command('namespaces');
69+
70+
$this->assertStringContainsString(
71+
'|CodeIgniter|ROOTPATH/system|Yes|',
72+
str_replace(' ', '', $this->getBuffer())
73+
);
74+
$this->assertStringContainsString(
75+
'|App|ROOTPATH/app|Yes|',
76+
str_replace(' ', '', $this->getBuffer())
77+
);
78+
$this->assertStringContainsString(
79+
'|Config|APPPATH/Config|Yes|',
80+
str_replace(' ', '', $this->getBuffer())
81+
);
82+
}
83+
}

0 commit comments

Comments
 (0)