Skip to content

Commit 9678ea1

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

File tree

2 files changed

+351
-0
lines changed

2 files changed

+351
-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: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
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)/',
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 (function_exists('xdebug_get_headers')) {
120+
// Xdebug is enabled.
121+
$this->assertStringContainsString(
122+
'class Config\App#',
123+
$output
124+
);
125+
$this->assertStringContainsString(
126+
<<<'EOL'
127+
{
128+
public string $baseURL =>
129+
string(19) "http://example.com/"
130+
public array $allowedHostnames =>
131+
array(0) {
132+
}
133+
public string $indexPage =>
134+
string(9) "index.php"
135+
public string $uriProtocol =>
136+
string(11) "REQUEST_URI"
137+
public string $defaultLocale =>
138+
string(2) "en"
139+
public bool $negotiateLocale =>
140+
bool(false)
141+
public array $supportedLocales =>
142+
array(1) {
143+
[0] =>
144+
string(2) "en"
145+
}
146+
public string $appTimezone =>
147+
string(3) "UTC"
148+
public string $charset =>
149+
string(5) "UTF-8"
150+
public bool $forceGlobalSecureRequests =>
151+
bool(false)
152+
public array $proxyIPs =>
153+
array(0) {
154+
}
155+
public bool $CSPEnabled =>
156+
bool(false)
157+
}
158+
EOL,
159+
$output
160+
);
161+
} else {
162+
// Xdebug is disabled.
163+
$this->assertStringContainsString(
164+
'object(Config\App)#',
165+
$output
166+
);
167+
$this->assertStringContainsString(
168+
<<<'EOL'
169+
{
170+
["baseURL"]=>
171+
string(19) "http://example.com/"
172+
["allowedHostnames"]=>
173+
array(0) {
174+
}
175+
["indexPage"]=>
176+
string(9) "index.php"
177+
["uriProtocol"]=>
178+
string(11) "REQUEST_URI"
179+
["defaultLocale"]=>
180+
string(2) "en"
181+
["negotiateLocale"]=>
182+
bool(false)
183+
["supportedLocales"]=>
184+
array(1) {
185+
[0]=>
186+
string(2) "en"
187+
}
188+
["appTimezone"]=>
189+
string(3) "UTC"
190+
["charset"]=>
191+
string(5) "UTF-8"
192+
["forceGlobalSecureRequests"]=>
193+
bool(false)
194+
["proxyIPs"]=>
195+
array(0) {
196+
}
197+
["CSPEnabled"]=>
198+
bool(false)
199+
}
200+
EOL,
201+
$output
202+
);
203+
}
204+
}
205+
}

0 commit comments

Comments
 (0)