|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tempest\Framework\Commands; |
| 6 | + |
| 7 | +use function file_get_contents; |
| 8 | +use function function_exists; |
| 9 | +use function is_array; |
| 10 | +use function is_object; |
| 11 | +use function realpath; |
| 12 | +use function str_contains; |
| 13 | +use Tempest\Console\ConsoleCommand; |
| 14 | +use Tempest\Console\ExitCode; |
| 15 | +use Tempest\Console\HasConsole; |
| 16 | +use Tempest\Console\Terminal\Terminal; |
| 17 | +use Tempest\Core\Kernel\LoadConfig; |
| 18 | +use Tempest\Highlight\Languages\Json\JsonLanguage; |
| 19 | +use Tempest\Highlight\Languages\Php\PhpLanguage; |
| 20 | +use Tempest\Reflection\ClassReflector; |
| 21 | +use function var_export; |
| 22 | + |
| 23 | +final readonly class ConfigShowCommand |
| 24 | +{ |
| 25 | + use HasConsole; |
| 26 | + |
| 27 | + private const int MAX_JSON_DEPTH = 32; |
| 28 | + |
| 29 | + public function __construct( |
| 30 | + private LoadConfig $loadConfig, |
| 31 | + ) { |
| 32 | + } |
| 33 | + |
| 34 | + #[ConsoleCommand( |
| 35 | + name: 'config:show', |
| 36 | + description: 'Show resolved configuration', |
| 37 | + aliases: ['config'], |
| 38 | + )] |
| 39 | + public function __invoke( |
| 40 | + ConfigShowFormat $format = ConfigShowFormat::PRETTY, |
| 41 | + ?bool $search = false, |
| 42 | + ?string $filter = null, |
| 43 | + ): ExitCode { |
| 44 | + $configs = $this->resolveConfig($filter, $search); |
| 45 | + |
| 46 | + if (empty($configs)) { |
| 47 | + $this->console->error('No configuration found'); |
| 48 | + |
| 49 | + return ExitCode::ERROR; |
| 50 | + } |
| 51 | + |
| 52 | + match ($format) { |
| 53 | + ConfigShowFormat::DUMP => $this->dump($configs), |
| 54 | + ConfigShowFormat::PRETTY => $this->pretty($configs), |
| 55 | + ConfigShowFormat::FILE => $this->file($configs), |
| 56 | + }; |
| 57 | + |
| 58 | + return ExitCode::SUCCESS; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @return array<string, mixed> |
| 63 | + */ |
| 64 | + private function resolveConfig(?string $filter, bool $search): array |
| 65 | + { |
| 66 | + $configPaths = $this->loadConfig->find(); |
| 67 | + $configs = []; |
| 68 | + $uniqueMap = []; |
| 69 | + |
| 70 | + foreach ($configPaths as $configPath) { |
| 71 | + $config = require $configPath; |
| 72 | + $configPath = realpath($configPath); |
| 73 | + |
| 74 | + if ( |
| 75 | + $filter === null |
| 76 | + || str_contains($configPath, $filter) |
| 77 | + || str_contains($config::class, $filter) |
| 78 | + ) { |
| 79 | + $configs[$configPath] = $config; |
| 80 | + $uniqueMap[$config::class] = $configPath; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // LoadConfig::find() returns all config paths |
| 85 | + // that are overwritten by container in their order |
| 86 | + $resolvedConfigs = []; |
| 87 | + |
| 88 | + foreach ($uniqueMap as $configPath) { |
| 89 | + $resolvedConfigs[$configPath] = $configs[$configPath]; |
| 90 | + } |
| 91 | + |
| 92 | + if (! $search) { |
| 93 | + return $resolvedConfigs; |
| 94 | + } |
| 95 | + |
| 96 | + $selectedPath = $this->search($resolvedConfigs); |
| 97 | + |
| 98 | + return [$selectedPath => $resolvedConfigs[$selectedPath]]; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @param array<string, mixed> $configs |
| 103 | + */ |
| 104 | + private function search(array $configs): string |
| 105 | + { |
| 106 | + $data = array_keys($configs); |
| 107 | + sort($data); |
| 108 | + |
| 109 | + $return = $this->console->search( |
| 110 | + label: 'Which configuration file would you like to view?', |
| 111 | + search: function (string $query) use ($data): array { |
| 112 | + if ($query === '') { |
| 113 | + return $data; |
| 114 | + } |
| 115 | + |
| 116 | + return array_filter( |
| 117 | + array: $data, |
| 118 | + callback: fn (string $path) => str_contains($path, $query), |
| 119 | + ); |
| 120 | + }, |
| 121 | + default: $data[0], |
| 122 | + ); |
| 123 | + |
| 124 | + // TODO: This is a workaround for SearchComponent not clearing the terminal properly |
| 125 | + $terminal = new Terminal($this->console); |
| 126 | + $terminal->cursor->clearAfter(); |
| 127 | + |
| 128 | + return $return; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @param array<string, mixed> $configs |
| 133 | + */ |
| 134 | + private function dump(array $configs): void |
| 135 | + { |
| 136 | + if (function_exists('lw')) { |
| 137 | + lw($configs); |
| 138 | + |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + $this->console->writeln(var_export($configs, true)); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @param array<string, mixed> $configs |
| 147 | + */ |
| 148 | + private function pretty(array $configs): void |
| 149 | + { |
| 150 | + $formatted = $this->formatForJson($configs); |
| 151 | + |
| 152 | + $this->console->writeWithLanguage( |
| 153 | + json_encode( |
| 154 | + $formatted, |
| 155 | + JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE |
| 156 | + ), |
| 157 | + new JsonLanguage(), |
| 158 | + ); |
| 159 | + } |
| 160 | + |
| 161 | + private function formatForJson(mixed $value, int $depth = 0): mixed |
| 162 | + { |
| 163 | + if ($depth > self::MAX_JSON_DEPTH) { |
| 164 | + return '@...'; |
| 165 | + } |
| 166 | + |
| 167 | + if (is_object($value)) { |
| 168 | + $result = [ |
| 169 | + '@type' => $value::class, |
| 170 | + ]; |
| 171 | + |
| 172 | + $reflector = new ClassReflector($value); |
| 173 | + |
| 174 | + foreach ($reflector->getProperties() as $property) { |
| 175 | + $result[$property->getName()] = $this->formatForJson($property->getValue($value), $depth + 1); |
| 176 | + } |
| 177 | + |
| 178 | + return $result; |
| 179 | + } |
| 180 | + |
| 181 | + if (is_array($value)) { |
| 182 | + $result = []; |
| 183 | + |
| 184 | + foreach ($value as $key => $item) { |
| 185 | + $result[$key] = $this->formatForJson($item, $depth + 1); |
| 186 | + } |
| 187 | + |
| 188 | + return $result; |
| 189 | + } |
| 190 | + |
| 191 | + return $value; |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * @param array<string, mixed> $configs |
| 196 | + */ |
| 197 | + private function file(array $configs): void |
| 198 | + { |
| 199 | + $phpLanguage = new PhpLanguage(); |
| 200 | + |
| 201 | + foreach (array_keys($configs) as $path) { |
| 202 | + $this->console->writeln("<em>{$path}</em>"); |
| 203 | + $this->console->writeWithLanguage( |
| 204 | + file_get_contents($path), |
| 205 | + $phpLanguage, |
| 206 | + ); |
| 207 | + $this->console->writeln(); |
| 208 | + } |
| 209 | + } |
| 210 | +} |
0 commit comments