|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright 2021 Navarr Barnier. All Rights Reserved. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Navarr\Depends\Controller; |
| 10 | + |
| 11 | +use DI\ContainerBuilder; |
| 12 | +use InvalidArgumentException; |
| 13 | +use Navarr\Attribute\Dependency; |
| 14 | +use Navarr\Depends\Command\WhyBlockCommand; |
| 15 | +use Navarr\Depends\Command\WhyBlockCommand\CsvOutputHandler; |
| 16 | +use Navarr\Depends\Command\WhyBlockCommand\JsonOutputHandler; |
| 17 | +use Navarr\Depends\Command\WhyBlockCommand\OutputHandlerInterface; |
| 18 | +use Navarr\Depends\Command\WhyBlockCommand\StandardOutputHandler; |
| 19 | +use Navarr\Depends\Command\WhyBlockCommand\XmlOutputHandler; |
| 20 | +use Navarr\Depends\IssueHandler\FailOnIssueHandler; |
| 21 | +use Navarr\Depends\IssueHandler\IssueHandlerInterface; |
| 22 | +use Navarr\Depends\IssueHandler\NotifyOnIssueHandler; |
| 23 | +use Navarr\Depends\Parser\AstParser; |
| 24 | +use Navarr\Depends\Parser\LegacyParser; |
| 25 | +use Navarr\Depends\Parser\ParserInterface; |
| 26 | +use Navarr\Depends\Parser\ParserPool; |
| 27 | +use Navarr\Depends\Proxy\StdOutWriter; |
| 28 | +use Navarr\Depends\Proxy\WriterInterface; |
| 29 | +use Navarr\Depends\ScopeDeterminer\ComposerScopeDeterminer; |
| 30 | +use Navarr\Depends\ScopeDeterminer\DirectoryScopeDeterminer; |
| 31 | +use Navarr\Depends\ScopeDeterminer\PhpFileFinder; |
| 32 | +use Navarr\Depends\ScopeDeterminer\ScopeDeterminerInterface; |
| 33 | +use Psr\Container\ContainerInterface; |
| 34 | +use Symfony\Component\Console\Command\Command; |
| 35 | +use Symfony\Component\Console\Input\InputArgument; |
| 36 | +use Symfony\Component\Console\Input\InputInterface; |
| 37 | +use Symfony\Component\Console\Input\InputOption; |
| 38 | +use Symfony\Component\Console\Output\OutputInterface; |
| 39 | + |
| 40 | +use function DI\autowire; |
| 41 | +use function DI\create; |
| 42 | + |
| 43 | +class WhyBlockCommandController extends Command |
| 44 | +{ |
| 45 | + private const LEGACY_ANNOTATION = 'include-legacy-annotations'; |
| 46 | + private const FAIL_ON_ERROR = 'fail-on-error'; |
| 47 | + private const OUTPUT_FORMAT = 'format'; |
| 48 | + |
| 49 | + private const FORMAT_TEXT = 'text'; |
| 50 | + private const FORMAT_CSV = 'csv'; |
| 51 | + private const FORMAT_JSON = 'json'; |
| 52 | + private const FORMAT_XML = 'xml'; |
| 53 | + |
| 54 | + private const ACCEPTABLE_FORMATS = [ |
| 55 | + self::FORMAT_CSV, |
| 56 | + self::FORMAT_TEXT, |
| 57 | + self::FORMAT_JSON, |
| 58 | + self::FORMAT_XML, |
| 59 | + ]; |
| 60 | + |
| 61 | + private const FORMAT_MAPPER = [ |
| 62 | + self::FORMAT_CSV => CsvOutputHandler::class, |
| 63 | + self::FORMAT_TEXT => StandardOutputHandler::class, |
| 64 | + self::FORMAT_JSON => JsonOutputHandler::class, |
| 65 | + self::FORMAT_XML => XmlOutputHandler::class, |
| 66 | + ]; |
| 67 | + |
| 68 | + // phpcs:disable Generic.Files.LineLength.TooLong -- Attribute support pre PHP 8 |
| 69 | + #[Dependency('symfony/console', '^5', 'Command\'s setName, addArgument and addOption methods as well as InputArgument\'s constants of REQUIRED and VALUE_NONE')] |
| 70 | + #[Dependency('php-di/php-di', '^6', 'DI\ContainerBuilder::addDefinitions and the existence of the DI\autowire function')] |
| 71 | + // phpcs:enable Generic.Files.LineLength.TooLong |
| 72 | + protected function configure(): void |
| 73 | + { |
| 74 | + $this->setName('why-block') |
| 75 | + ->addArgument('package', InputArgument::REQUIRED, 'Package to inspect') |
| 76 | + ->addArgument('version', InputArgument::REQUIRED, 'Version you want to update it to') |
| 77 | + ->addArgument('directory', InputArgument::REQUIRED, 'Directory to search in') |
| 78 | + ->addOption( |
| 79 | + self::OUTPUT_FORMAT, |
| 80 | + ['f'], |
| 81 | + InputOption::VALUE_OPTIONAL, |
| 82 | + 'Format to output results in. Accepted values: text, csv, json, xml', |
| 83 | + 'text' |
| 84 | + ) |
| 85 | + ->addOption( |
| 86 | + self::FAIL_ON_ERROR, |
| 87 | + ['e'], |
| 88 | + InputOption::VALUE_NONE, |
| 89 | + 'Immediately fail on parsing errors' |
| 90 | + ) |
| 91 | + ->addOption( |
| 92 | + self::LEGACY_ANNOTATION, |
| 93 | + ['l'], |
| 94 | + InputOption::VALUE_NONE, |
| 95 | + 'Include old @dependency/@composerDependency annotations in search' |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + #[Dependency('symfony/console', '^5', 'InputInterface::getOption and OutputInterface::writeln')] |
| 100 | + protected function execute( |
| 101 | + InputInterface $input, |
| 102 | + OutputInterface $output |
| 103 | + ): int { |
| 104 | + $packageToSearchFor = $input->getArgument('package'); |
| 105 | + $versionToCompareTo = $input->getArgument('version'); |
| 106 | + $directory = $input->getArgument('directory'); |
| 107 | + $outputFormat = $input->getOption(self::OUTPUT_FORMAT); |
| 108 | + |
| 109 | + if (!is_string($directory)) { |
| 110 | + throw new InvalidArgumentException('Only one directory is allowed'); |
| 111 | + } |
| 112 | + if (!is_string($packageToSearchFor)) { |
| 113 | + throw new InvalidArgumentException('Only one package is allowed'); |
| 114 | + } |
| 115 | + if (!is_string($versionToCompareTo)) { |
| 116 | + throw new InvalidArgumentException('Only one version is allowed'); |
| 117 | + } |
| 118 | + if (!is_string($outputFormat)) { |
| 119 | + throw new InvalidArgumentException('Only one output format is allowed'); |
| 120 | + } |
| 121 | + |
| 122 | + $outputFormat = strtolower($outputFormat); |
| 123 | + if (!in_array($outputFormat, static::ACCEPTABLE_FORMATS)) { |
| 124 | + $outputFormat = 'text'; |
| 125 | + } |
| 126 | + |
| 127 | + $containerBuilder = new ContainerBuilder(); |
| 128 | + $containerBuilder->addDefinitions( |
| 129 | + [ |
| 130 | + InputInterface::class => $input, |
| 131 | + OutputInterface::class => $output, |
| 132 | + IssueHandlerInterface::class => $input->getOption(static::FAIL_ON_ERROR) |
| 133 | + ? FailOnIssueHandler::class |
| 134 | + : NotifyOnIssueHandler::class, |
| 135 | + ParserInterface::class => static function (ContainerInterface $container) use ($input) { |
| 136 | + $parsers = [$container->get(AstParser::class)]; |
| 137 | + if ($input->getOption(static::LEGACY_ANNOTATION)) { |
| 138 | + $parsers[] = $container->get(LegacyParser::class); |
| 139 | + } |
| 140 | + return new ParserPool($parsers); |
| 141 | + }, |
| 142 | + WriterInterface::class => autowire(StdOutWriter::class), |
| 143 | + ScopeDeterminerInterface::class => static function (ContainerInterface $container) use ($directory) { |
| 144 | + return new DirectoryScopeDeterminer( |
| 145 | + $container->get(PhpFileFinder::class), |
| 146 | + $directory |
| 147 | + ); |
| 148 | + }, |
| 149 | + OutputHandlerInterface::class => autowire(static::FORMAT_MAPPER[$outputFormat]), |
| 150 | + ] |
| 151 | + ); |
| 152 | + $container = $containerBuilder->build(); |
| 153 | + |
| 154 | + /** @var WhyBlockCommand $command */ |
| 155 | + $command = $container->get(WhyBlockCommand::class); |
| 156 | + return $command->execute($packageToSearchFor, $versionToCompareTo); |
| 157 | + } |
| 158 | +} |
0 commit comments