diff --git a/system/Commands/Utilities/ConfigCheck.php b/system/Commands/Utilities/ConfigCheck.php index 4583c67dae87..9010ac0504c5 100644 --- a/system/Commands/Utilities/ConfigCheck.php +++ b/system/Commands/Utilities/ConfigCheck.php @@ -14,112 +14,69 @@ namespace CodeIgniter\Commands\Utilities; use CodeIgniter\Cache\FactoriesCache; -use CodeIgniter\CLI\BaseCommand; +use CodeIgniter\CLI\AbstractCommand; +use CodeIgniter\CLI\Attributes\Command; use CodeIgniter\CLI\CLI; +use CodeIgniter\CLI\Input\Argument; use CodeIgniter\Config\BaseConfig; use Config\Optimize; use Kint\Kint; /** * Check the Config values. - * - * @see \CodeIgniter\Commands\Utilities\ConfigCheckTest */ -final class ConfigCheck extends BaseCommand +#[Command(name: 'config:check', description: 'Check your config values.', group: 'CodeIgniter')] +class ConfigCheck extends AbstractCommand { - /** - * The group the command is lumped under - * when listing commands. - * - * @var string - */ - protected $group = 'CodeIgniter'; - - /** - * The Command's name - * - * @var string - */ - protected $name = 'config:check'; - - /** - * The Command's short description - * - * @var string - */ - protected $description = 'Check your Config values.'; - - /** - * The Command's usage - * - * @var string - */ - protected $usage = 'config:check '; - - /** - * The Command's arguments - * - * @var array - */ - protected $arguments = [ - 'classname' => 'The config classname to check. Short classname or FQCN.', - ]; - - /** - * The Command's options - * - * @var array - */ - protected $options = []; - - /** - * @return int - */ - public function run(array $params) + protected function configure(): void { - if (! isset($params[0])) { - CLI::error('You must specify a Config classname.'); - CLI::write(' Usage: ' . $this->usage); - CLI::write('Example: config:check App'); - CLI::write(' config:check \'CodeIgniter\Shield\Config\Auth\''); - - return EXIT_ERROR; - } + $this->addArgument(new Argument( + name: 'class_name', + description: 'The config class to check. Short name or FQCN.', + required: true, + )); + } + protected function execute(array $arguments, array $options): int + { /** @var class-string $class */ - $class = $params[0]; + $class = $arguments['class_name']; + + $configCacheEnabled = class_exists(Optimize::class) && (new Optimize())->configCacheEnabled; - // Load Config cache if it is enabled. - $configCacheEnabled = class_exists(Optimize::class) - && (new Optimize())->configCacheEnabled; if ($configCacheEnabled) { - $factoriesCache = new FactoriesCache(); - $factoriesCache->load('config'); + (new FactoriesCache())->load('config'); } $config = config($class); if ($config === null) { - CLI::error('No such Config class: ' . $class); + CLI::error(sprintf('Config class "%s" not found.', $class)); return EXIT_ERROR; } - if (defined('KINT_DIR') && Kint::$enabled_mode !== false) { - CLI::write($this->getKintD($config)); - } else { - CLI::write( - CLI::color($this->getVarDump($config), 'cyan'), - ); - } + CLI::write($this->getDump($config)); CLI::newLine(); - $state = CLI::color($configCacheEnabled ? 'Enabled' : 'Disabled', 'green'); - CLI::write('Config Caching: ' . $state); + $state = CLI::color($configCacheEnabled ? 'enabled' : 'disabled', 'green'); + CLI::write(sprintf('Config caching: %s', $state)); return EXIT_SUCCESS; } + /** + * Renders the config object using Kint when available, otherwise var_dump(). + */ + private function getDump(object $config): string + { + if (defined('KINT_DIR') && Kint::$enabled_mode !== false) { + return $this->getKintD($config); + } + + return CLI::color($this->getVarDump($config), 'cyan'); + } + /** * Gets object dump by Kint d() */ @@ -129,11 +86,7 @@ private function getKintD(object $config): string d($config); $output = ob_get_clean(); - $output = trim($output); - - $lines = explode("\n", $output); - array_splice($lines, 0, 3); - array_splice($lines, -3); + $lines = array_slice(explode("\n", trim($output)), 3, -3); return implode("\n", $lines); } diff --git a/tests/system/Commands/Utilities/ConfigCheckTest.php b/tests/system/Commands/Utilities/ConfigCheckTest.php index d6aeba15e664..81a27cb69021 100644 --- a/tests/system/Commands/Utilities/ConfigCheckTest.php +++ b/tests/system/Commands/Utilities/ConfigCheckTest.php @@ -13,7 +13,6 @@ namespace CodeIgniter\Commands\Utilities; -use Closure; use CodeIgniter\CLI\CLI; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\StreamFilterTrait; @@ -66,38 +65,20 @@ protected function tearDown(): void CLI::reset(); } - public function testCommandConfigCheckWithNoArgumentPassed(): void - { - command('config:check'); - - $this->assertSame( - <<<'EOF' - - You must specify a Config classname. - Usage: config:check - Example: config:check App - config:check 'CodeIgniter\Shield\Config\Auth' - - EOF, - $this->getStreamFilterBuffer(), - ); - } - public function testCommandConfigCheckNonexistentClass(): void { command('config:check Nonexistent'); $this->assertSame( - "\nNo such Config class: Nonexistent\n", + "\nConfig class \"Nonexistent\" not found.\n", $this->getStreamFilterBuffer(), ); } public function testConfigCheckWithKintEnabledUsesKintD(): void { - /** @var Closure(mixed...): string */ $command = self::getPrivateMethodInvoker( - new ConfigCheck(service('logger'), service('commands')), + new ConfigCheck(service('commands')), 'getKintD', ); @@ -105,15 +86,14 @@ public function testConfigCheckWithKintEnabledUsesKintD(): void $this->assertSame( "\n" . $command(config('App')) . "\n", - preg_replace('/\s+Config Caching: \S+/', '', $this->getStreamFilterBuffer()), + preg_replace('/\s+Config caching: \S+/', '', $this->getStreamFilterBuffer()), ); } public function testConfigCheckWithKintDisabledUsesVarDump(): void { - /** @var Closure(mixed...): string */ $command = self::getPrivateMethodInvoker( - new ConfigCheck(service('logger'), service('commands')), + new ConfigCheck(service('commands')), 'getVarDump', ); @@ -123,7 +103,7 @@ public function testConfigCheckWithKintDisabledUsesVarDump(): void $this->assertSame( "\n" . $command(config('App')), - preg_replace('/\s+Config Caching: \S+/', '', $this->getStreamFilterBuffer()), + preg_replace('/\s+Config caching: \S+/', '', $this->getStreamFilterBuffer()), ); } finally { Kint::$enabled_mode = true; diff --git a/user_guide_src/source/general/configuration.rst b/user_guide_src/source/general/configuration.rst index 601ab6e81177..a2f33ae3852b 100644 --- a/user_guide_src/source/general/configuration.rst +++ b/user_guide_src/source/general/configuration.rst @@ -422,9 +422,9 @@ The output is like the following: public 'CSPEnabled' -> boolean false ) - Config Caching: Disabled + Config caching: disabled You can see if Config Caching is enabled or not. -.. note:: If Config Caching is enabled, the cached values are used permanently. +.. note:: If config caching is enabled, the cached values are used permanently. See :ref:`factories-config-caching` for details.