-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[10.x] Add
config:show
command (#47858)
* Add `config:show {config}` command. * style: fixes. * style: fixes. * typos. * Move `ConfigShowCommandTest`. * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
- Loading branch information
1 parent
a54f988
commit 4fbf481
Showing
3 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
src/Illuminate/Foundation/Console/ConfigShowCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |