|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com> |
| 5 | + * SPDX-License-Identifier: MIT |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace Respect\Dev\Commands; |
| 11 | + |
| 12 | +use Nette\UnexpectedValueException; |
| 13 | +use Respect\Dev\Markdown\Builder; |
| 14 | +use Respect\Dev\Markdown\Differ; |
| 15 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 16 | +use Symfony\Component\Console\Command\Command; |
| 17 | +use Symfony\Component\Console\Input\InputInterface; |
| 18 | +use Symfony\Component\Console\Output\OutputInterface; |
| 19 | +use Symfony\Component\Finder\Finder; |
| 20 | + |
| 21 | +use function array_keys; |
| 22 | +use function array_map; |
| 23 | +use function dirname; |
| 24 | +use function file_get_contents; |
| 25 | +use function ksort; |
| 26 | +use function preg_match; |
| 27 | +use function preg_match_all; |
| 28 | +use function sort; |
| 29 | +use function sprintf; |
| 30 | + |
| 31 | +#[AsCommand( |
| 32 | + name: 'docs:validators:index', |
| 33 | + description: 'Update the index of validators by category', |
| 34 | +)] |
| 35 | +final class DocsValidatorsListCommand extends Command |
| 36 | +{ |
| 37 | + public function __construct( |
| 38 | + private readonly Differ $differ, |
| 39 | + ) { |
| 40 | + parent::__construct(); |
| 41 | + } |
| 42 | + |
| 43 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 44 | + { |
| 45 | + $validators = $this->getSortedListOfValidators(); |
| 46 | + |
| 47 | + $validatorsByCategory = []; |
| 48 | + foreach ($validators as $validator) { |
| 49 | + foreach ($this->getCategoriesByValidator($validator) as $category) { |
| 50 | + $validatorsByCategory[$category] ??= []; |
| 51 | + $validatorsByCategory[$category][] = $validator; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + ksort($validatorsByCategory); |
| 56 | + $validatorsByCategory['Alphabetically'] = $validators; |
| 57 | + |
| 58 | + $categories = array_keys($validatorsByCategory); |
| 59 | + |
| 60 | + $build = Builder::fromDocs('validators/index.md'); |
| 61 | + $build->h1('Overviews'); |
| 62 | + $build->paragraph('In this page you will find a list of validators by their category.'); |
| 63 | + $build->newLine(); |
| 64 | + |
| 65 | + foreach ($categories as $category) { |
| 66 | + $build->h2($category); |
| 67 | + if (isset($validatorsByCategory[$category])) { |
| 68 | + $categoryValidators = $validatorsByCategory[$category]; |
| 69 | + sort($categoryValidators); |
| 70 | + foreach ($categoryValidators as $validator) { |
| 71 | + $build->anchorListItem($validator, sprintf('validators/%1$s.md', $validator)); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + $build->newLine(); |
| 76 | + } |
| 77 | + |
| 78 | + $diff = $this->differ->diff($build); |
| 79 | + if ($diff === null) { |
| 80 | + $output->writeln('<info>No changes needed.</info>'); |
| 81 | + |
| 82 | + return Command::SUCCESS; |
| 83 | + } |
| 84 | + |
| 85 | + $build->save(); |
| 86 | + |
| 87 | + $output->writeln($diff); |
| 88 | + $output->writeln('<comment>Changes needed.</comment>'); |
| 89 | + |
| 90 | + return Command::FAILURE; |
| 91 | + } |
| 92 | + |
| 93 | + /** @return array<string> */ |
| 94 | + private function getSortedListOfValidators(): array |
| 95 | + { |
| 96 | + $finder = new Finder(); |
| 97 | + $finder->in(dirname(__DIR__, 2) . '/library/Validators')->depth(0)->files()->name('*.php')->sortByName(); |
| 98 | + |
| 99 | + $validators = []; |
| 100 | + foreach ($finder as $file) { |
| 101 | + $validators[] = $file->getBasename('.php'); |
| 102 | + } |
| 103 | + |
| 104 | + return $validators; |
| 105 | + } |
| 106 | + |
| 107 | + /** @return array<string> */ |
| 108 | + private function getCategoriesByValidator(string $validator): array |
| 109 | + { |
| 110 | + $docsDirectory = dirname(__DIR__, 2) . '/docs'; |
| 111 | + $filename = sprintf('%s/validators/%s.md', $docsDirectory, $validator); |
| 112 | + $content = file_get_contents($filename); |
| 113 | + if ($content === false) { |
| 114 | + throw new UnexpectedValueException(sprintf('Could not find content for "%s"', $validator)); |
| 115 | + } |
| 116 | + |
| 117 | + if (!preg_match('/## Categorization\s*(.*?)\s*## Changelog/s', $content, $matches)) { |
| 118 | + throw new UnexpectedValueException(sprintf('Could not find categorization for "%s"', $validator)); |
| 119 | + } |
| 120 | + |
| 121 | + preg_match_all('/^-\s*(.+)$/m', $matches[1], $categoryMatches); |
| 122 | + |
| 123 | + return array_map('trim', $categoryMatches[1]); |
| 124 | + } |
| 125 | +} |
0 commit comments