Skip to content

Commit

Permalink
feat: add config:check command to check Config vaules
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Oct 28, 2023
1 parent b3db566 commit b6f96c5
Show file tree
Hide file tree
Showing 2 changed files with 251 additions and 0 deletions.
132 changes: 132 additions & 0 deletions system/Commands/Utilities/ConfigCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* 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 <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 = [];

/**
* {@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<BaseConfig> $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) {
CLI::write($this->getKintD($config));
} else {
CLI::write(
CLI::color($this->getVarDump($config), 'cyan')
);
}

return EXIT_SUCCESS;
}

private function getKintD($config): string
{
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);

return trim($output);
}

private function getVarDump($config): string
{
ob_start();
var_dump($config);
$output = ob_get_clean();

return preg_replace(
'!.*system/Commands/Utilities/ConfigCheck.php.*\n!u',
'',
$output
);
}
}
119 changes: 119 additions & 0 deletions tests/system/Commands/Utilities/ConfigCheckTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* 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;
use Config\Services;

/**
* @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 testCommandConfigCheckNoArg(): void
{
command('config:check');

$this->assertStringContainsString(
'You must specify a Config classname.',
$this->getBuffer()
);
}

public function testCommandConfigCheckApp(): void
{
command('config:check App');

$this->assertStringContainsString(App::class, $this->getBuffer());
$this->assertStringContainsString("public 'baseURL", $this->getBuffer());
}

public function testCommandConfigCheckNonexistentClass(): void
{
command('config:check Nonexistent');

$this->assertStringContainsString(
'No such Config class: Nonexistent',
$this->getBuffer()
);
}

public function testGetVarDump()
{
$command = new ConfigCheck(Services::logger(), Services::commands());
$getVarDump = $this->getPrivateMethodInvoker($command, 'getVarDump');

$output = $getVarDump(new App());

$this->assertStringContainsString(
'class Config\App',
$output
);
$this->assertStringContainsString(
'{
public string $baseURL =>
string(19) "http://example.com/"
public array $allowedHostnames =>
array(0) {
}
public string $indexPage =>
string(9) "index.php"
public string $uriProtocol =>
string(11) "REQUEST_URI"
public string $defaultLocale =>
string(2) "en"
public bool $negotiateLocale =>
bool(false)
public array $supportedLocales =>
array(1) {
[0] =>
string(2) "en"
}
public string $appTimezone =>
string(3) "UTC"
public string $charset =>
string(5) "UTF-8"
public bool $forceGlobalSecureRequests =>
bool(false)
public array $proxyIPs =>
array(0) {
}
public bool $CSPEnabled =>
bool(false)
}',
$output
);
}
}

0 comments on commit b6f96c5

Please sign in to comment.