Skip to content

Commit 0be5335

Browse files
committed
feat: implement config:show command
1 parent 47a3382 commit 0be5335

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

src/foundation/src/ConfigProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Hyperf\ExceptionHandler\Listener\ErrorExceptionHandler;
1010
use Hypervel\Console\ApplicationFactory;
1111
use Hypervel\Foundation\Console\Commands\AboutCommand;
12+
use Hypervel\Foundation\Console\Commands\ConfigShowCommand;
1213
use Hypervel\Foundation\Console\Commands\ServerReloadCommand;
1314
use Hypervel\Foundation\Console\Commands\VendorPublishCommand;
1415
use Hypervel\Foundation\Exceptions\Contracts\ExceptionHandler as ExceptionHandlerContract;
@@ -31,6 +32,7 @@ public function __invoke(): array
3132
],
3233
'commands' => [
3334
AboutCommand::class,
35+
ConfigShowCommand::class,
3436
ServerReloadCommand::class,
3537
VendorPublishCommand::class,
3638
],
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypervel\Foundation\Console\Commands;
6+
7+
use Hyperf\Contract\ConfigInterface;
8+
use Hypervel\Console\Command;
9+
use Hypervel\Support\Arr;
10+
11+
class ConfigShowCommand extends Command
12+
{
13+
protected ?string $signature = 'config:show {config : The configuration file or key to show}';
14+
15+
protected string $description = 'Display all of the values for a given configuration file or key';
16+
17+
public function __construct(
18+
protected ConfigInterface $config
19+
) {
20+
parent::__construct();
21+
}
22+
23+
public function handle()
24+
{
25+
$config = $this->argument('config');
26+
27+
if (! $this->config->has($config)) {
28+
$this->fail("Configuration file or key <comment>{$config}</comment> does not exist.");
29+
}
30+
31+
$this->newLine();
32+
$this->render($config);
33+
$this->newLine();
34+
35+
return Command::SUCCESS;
36+
}
37+
38+
/**
39+
* Render the configuration values.
40+
*/
41+
public function render(string $name): void
42+
{
43+
$data = $this->config->get($name);
44+
45+
if (! is_array($data)) {
46+
$this->title($name, $this->formatValue($data));
47+
48+
return;
49+
}
50+
51+
$this->title($name);
52+
53+
foreach (Arr::dot($data) as $key => $value) {
54+
$this->components->twoColumnDetail(
55+
$this->formatKey($key),
56+
$this->formatValue($value)
57+
);
58+
}
59+
}
60+
61+
/**
62+
* Render the title.
63+
*/
64+
public function title(string $title, ?string $subtitle = null): void
65+
{
66+
$this->components->twoColumnDetail(
67+
"<fg=green;options=bold>{$title}</>",
68+
$subtitle,
69+
);
70+
}
71+
72+
/**
73+
* Format the given configuration key.
74+
*/
75+
protected function formatKey(string $key): string
76+
{
77+
return preg_replace_callback(
78+
'/(.*)\.(.*)$/',
79+
fn ($matches) => sprintf(
80+
'<fg=gray>%s ⇁</> %s',
81+
str_replace('.', '', $matches[1]),
82+
$matches[2]
83+
),
84+
$key
85+
);
86+
}
87+
88+
/**
89+
* Format the given configuration value.
90+
*/
91+
protected function formatValue(mixed $value): string
92+
{
93+
return match (true) {
94+
is_bool($value) => sprintf('<fg=#ef8414;options=bold>%s</>', $value ? 'true' : 'false'),
95+
is_null($value) => '<fg=#ef8414;options=bold>null</>',
96+
is_numeric($value) => "<fg=#ef8414;options=bold>{$value}</>",
97+
is_array($value) => '[]',
98+
is_object($value) => get_class($value),
99+
is_string($value) => $value,
100+
default => print_r($value, true),
101+
};
102+
}
103+
}

0 commit comments

Comments
 (0)