Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add config:show command #47858

Merged
merged 6 commits into from
Jul 28, 2023
Merged
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
125 changes: 125 additions & 0 deletions src/Illuminate/Foundation/Console/ConfigShowCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

namespace Illuminate\Foundation\Console;

use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'config:show')]
class ConfigShowCommand extends Command
{
/**
* The console command signature.
*
* @var string
*/
protected $signature = 'config:show {config : The configuration file to show}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Display all of the values for a given configuration file';

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$config = $this->argument('config');

$data = config($config);

if (! $data) {
$this->components->error("Configuration file `{$config}` does not exist.");

return Command::FAILURE;
}

$this->newLine();
$this->render($config, $data);
$this->newLine();

return Command::SUCCESS;
}

/**
* Render the configuration values.
*
* @param string $name
* @param mixed $data
* @return void
*/
public function render($name, $data)
{
if (! is_array($data)) {
$this->title($name, $this->formatValue($data));

return;
}

$this->title($name);

foreach (Arr::dot($data) as $key => $value) {
$this->components->twoColumnDetail(
$this->formatKey($key),
$this->formatValue($value)
);
}
}

/**
* Render the title.
*
* @param string $title
* @param string|null $subtitle
* @return void
*/
public function title($title, $subtitle = null)
{
$this->components->twoColumnDetail(
"<fg=green;options=bold>{$title}</>",
$subtitle,
);
}

/**
* Format the given configuration key.
*
* @param string $key
* @return string
*/
protected function formatKey($key)
{
return preg_replace_callback(
'/(.*)\.(.*)$/', fn ($matches) => sprintf(
'<fg=gray>%s ⇁</> %s',
str_replace('.', ' ⇁ ', $matches[1]),
$matches[2]
), $key
);
}

/**
* Format the given configuration value.
*
* @param mixed $value
* @return string
*/
protected function formatValue($value)
{
return match (true) {
is_bool($value) => sprintf('<fg=#ef8414;options=bold>%s</>', $value ? 'true' : 'false'),
is_null($value) => '<fg=#ef8414;options=bold>null</>',
is_numeric($value) => "<fg=#ef8414;options=bold>{$value}</>",
is_array($value) => '[]',
is_object($value) => get_class($value),
is_string($value) => $value,
default => print_r($value, true),
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Illuminate\Foundation\Console\ComponentMakeCommand;
use Illuminate\Foundation\Console\ConfigCacheCommand;
use Illuminate\Foundation\Console\ConfigClearCommand;
use Illuminate\Foundation\Console\ConfigShowCommand;
use Illuminate\Foundation\Console\ConsoleMakeCommand;
use Illuminate\Foundation\Console\DocsCommand;
use Illuminate\Foundation\Console\DownCommand;
Expand Down Expand Up @@ -111,6 +112,7 @@ class ArtisanServiceProvider extends ServiceProvider implements DeferrableProvid
'ClearResets' => ClearResetsCommand::class,
'ConfigCache' => ConfigCacheCommand::class,
'ConfigClear' => ConfigClearCommand::class,
'ConfigShow' => ConfigShowCommand::class,
'Db' => DbCommand::class,
'DbMonitor' => DatabaseMonitorCommand::class,
'DbPrune' => PruneCommand::class,
Expand Down
75 changes: 75 additions & 0 deletions tests/Testing/Console/ConfigShowCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Illuminate\Tests\Testing\Console;

use Illuminate\Foundation\Console\ConfigShowCommand;
use Orchestra\Testbench\TestCase;

class ConfigShowCommandTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

putenv('COLUMNS=64');
}

public function testDisplayConfig()
{
config()->set('test', [
'string' => 'Test',
'int' => 1,
'float' => 1.2,
'boolean' => true,
'null' => null,
'array' => [
ConfigShowCommand::class,
],
'empty_array' => [],
'assoc_array' => ['foo' => 'bar'],
'class' => new \stdClass,
]);

$this->artisan(ConfigShowCommand::class, ['config' => 'test'])
->assertSuccessful()
->expectsOutput(' test ....................................................... ')
->expectsOutput(' string ................................................ Test ')
->expectsOutput(' int ...................................................... 1 ')
->expectsOutput(' float .................................................. 1.2 ')
->expectsOutput(' boolean ............................................... true ')
->expectsOutput(' null .................................................. null ')
->expectsOutput(' array ⇁ 0 .. Illuminate\Foundation\Console\ConfigShowCommand ')
->expectsOutput(' empty_array ............................................. [] ')
->expectsOutput(' assoc_array ⇁ foo ...................................... bar ')
->expectsOutput(' class ............................................. stdClass ');
}

public function testDisplayNestedConfigItems()
{
config()->set('test', [
'nested' => [
'foo' => 'bar',
],
]);

$this->artisan(ConfigShowCommand::class, ['config' => 'test.nested'])
->assertSuccessful()
->expectsOutput(' test.nested ................................................ ')
->expectsOutput(' foo .................................................... bar ');
}

public function testDisplaySingleValue()
{
config()->set('foo', 'bar');

$this->artisan(ConfigShowCommand::class, ['config' => 'foo'])
->assertSuccessful()
->expectsOutput(' foo .................................................... bar ');
}

public function testDisplayErrorIfConfigDoesNotExist()
{
$this->artisan(ConfigShowCommand::class, ['config' => 'invalid'])
->assertFailed();
}
}