Skip to content

Commit 698a1f7

Browse files
committed
feat: add config:check command to check Config vaules
1 parent 3589426 commit 698a1f7

File tree

2 files changed

+185
-0
lines changed

2 files changed

+185
-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
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/Config.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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
17+
/**
18+
* @internal
19+
*
20+
* @group Others
21+
*/
22+
final class ConfigCheckTest extends CIUnitTestCase
23+
{
24+
use StreamFilterTrait;
25+
26+
protected function setUp(): void
27+
{
28+
$this->resetServices();
29+
parent::setUp();
30+
}
31+
32+
protected function tearDown(): void
33+
{
34+
$this->resetServices();
35+
parent::tearDown();
36+
}
37+
38+
protected function getBuffer()
39+
{
40+
return $this->getStreamFilterBuffer();
41+
}
42+
43+
public function testConfigApp(): void
44+
{
45+
command('config App');
46+
47+
$this->assertStringContainsString('Config\App', $this->getBuffer());
48+
$this->assertStringContainsString("public 'baseURL", $this->getBuffer());
49+
}
50+
51+
public function testConfigNonexistentClass(): void
52+
{
53+
command('config NotExist');
54+
55+
$this->assertStringContainsString(
56+
'No such Config class: NotExist',
57+
$this->getBuffer()
58+
);
59+
}
60+
}

0 commit comments

Comments
 (0)