Skip to content

Commit 389974e

Browse files
committed
feat: add config:check command to check Config vaules
1 parent b3db566 commit 389974e

File tree

2 files changed

+358
-0
lines changed

2 files changed

+358
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[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 CodeIgniter\Commands\Utilities;
13+
14+
use CodeIgniter\CLI\BaseCommand;
15+
use CodeIgniter\CLI\CLI;
16+
use CodeIgniter\Config\BaseConfig;
17+
use Kint\Kint;
18+
19+
/**
20+
* Check the Config values.
21+
*
22+
* @see \CodeIgniter\Commands\Utilities\ConfigCheckTest
23+
*/
24+
final class ConfigCheck extends BaseCommand
25+
{
26+
/**
27+
* The group the command is lumped under
28+
* when listing commands.
29+
*
30+
* @var string
31+
*/
32+
protected $group = 'CodeIgniter';
33+
34+
/**
35+
* The Command's name
36+
*
37+
* @var string
38+
*/
39+
protected $name = 'config:check';
40+
41+
/**
42+
* The Command's short description
43+
*
44+
* @var string
45+
*/
46+
protected $description = 'Check your Config values.';
47+
48+
/**
49+
* The Command's usage
50+
*
51+
* @var string
52+
*/
53+
protected $usage = 'config:check <classname>';
54+
55+
/**
56+
* The Command's arguments
57+
*
58+
* @var array<string, string>
59+
*/
60+
protected $arguments = [
61+
'classname' => 'The config classname to check. Short classname or FQCN.',
62+
];
63+
64+
/**
65+
* The Command's options
66+
*
67+
* @var array<string, string>
68+
*/
69+
protected $options = [];
70+
71+
/**
72+
* {@inheritDoc}
73+
*/
74+
public function run(array $params)
75+
{
76+
if (! isset($params[0])) {
77+
CLI::error('You must specify a Config classname.');
78+
CLI::write(' Usage: ' . $this->usage);
79+
CLI::write('Example: config:check App');
80+
CLI::write(' config:check \'CodeIgniter\Shield\Config\Auth\'');
81+
82+
return EXIT_ERROR;
83+
}
84+
85+
/** @var class-string<BaseConfig> $class */
86+
$class = $params[0];
87+
88+
$config = config($class);
89+
90+
if ($config === null) {
91+
CLI::error('No such Config class: ' . $class);
92+
93+
return EXIT_ERROR;
94+
}
95+
96+
if (defined('KINT_DIR') && Kint::$enabled_mode !== false) {
97+
CLI::write($this->getKintD($config));
98+
} else {
99+
CLI::write(
100+
CLI::color($this->getVarDump($config), 'cyan')
101+
);
102+
}
103+
104+
return EXIT_SUCCESS;
105+
}
106+
107+
/**
108+
* Gets object dump by Kint d()
109+
*/
110+
private function getKintD(object $config): string
111+
{
112+
ob_start();
113+
d($config);
114+
$output = ob_get_clean();
115+
116+
$output = trim($output);
117+
$output = preg_replace(
118+
'/\x1b\[36m.*┘\x1b\[0m/su',
119+
'',
120+
$output
121+
);
122+
$output = preg_replace(
123+
'/\x1b\[36m.*Called from .*\x1b\[0m/su',
124+
'',
125+
$output
126+
);
127+
128+
return trim($output);
129+
}
130+
131+
/**
132+
* Gets object dump by var_dump()
133+
*/
134+
private function getVarDump(object $config): string
135+
{
136+
ob_start();
137+
var_dump($config);
138+
$output = ob_get_clean();
139+
140+
return preg_replace(
141+
'!.*system/Commands/Utilities/ConfigCheck.php.*\n!u',
142+
'',
143+
$output
144+
);
145+
}
146+
}
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[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 CodeIgniter\Commands\Utilities;
13+
14+
use CodeIgniter\Test\CIUnitTestCase;
15+
use CodeIgniter\Test\StreamFilterTrait;
16+
use Config\App;
17+
use Config\Services;
18+
19+
/**
20+
* @internal
21+
*
22+
* @group Others
23+
*/
24+
final class ConfigCheckTest extends CIUnitTestCase
25+
{
26+
use StreamFilterTrait;
27+
28+
protected function setUp(): void
29+
{
30+
$this->resetServices();
31+
parent::setUp();
32+
}
33+
34+
protected function tearDown(): void
35+
{
36+
$this->resetServices();
37+
parent::tearDown();
38+
}
39+
40+
protected function getBuffer()
41+
{
42+
return $this->getStreamFilterBuffer();
43+
}
44+
45+
public function testCommandConfigCheckNoArg(): void
46+
{
47+
command('config:check');
48+
49+
$this->assertStringContainsString(
50+
'You must specify a Config classname.',
51+
$this->getBuffer()
52+
);
53+
}
54+
55+
public function testCommandConfigCheckApp(): void
56+
{
57+
command('config:check App');
58+
59+
$this->assertStringContainsString(App::class, $this->getBuffer());
60+
$this->assertStringContainsString("public 'baseURL", $this->getBuffer());
61+
}
62+
63+
public function testCommandConfigCheckNonexistentClass(): void
64+
{
65+
command('config:check Nonexistent');
66+
67+
$this->assertStringContainsString(
68+
'No such Config class: Nonexistent',
69+
$this->getBuffer()
70+
);
71+
}
72+
73+
public function testGetKintD()
74+
{
75+
$command = new ConfigCheck(Services::logger(), Services::commands());
76+
$getKintD = $this->getPrivateMethodInvoker($command, 'getKintD');
77+
78+
$output = $getKintD(new App());
79+
80+
$output = preg_replace(
81+
'/(\033\[[0-9;]+m)|(\035\[[0-9;]+m)/u',
82+
'',
83+
$output
84+
);
85+
86+
$this->assertStringContainsString(
87+
'Config\App#',
88+
$output
89+
);
90+
$this->assertStringContainsString(
91+
<<<'EOL'
92+
(
93+
public 'baseURL' -> string (19) "http://example.com/"
94+
public 'allowedHostnames' -> array (0) []
95+
public 'indexPage' -> string (9) "index.php"
96+
public 'uriProtocol' -> string (11) "REQUEST_URI"
97+
public 'defaultLocale' -> string (2) "en"
98+
public 'negotiateLocale' -> boolean false
99+
public 'supportedLocales' -> array (1) [
100+
0 => string (2) "en"
101+
]
102+
public 'appTimezone' -> string (3) "UTC"
103+
public 'charset' -> string (5) "UTF-8"
104+
public 'forceGlobalSecureRequests' -> boolean false
105+
public 'proxyIPs' -> array (0) []
106+
public 'CSPEnabled' -> boolean false
107+
EOL,
108+
$output
109+
);
110+
}
111+
112+
public function testGetVarDump()
113+
{
114+
$command = new ConfigCheck(Services::logger(), Services::commands());
115+
$getVarDump = $this->getPrivateMethodInvoker($command, 'getVarDump');
116+
117+
$output = $getVarDump(new App());
118+
119+
if (
120+
ini_get('xdebug.mode')
121+
&& in_array(
122+
'develop',
123+
explode(',', ini_get('xdebug.mode')),
124+
true
125+
)
126+
) {
127+
// Xdebug overloads var_dump().
128+
$this->assertStringContainsString(
129+
'class Config\App#',
130+
$output
131+
);
132+
$this->assertStringContainsString(
133+
<<<'EOL'
134+
{
135+
public string $baseURL =>
136+
string(19) "http://example.com/"
137+
public array $allowedHostnames =>
138+
array(0) {
139+
}
140+
public string $indexPage =>
141+
string(9) "index.php"
142+
public string $uriProtocol =>
143+
string(11) "REQUEST_URI"
144+
public string $defaultLocale =>
145+
string(2) "en"
146+
public bool $negotiateLocale =>
147+
bool(false)
148+
public array $supportedLocales =>
149+
array(1) {
150+
[0] =>
151+
string(2) "en"
152+
}
153+
public string $appTimezone =>
154+
string(3) "UTC"
155+
public string $charset =>
156+
string(5) "UTF-8"
157+
public bool $forceGlobalSecureRequests =>
158+
bool(false)
159+
public array $proxyIPs =>
160+
array(0) {
161+
}
162+
public bool $CSPEnabled =>
163+
bool(false)
164+
}
165+
EOL,
166+
$output
167+
);
168+
} else {
169+
// PHP's var_dump().
170+
$this->assertStringContainsString(
171+
'object(Config\App)#',
172+
$output
173+
);
174+
$this->assertStringContainsString(
175+
<<<'EOL'
176+
{
177+
["baseURL"]=>
178+
string(19) "http://example.com/"
179+
["allowedHostnames"]=>
180+
array(0) {
181+
}
182+
["indexPage"]=>
183+
string(9) "index.php"
184+
["uriProtocol"]=>
185+
string(11) "REQUEST_URI"
186+
["defaultLocale"]=>
187+
string(2) "en"
188+
["negotiateLocale"]=>
189+
bool(false)
190+
["supportedLocales"]=>
191+
array(1) {
192+
[0]=>
193+
string(2) "en"
194+
}
195+
["appTimezone"]=>
196+
string(3) "UTC"
197+
["charset"]=>
198+
string(5) "UTF-8"
199+
["forceGlobalSecureRequests"]=>
200+
bool(false)
201+
["proxyIPs"]=>
202+
array(0) {
203+
}
204+
["CSPEnabled"]=>
205+
bool(false)
206+
}
207+
EOL,
208+
$output
209+
);
210+
}
211+
}
212+
}

0 commit comments

Comments
 (0)