diff --git a/packages/typo3-guides-extension/resources/config/typo3-guides.php b/packages/typo3-guides-extension/resources/config/typo3-guides.php index 37ae186a8..5665de445 100644 --- a/packages/typo3-guides-extension/resources/config/typo3-guides.php +++ b/packages/typo3-guides-extension/resources/config/typo3-guides.php @@ -3,7 +3,9 @@ declare(strict_types=1); use phpDocumentor\Guides\Cli\Command\Run; +use Symfony\Component\Console\ConsoleEvents; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; +use T3Docs\GuidesExtension\Command\ApplicationEventListener; use T3Docs\GuidesExtension\Command\RunDecorator; use function Symfony\Component\DependencyInjection\Loader\Configurator\service; @@ -30,5 +32,7 @@ ->set(\phpDocumentor\Guides\NodeRenderers\DelegatingNodeRenderer::class) ->call('setNodeRendererFactory', [service('phpdoc.guides.noderenderer.factory.html')]) ->tag('phpdoc.guides.noderenderer.singlepage') + ->set(ApplicationEventListener::class) + ->tag('event_listener', ['event' => ConsoleEvents::COMMAND, 'method' => '__invoke']); ; }; diff --git a/packages/typo3-guides-extension/src/Command/ApplicationEventListener.php b/packages/typo3-guides-extension/src/Command/ApplicationEventListener.php new file mode 100644 index 000000000..bfb2e73c7 --- /dev/null +++ b/packages/typo3-guides-extension/src/Command/ApplicationEventListener.php @@ -0,0 +1,131 @@ + 'rst', + 'index.rst' => 'rst', + 'Index.md' => 'md', + 'index.md' => 'md', + ]; + private const FALLBACK_FILE_NAMES = [ + 'README.rst' => 'rst', + 'README.md' => 'md', + ]; + public function __invoke(ConsoleCommandEvent $event): void + { + $input = $event->getInput(); + $output = $event->getOutput(); + $options = []; + foreach ($input->getOptions() as $option => $value) { + if ($value === null) { + continue; + } + + $options['--' . $option] = $value; + } + $arguments = $input->getArguments(); + if ($arguments['input'] === null) { + $guessedInput = $this->guessInput($output); + } else { + $guessedInput = []; + } + + if (!isset($options['--output'])) { + $options['--output'] = getcwd() . '/' . self::DEFAULT_OUTPUT_DIRECTORY; + } + + + $input = new ArrayInput( + [ + ...$arguments, + ...$options, + ...$guessedInput, + ] + ); + + if ($output->isDebug()) { + $readableOutput = "Options:\n"; + $readableOutput .= print_r($input->getOptions(), true); + + $readableOutput .= "Arguments:\n"; + $readableOutput .= print_r($input->getArguments(), true); + + $readableOutput .= "Guessed Input:\n"; + $readableOutput .= print_r($guessedInput, true); + + $output->writeln(sprintf("DEBUG Using parameters:\n%s", $readableOutput)); + } + + // Todo: How do we set the new input + + $event->getOutput()->writeln( + sprintf( + 'We are in the ApplicationEventListener', + ), + ); + } + + /** @return array */ + private function guessInput(OutputInterface $output): array + { + $currentDirectory = getcwd(); + if ($currentDirectory === false) { + if ($output->isDebug()) { + $output->writeln('DEBUG Could not fetch current working directory.'); + } + + return []; + } + + $inputDirectory = $currentDirectory . '/Documentation'; + + if (is_dir($inputDirectory)) { + if ($output->isVerbose()) { + $output->writeln(sprintf('INFO Input directory not specified, using %s', $inputDirectory)); + } + + foreach (self::INDEX_FILE_NAMES as $filename => $extension) { + if (file_exists($inputDirectory . DIRECTORY_SEPARATOR . $filename)) { + if ($output->isDebug()) { + $output->writeln(sprintf('DEBUG Using entrypoint %s', $filename)); + } + + return [ + 'input' => $inputDirectory, + '--input-format' => $extension, + ]; + } + } + } + + if ($output->isVerbose()) { + $output->writeln('INFO Index documentation file not found, trying README.rst or README.md'); + } + + foreach (self::FALLBACK_FILE_NAMES as $filename => $extension) { + if (file_exists($currentDirectory . DIRECTORY_SEPARATOR . $filename)) { + if ($output->isVerbose()) { + $output->writeln(sprintf('DEBUG Using entrypoint %s', $filename)); + } + + return [ + 'input' => $currentDirectory, + '--input-file' => $currentDirectory . DIRECTORY_SEPARATOR . $filename, + '--input-format' => $extension, + ]; + } + } + + return []; + } +}