Skip to content

Commit f34cb7a

Browse files
committed
refactor: migrate config:check as a modern command
1 parent 7b7742f commit f34cb7a

3 files changed

Lines changed: 42 additions & 109 deletions

File tree

‎system/Commands/Utilities/ConfigCheck.php‎

Lines changed: 35 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -14,112 +14,69 @@
1414
namespace CodeIgniter\Commands\Utilities;
1515

1616
use CodeIgniter\Cache\FactoriesCache;
17-
use CodeIgniter\CLI\BaseCommand;
17+
use CodeIgniter\CLI\AbstractCommand;
18+
use CodeIgniter\CLI\Attributes\Command;
1819
use CodeIgniter\CLI\CLI;
20+
use CodeIgniter\CLI\Input\Argument;
1921
use CodeIgniter\Config\BaseConfig;
2022
use Config\Optimize;
2123
use Kint\Kint;
2224

2325
/**
2426
* Check the Config values.
25-
*
26-
* @see \CodeIgniter\Commands\Utilities\ConfigCheckTest
2727
*/
28-
final class ConfigCheck extends BaseCommand
28+
#[Command(name: 'config:check', description: 'Check your config values.', group: 'CodeIgniter')]
29+
class ConfigCheck extends AbstractCommand
2930
{
30-
/**
31-
* The group the command is lumped under
32-
* when listing commands.
33-
*
34-
* @var string
35-
*/
36-
protected $group = 'CodeIgniter';
37-
38-
/**
39-
* The Command's name
40-
*
41-
* @var string
42-
*/
43-
protected $name = 'config:check';
44-
45-
/**
46-
* The Command's short description
47-
*
48-
* @var string
49-
*/
50-
protected $description = 'Check your Config values.';
51-
52-
/**
53-
* The Command's usage
54-
*
55-
* @var string
56-
*/
57-
protected $usage = 'config:check <classname>';
58-
59-
/**
60-
* The Command's arguments
61-
*
62-
* @var array<string, string>
63-
*/
64-
protected $arguments = [
65-
'classname' => 'The config classname to check. Short classname or FQCN.',
66-
];
67-
68-
/**
69-
* The Command's options
70-
*
71-
* @var array<string, string>
72-
*/
73-
protected $options = [];
74-
75-
/**
76-
* @return int
77-
*/
78-
public function run(array $params)
31+
protected function configure(): void
7932
{
80-
if (! isset($params[0])) {
81-
CLI::error('You must specify a Config classname.');
82-
CLI::write(' Usage: ' . $this->usage);
83-
CLI::write('Example: config:check App');
84-
CLI::write(' config:check \'CodeIgniter\Shield\Config\Auth\'');
85-
86-
return EXIT_ERROR;
87-
}
33+
$this->addArgument(new Argument(
34+
name: 'class_name',
35+
description: 'The config class to check. Short name or FQCN.',
36+
required: true,
37+
));
38+
}
8839

40+
protected function execute(array $arguments, array $options): int
41+
{
8942
/** @var class-string<BaseConfig> $class */
90-
$class = $params[0];
43+
$class = $arguments['class_name'];
44+
45+
$configCacheEnabled = class_exists(Optimize::class) && (new Optimize())->configCacheEnabled;
9146

92-
// Load Config cache if it is enabled.
93-
$configCacheEnabled = class_exists(Optimize::class)
94-
&& (new Optimize())->configCacheEnabled;
9547
if ($configCacheEnabled) {
96-
$factoriesCache = new FactoriesCache();
97-
$factoriesCache->load('config');
48+
(new FactoriesCache())->load('config');
9849
}
9950

10051
$config = config($class);
10152

10253
if ($config === null) {
103-
CLI::error('No such Config class: ' . $class);
54+
CLI::error(sprintf('Config class "%s" not found.', $class));
10455

10556
return EXIT_ERROR;
10657
}
10758

108-
if (defined('KINT_DIR') && Kint::$enabled_mode !== false) {
109-
CLI::write($this->getKintD($config));
110-
} else {
111-
CLI::write(
112-
CLI::color($this->getVarDump($config), 'cyan'),
113-
);
114-
}
59+
CLI::write($this->getDump($config));
11560

11661
CLI::newLine();
117-
$state = CLI::color($configCacheEnabled ? 'Enabled' : 'Disabled', 'green');
118-
CLI::write('Config Caching: ' . $state);
62+
$state = CLI::color($configCacheEnabled ? 'enabled' : 'disabled', 'green');
63+
CLI::write(sprintf('Config caching: %s', $state));
11964

12065
return EXIT_SUCCESS;
12166
}
12267

68+
/**
69+
* Renders the config object using Kint when available, otherwise var_dump().
70+
*/
71+
private function getDump(object $config): string
72+
{
73+
if (defined('KINT_DIR') && Kint::$enabled_mode !== false) {
74+
return $this->getKintD($config);
75+
}
76+
77+
return CLI::color($this->getVarDump($config), 'cyan');
78+
}
79+
12380
/**
12481
* Gets object dump by Kint d()
12582
*/
@@ -129,11 +86,7 @@ private function getKintD(object $config): string
12986
d($config);
13087
$output = ob_get_clean();
13188

132-
$output = trim($output);
133-
134-
$lines = explode("\n", $output);
135-
array_splice($lines, 0, 3);
136-
array_splice($lines, -3);
89+
$lines = array_slice(explode("\n", trim($output)), 3, -3);
13790

13891
return implode("\n", $lines);
13992
}

‎tests/system/Commands/Utilities/ConfigCheckTest.php‎

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace CodeIgniter\Commands\Utilities;
1515

16-
use Closure;
1716
use CodeIgniter\CLI\CLI;
1817
use CodeIgniter\Test\CIUnitTestCase;
1918
use CodeIgniter\Test\StreamFilterTrait;
@@ -66,54 +65,35 @@ protected function tearDown(): void
6665
CLI::reset();
6766
}
6867

69-
public function testCommandConfigCheckWithNoArgumentPassed(): void
70-
{
71-
command('config:check');
72-
73-
$this->assertSame(
74-
<<<'EOF'
75-
76-
You must specify a Config classname.
77-
Usage: config:check <classname>
78-
Example: config:check App
79-
config:check 'CodeIgniter\Shield\Config\Auth'
80-
81-
EOF,
82-
$this->getStreamFilterBuffer(),
83-
);
84-
}
85-
8668
public function testCommandConfigCheckNonexistentClass(): void
8769
{
8870
command('config:check Nonexistent');
8971

9072
$this->assertSame(
91-
"\nNo such Config class: Nonexistent\n",
73+
"\nConfig class \"Nonexistent\" not found.\n",
9274
$this->getStreamFilterBuffer(),
9375
);
9476
}
9577

9678
public function testConfigCheckWithKintEnabledUsesKintD(): void
9779
{
98-
/** @var Closure(mixed...): string */
9980
$command = self::getPrivateMethodInvoker(
100-
new ConfigCheck(service('logger'), service('commands')),
81+
new ConfigCheck(service('commands')),
10182
'getKintD',
10283
);
10384

10485
command('config:check App');
10586

10687
$this->assertSame(
10788
"\n" . $command(config('App')) . "\n",
108-
preg_replace('/\s+Config Caching: \S+/', '', $this->getStreamFilterBuffer()),
89+
preg_replace('/\s+Config caching: \S+/', '', $this->getStreamFilterBuffer()),
10990
);
11091
}
11192

11293
public function testConfigCheckWithKintDisabledUsesVarDump(): void
11394
{
114-
/** @var Closure(mixed...): string */
11595
$command = self::getPrivateMethodInvoker(
116-
new ConfigCheck(service('logger'), service('commands')),
96+
new ConfigCheck(service('commands')),
11797
'getVarDump',
11898
);
11999

@@ -123,7 +103,7 @@ public function testConfigCheckWithKintDisabledUsesVarDump(): void
123103

124104
$this->assertSame(
125105
"\n" . $command(config('App')),
126-
preg_replace('/\s+Config Caching: \S+/', '', $this->getStreamFilterBuffer()),
106+
preg_replace('/\s+Config caching: \S+/', '', $this->getStreamFilterBuffer()),
127107
);
128108
} finally {
129109
Kint::$enabled_mode = true;

‎user_guide_src/source/general/configuration.rst‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,9 @@ The output is like the following:
422422
public 'CSPEnabled' -> boolean false
423423
)
424424
425-
Config Caching: Disabled
425+
Config caching: disabled
426426
427427
You can see if Config Caching is enabled or not.
428428

429-
.. note:: If Config Caching is enabled, the cached values are used permanently.
429+
.. note:: If config caching is enabled, the cached values are used permanently.
430430
See :ref:`factories-config-caching` for details.

0 commit comments

Comments
 (0)