Skip to content

Commit 26beec9

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

3 files changed

Lines changed: 44 additions & 92 deletions

File tree

system/Commands/Utilities/ConfigCheck.php

Lines changed: 27 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -14,108 +14,64 @@
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\'');
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+
}
8539

86-
return EXIT_ERROR;
40+
protected function interact(array &$arguments, array &$options): void
41+
{
42+
if ($arguments === []) {
43+
$arguments[] = CLI::prompt('Config class name', null, 'required');
8744
}
45+
}
8846

47+
protected function execute(array $arguments, array $options): int
48+
{
8949
/** @var class-string<BaseConfig> $class */
90-
$class = $params[0];
50+
$class = $arguments['class_name'];
51+
52+
$configCacheEnabled = class_exists(Optimize::class) && (new Optimize())->configCacheEnabled;
9153

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

10058
$config = config($class);
10159

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

10563
return EXIT_ERROR;
10664
}
10765

10866
if (defined('KINT_DIR') && Kint::$enabled_mode !== false) {
10967
CLI::write($this->getKintD($config));
11068
} else {
111-
CLI::write(
112-
CLI::color($this->getVarDump($config), 'cyan'),
113-
);
69+
CLI::write(CLI::color($this->getVarDump($config), 'cyan'));
11470
}
11571

11672
CLI::newLine();
117-
$state = CLI::color($configCacheEnabled ? 'Enabled' : 'Disabled', 'green');
118-
CLI::write('Config Caching: ' . $state);
73+
$state = CLI::color($configCacheEnabled ? 'enabled' : 'disabled', 'green');
74+
CLI::write(sprintf('Config caching: %s', $state));
11975

12076
return EXIT_SUCCESS;
12177
}

tests/system/Commands/Utilities/ConfigCheckTest.php

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
namespace CodeIgniter\Commands\Utilities;
1515

16-
use Closure;
1716
use CodeIgniter\CLI\CLI;
1817
use CodeIgniter\Test\CIUnitTestCase;
18+
use CodeIgniter\Test\Mock\MockInputOutput;
1919
use CodeIgniter\Test\StreamFilterTrait;
2020
use Config\App;
2121
use Kint\Kint;
@@ -66,54 +66,50 @@ protected function tearDown(): void
6666
CLI::reset();
6767
}
6868

69-
public function testCommandConfigCheckWithNoArgumentPassed(): void
69+
public function testCommandConfigCheckPromptsForClassNameWhenMissing(): void
7070
{
71-
command('config:check');
71+
$io = new MockInputOutput();
72+
$io->setInputs(['App']);
73+
CLI::setInputOutput($io);
7274

73-
$this->assertSame(
74-
<<<'EOF'
75+
command('config:check');
7576

76-
You must specify a Config classname.
77-
Usage: config:check <classname>
78-
Example: config:check App
79-
config:check 'CodeIgniter\Shield\Config\Auth'
77+
CLI::resetInputOutput();
8078

81-
EOF,
82-
$this->getStreamFilterBuffer(),
83-
);
79+
$output = $io->getOutput();
80+
$this->assertStringContainsString('Config class name', $output);
81+
$this->assertStringContainsString('Config caching:', $output);
8482
}
8583

8684
public function testCommandConfigCheckNonexistentClass(): void
8785
{
8886
command('config:check Nonexistent');
8987

9088
$this->assertSame(
91-
"\nNo such Config class: Nonexistent\n",
89+
"\nConfig class \"Nonexistent\" not found.\n",
9290
$this->getStreamFilterBuffer(),
9391
);
9492
}
9593

9694
public function testConfigCheckWithKintEnabledUsesKintD(): void
9795
{
98-
/** @var Closure(mixed...): string */
9996
$command = self::getPrivateMethodInvoker(
100-
new ConfigCheck(service('logger'), service('commands')),
97+
new ConfigCheck(service('commands')),
10198
'getKintD',
10299
);
103100

104101
command('config:check App');
105102

106103
$this->assertSame(
107104
"\n" . $command(config('App')) . "\n",
108-
preg_replace('/\s+Config Caching: \S+/', '', $this->getStreamFilterBuffer()),
105+
preg_replace('/\s+Config caching: \S+/', '', $this->getStreamFilterBuffer()),
109106
);
110107
}
111108

112109
public function testConfigCheckWithKintDisabledUsesVarDump(): void
113110
{
114-
/** @var Closure(mixed...): string */
115111
$command = self::getPrivateMethodInvoker(
116-
new ConfigCheck(service('logger'), service('commands')),
112+
new ConfigCheck(service('commands')),
117113
'getVarDump',
118114
);
119115

@@ -123,7 +119,7 @@ public function testConfigCheckWithKintDisabledUsesVarDump(): void
123119

124120
$this->assertSame(
125121
"\n" . $command(config('App')),
126-
preg_replace('/\s+Config Caching: \S+/', '', $this->getStreamFilterBuffer()),
122+
preg_replace('/\s+Config caching: \S+/', '', $this->getStreamFilterBuffer()),
127123
);
128124
} finally {
129125
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)