diff --git a/system/Commands/Utilities/ConfigCheck.php b/system/Commands/Utilities/ConfigCheck.php new file mode 100644 index 000000000000..0fe8ac0b0a34 --- /dev/null +++ b/system/Commands/Utilities/ConfigCheck.php @@ -0,0 +1,125 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter\Commands\Utilities; + +use CodeIgniter\CLI\BaseCommand; +use CodeIgniter\CLI\CLI; +use CodeIgniter\Config\BaseConfig; +use Kint\Kint; + +/** + * Check the Config values. + * + * @see \CodeIgniter\Commands\Utilities\ConfigCheckTest + */ +final class ConfigCheck extends BaseCommand +{ + /** + * 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 = []; + + /** + * {@inheritDoc} + */ + public function run(array $params) + { + 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; + } + + /** @var class-string $class */ + $class = $params[0]; + + $config = config($class); + + if ($config === null) { + CLI::error('No such Config class: ' . $class); + + return EXIT_ERROR; + } + + if (defined('KINT_DIR') && Kint::$enabled_mode !== false) { + ob_start(); + d($config); + $output = ob_get_clean(); + + $output = trim($output); + $output = preg_replace('/\x1b\[36m.*┘\x1b\[0m/su', '', $output); + $output = preg_replace('/\x1b\[36m.*Called from .*\x1b\[0m/su', '', $output); + $output = trim($output); + + CLI::write($output); + } else { + ob_start(); + var_dump($config); + $output = ob_get_clean(); + + $output = preg_replace( + '!.*system/Commands/Utilities/Config.php.*\n!u', + '', + $output + ); + + CLI::write( + CLI::color($output, 'cyan') + ); + } + + return EXIT_SUCCESS; + } +} diff --git a/tests/system/Commands/Utilities/ConfigCheckTest.php b/tests/system/Commands/Utilities/ConfigCheckTest.php new file mode 100644 index 000000000000..5496a47ef78b --- /dev/null +++ b/tests/system/Commands/Utilities/ConfigCheckTest.php @@ -0,0 +1,61 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter\Commands\Utilities; + +use CodeIgniter\Test\CIUnitTestCase; +use CodeIgniter\Test\StreamFilterTrait; +use Config\App; + +/** + * @internal + * + * @group Others + */ +final class ConfigCheckTest extends CIUnitTestCase +{ + use StreamFilterTrait; + + protected function setUp(): void + { + $this->resetServices(); + parent::setUp(); + } + + protected function tearDown(): void + { + $this->resetServices(); + parent::tearDown(); + } + + protected function getBuffer() + { + return $this->getStreamFilterBuffer(); + } + + public function testConfigApp(): void + { + command('config:check App'); + + $this->assertStringContainsString(App::class, $this->getBuffer()); + $this->assertStringContainsString("public 'baseURL", $this->getBuffer()); + } + + public function testConfigNonexistentClass(): void + { + command('config:check NotExist'); + + $this->assertStringContainsString( + 'No such Config class: NotExist', + $this->getBuffer() + ); + } +}