Skip to content

Commit 934e643

Browse files
committed
feat: add config:check command to check Config vaules
1 parent 76edbf4 commit 934e643

File tree

2 files changed

+175
-0
lines changed

2 files changed

+175
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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 Kint\Kint;
17+
18+
/**
19+
* Check the Config values.
20+
*/
21+
final class ConfigCheck extends BaseCommand
22+
{
23+
/**
24+
* The group the command is lumped under
25+
* when listing commands.
26+
*
27+
* @var string
28+
*/
29+
protected $group = 'CodeIgniter';
30+
31+
/**
32+
* The Command's name
33+
*
34+
* @var string
35+
*/
36+
protected $name = 'config:check';
37+
38+
/**
39+
* The Command's short description
40+
*
41+
* @var string
42+
*/
43+
protected $description = 'Check your Config values.';
44+
45+
/**
46+
* The Command's usage
47+
*
48+
* @var string
49+
*/
50+
protected $usage = 'config:check <classname>';
51+
52+
/**
53+
* The Command's arguments
54+
*
55+
* @var array<string, string>
56+
*/
57+
protected $arguments = [];
58+
59+
/**
60+
* The Command's options
61+
*
62+
* @var array
63+
*/
64+
protected $options = [];
65+
66+
/**
67+
* {@inheritDoc}
68+
*/
69+
public function run(array $params)
70+
{
71+
if (! isset($params[0])) {
72+
CLI::error('You must specify a Config classname.');
73+
CLI::write(' Usage: ' . $this->usage);
74+
CLI::write('Example: config:check App');
75+
CLI::write(' config:check \'CodeIgniter\Shield\Config\Auth\'');
76+
77+
return EXIT_ERROR;
78+
}
79+
80+
$class = $params[0];
81+
82+
$config = config($class);
83+
84+
if ($config === null) {
85+
CLI::error('No such Config class: ' . $class);
86+
87+
return EXIT_ERROR;
88+
}
89+
90+
if (defined('KINT_DIR') && Kint::$enabled_mode) {
91+
ob_start();
92+
d($config);
93+
$output = ob_get_clean();
94+
95+
$output = trim($output);
96+
$output = preg_replace('/\x1b\[36m.*┘\x1b\[0m/su', '', $output);
97+
$output = preg_replace('/\x1b\[36m.*Called from .*\x1b\[0m/su', '', $output);
98+
$output = trim($output);
99+
100+
CLI::write($output);
101+
} else {
102+
ob_start();
103+
var_dump($config);
104+
$output = ob_get_clean();
105+
106+
$output = preg_replace('!.*system/Commands/Utilities/Config.php.*\n!u', '', $output);
107+
108+
CLI::write(
109+
CLI::color($output, 'cyan')
110+
);
111+
}
112+
113+
return EXIT_SUCCESS;
114+
}
115+
}
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)