Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 35 additions & 82 deletions system/Commands/Utilities/ConfigCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <classname>';

/**
* The Command's arguments
*
* @var array<string, string>
*/
protected $arguments = [
'classname' => 'The config classname to check. Short classname or FQCN.',
];

/**
* The Command's options
*
* @var array<string, string>
*/
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<BaseConfig> $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()
*/
Expand All @@ -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);
}
Expand Down
30 changes: 5 additions & 25 deletions tests/system/Commands/Utilities/ConfigCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace CodeIgniter\Commands\Utilities;

use Closure;
use CodeIgniter\CLI\CLI;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\StreamFilterTrait;
Expand Down Expand Up @@ -66,54 +65,35 @@ 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 <classname>
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',
);

command('config:check App');

$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',
);

Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions user_guide_src/source/general/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.