Skip to content

Commit

Permalink
[TASK] Refactor RunDecorator to event listener
Browse files Browse the repository at this point in the history
resolves #105
  • Loading branch information
linawolf committed Dec 12, 2023
1 parent 62b6d5b commit 1dd97e5
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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']);
;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

declare(strict_types=1);
namespace T3Docs\GuidesExtension\Command;

use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\OutputInterface;

final class ApplicationEventListener
{
private const DEFAULT_OUTPUT_DIRECTORY = 'Documentation-GENERATED-temp';

private const INDEX_FILE_NAMES = [
'Index.rst' => '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 = "<info>Options:</info>\n";
$readableOutput .= print_r($input->getOptions(), true);

$readableOutput .= "<info>Arguments:</info>\n";
$readableOutput .= print_r($input->getArguments(), true);

$readableOutput .= "<info>Guessed Input:</info>\n";
$readableOutput .= print_r($guessedInput, true);

$output->writeln(sprintf("<info>DEBUG</info> Using parameters:\n%s", $readableOutput));
}

// Todo: How do we set the new input

$event->getOutput()->writeln(
sprintf(
'We are in the ApplicationEventListener',
),
);
}

/** @return array<string, string> */
private function guessInput(OutputInterface $output): array
{
$currentDirectory = getcwd();
if ($currentDirectory === false) {
if ($output->isDebug()) {
$output->writeln('<info>DEBUG</info> Could not fetch current working directory.');
}

return [];
}

$inputDirectory = $currentDirectory . '/Documentation';

if (is_dir($inputDirectory)) {
if ($output->isVerbose()) {
$output->writeln(sprintf('<info>INFO</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('<info>DEBUG</info> Using entrypoint %s', $filename));
}

return [
'input' => $inputDirectory,
'--input-format' => $extension,
];
}
}
}

if ($output->isVerbose()) {
$output->writeln('<info>INFO</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('<info>DEBUG</info> Using entrypoint %s', $filename));
}

return [
'input' => $currentDirectory,
'--input-file' => $currentDirectory . DIRECTORY_SEPARATOR . $filename,
'--input-format' => $extension,
];
}
}

return [];
}
}

0 comments on commit 1dd97e5

Please sign in to comment.