Skip to content

Commit

Permalink
[10.x] Add config:show command (laravel#47858)
Browse files Browse the repository at this point in the history
* Add `config:show {config}` command.

* style: fixes.

* style: fixes.

* typos.

* Move `ConfigShowCommandTest`.

* formatting

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
xiCO2k and taylorotwell authored Jul 28, 2023
1 parent a54f988 commit 4fbf481
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 0 deletions.
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();
}
}

0 comments on commit 4fbf481

Please sign in to comment.