Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TASK] Refactor RunDecorator to event listener #171

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 [];
}
}
Loading