Skip to content

Commit e875f30

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

File tree

2 files changed

+186
-0
lines changed

2 files changed

+186
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
ob_start();
98+
d($config);
99+
$output = ob_get_clean();
100+
101+
$output = trim($output);
102+
$output = preg_replace('/\x1b\[36m.*┘\x1b\[0m/su', '', $output);
103+
$output = preg_replace('/\x1b\[36m.*Called from .*\x1b\[0m/su', '', $output);
104+
$output = trim($output);
105+
106+
CLI::write($output);
107+
} else {
108+
ob_start();
109+
var_dump($config);
110+
$output = ob_get_clean();
111+
112+
$output = preg_replace(
113+
'!.*system/Commands/Utilities/ConfigCheck.php.*\n!u',
114+
'',
115+
$output
116+
);
117+
118+
CLI::write(
119+
CLI::color($output, 'cyan')
120+
);
121+
}
122+
123+
return EXIT_SUCCESS;
124+
}
125+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
18+
/**
19+
* @internal
20+
*
21+
* @group Others
22+
*/
23+
final class ConfigCheckTest extends CIUnitTestCase
24+
{
25+
use StreamFilterTrait;
26+
27+
protected function setUp(): void
28+
{
29+
$this->resetServices();
30+
parent::setUp();
31+
}
32+
33+
protected function tearDown(): void
34+
{
35+
$this->resetServices();
36+
parent::tearDown();
37+
}
38+
39+
protected function getBuffer()
40+
{
41+
return $this->getStreamFilterBuffer();
42+
}
43+
44+
public function testConfigApp(): void
45+
{
46+
command('config:check App');
47+
48+
$this->assertStringContainsString(App::class, $this->getBuffer());
49+
$this->assertStringContainsString("public 'baseURL", $this->getBuffer());
50+
}
51+
52+
public function testConfigNonexistentClass(): void
53+
{
54+
command('config:check NotExist');
55+
56+
$this->assertStringContainsString(
57+
'No such Config class: NotExist',
58+
$this->getBuffer()
59+
);
60+
}
61+
}

0 commit comments

Comments
 (0)