diff --git a/src/foundation/src/ConfigProvider.php b/src/foundation/src/ConfigProvider.php index ad65fea06..dd728ca2e 100644 --- a/src/foundation/src/ConfigProvider.php +++ b/src/foundation/src/ConfigProvider.php @@ -9,6 +9,7 @@ use Hyperf\ExceptionHandler\Listener\ErrorExceptionHandler; use Hypervel\Console\ApplicationFactory; use Hypervel\Foundation\Console\Commands\AboutCommand; +use Hypervel\Foundation\Console\Commands\ConfigShowCommand; use Hypervel\Foundation\Console\Commands\ServerReloadCommand; use Hypervel\Foundation\Console\Commands\VendorPublishCommand; use Hypervel\Foundation\Exceptions\Contracts\ExceptionHandler as ExceptionHandlerContract; @@ -31,6 +32,7 @@ public function __invoke(): array ], 'commands' => [ AboutCommand::class, + ConfigShowCommand::class, ServerReloadCommand::class, VendorPublishCommand::class, ], diff --git a/src/foundation/src/Console/Commands/ConfigShowCommand.php b/src/foundation/src/Console/Commands/ConfigShowCommand.php new file mode 100644 index 000000000..cd38ae41e --- /dev/null +++ b/src/foundation/src/Console/Commands/ConfigShowCommand.php @@ -0,0 +1,103 @@ +argument('config'); + + if (! $this->config->has($config)) { + $this->fail("Configuration file or key {$config} does not exist."); + } + + $this->newLine(); + $this->render($config); + $this->newLine(); + + return Command::SUCCESS; + } + + /** + * Render the configuration values. + */ + public function render(string $name): void + { + $data = $this->config->get($name); + + 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. + */ + public function title(string $title, ?string $subtitle = null): void + { + $this->components->twoColumnDetail( + "{$title}", + $subtitle, + ); + } + + /** + * Format the given configuration key. + */ + protected function formatKey(string $key): string + { + return preg_replace_callback( + '/(.*)\.(.*)$/', + fn ($matches) => sprintf( + '%s ⇁ %s', + str_replace('.', ' ⇁ ', $matches[1]), + $matches[2] + ), + $key + ); + } + + /** + * Format the given configuration value. + */ + protected function formatValue(mixed $value): string + { + return match (true) { + is_bool($value) => sprintf('%s', $value ? 'true' : 'false'), + is_null($value) => 'null', + is_numeric($value) => "{$value}", + is_array($value) => '[]', + is_object($value) => get_class($value), + is_string($value) => $value, + default => print_r($value, true), + }; + } +}