|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Butschster\ContextGenerator\Console; |
| 6 | + |
| 7 | +use Butschster\ContextGenerator\Console\Renderer\DocumentRenderer; |
| 8 | +use Butschster\ContextGenerator\Console\Renderer\Style; |
| 9 | +use Butschster\ContextGenerator\FilesInterface; |
| 10 | +use Butschster\ContextGenerator\Loader\ConfigRegistry\ConfigParser; |
| 11 | +use Butschster\ContextGenerator\Loader\ConfigRegistry\DocumentsParserPlugin; |
| 12 | +use Butschster\ContextGenerator\Loader\JsonConfigDocumentsLoader; |
| 13 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 14 | +use Symfony\Component\Console\Command\Command; |
| 15 | +use Symfony\Component\Console\Input\InputArgument; |
| 16 | +use Symfony\Component\Console\Input\InputInterface; |
| 17 | +use Symfony\Component\Console\Output\OutputInterface; |
| 18 | + |
| 19 | +#[AsCommand( |
| 20 | + name: 'display', |
| 21 | + description: 'Display the context configuration in a human-readable format', |
| 22 | +)] |
| 23 | +final class DisplayCommand extends Command |
| 24 | +{ |
| 25 | + public function __construct( |
| 26 | + private readonly FilesInterface $files, |
| 27 | + private readonly string $defaultConfigPath = 'context.json', |
| 28 | + private readonly string $rootPath = '.', |
| 29 | + ) { |
| 30 | + parent::__construct(); |
| 31 | + } |
| 32 | + |
| 33 | + protected function configure(): void |
| 34 | + { |
| 35 | + $this |
| 36 | + ->setHelp('This command displays the context configuration in a human-readable format') |
| 37 | + ->addArgument( |
| 38 | + 'config', |
| 39 | + InputArgument::OPTIONAL, |
| 40 | + 'Path to the configuration file', |
| 41 | + $this->defaultConfigPath, |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 46 | + { |
| 47 | + $configPath = $input->getArgument('config'); |
| 48 | + |
| 49 | + if (!$this->files->exists($configPath)) { |
| 50 | + $output->writeln("<error>Configuration file not found: $configPath</error>"); |
| 51 | + return Command::FAILURE; |
| 52 | + } |
| 53 | + |
| 54 | + try { |
| 55 | + // Load the configuration using JsonConfigDocumentsLoader |
| 56 | + // todo use factory |
| 57 | + $loader = new JsonConfigDocumentsLoader( |
| 58 | + files: $this->files, |
| 59 | + parser: new ConfigParser( |
| 60 | + rootPath: $this->rootPath, |
| 61 | + documentsParser: new DocumentsParserPlugin(), |
| 62 | + ), |
| 63 | + configPath: $configPath, |
| 64 | + rootPath: $this->rootPath, |
| 65 | + ); |
| 66 | + |
| 67 | + if (!$loader->isSupported()) { |
| 68 | + $output->writeln("<error>Configuration file format not supported: $configPath</error>"); |
| 69 | + return Command::FAILURE; |
| 70 | + } |
| 71 | + |
| 72 | + // Load the document registry |
| 73 | + $registry = $loader->load(); |
| 74 | + |
| 75 | + // Create renderer and render each document |
| 76 | + $renderer = new DocumentRenderer(); |
| 77 | + |
| 78 | + $title = "Context: {$configPath}"; |
| 79 | + |
| 80 | + $output->writeln("\n" . Style::header($title)); |
| 81 | + $output->writeln(Style::separator('=', \strlen($title)) . "\n"); |
| 82 | + |
| 83 | + $documents = $registry->getDocuments(); |
| 84 | + $output->writeln(Style::property("Total documents") . ": " . Style::count(\count($documents)) . "\n\n"); |
| 85 | + |
| 86 | + foreach ($documents as $index => $document) { |
| 87 | + $output->writeln( |
| 88 | + Style::header("Document") . " " . Style::itemNumber($index + 1, \count($documents)) . ":", |
| 89 | + ); |
| 90 | + $output->writeln(Style::separator('=', \strlen($title)) . "\n"); |
| 91 | + $output->writeln(Style::indent($renderer->renderDocument($document))); |
| 92 | + $output->writeln(""); |
| 93 | + } |
| 94 | + |
| 95 | + return Command::SUCCESS; |
| 96 | + } catch (\Exception $e) { |
| 97 | + $output->writeln("<error>Error displaying configuration: {$e->getMessage()}</error>"); |
| 98 | + |
| 99 | + if ($output->isVerbose()) { |
| 100 | + $output->writeln("<error>{$e->getTraceAsString()}</error>"); |
| 101 | + } |
| 102 | + |
| 103 | + return Command::FAILURE; |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments