|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +use Symfony\Component\Console\Application; |
| 5 | +use Symfony\Component\Console\Command\Command; |
| 6 | +use Symfony\Component\Console\Helper\ProgressIndicator; |
| 7 | +use Symfony\Component\Console\Output\OutputInterface; |
| 8 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 9 | +use Symfony\Component\DomCrawler\Crawler; |
| 10 | +use Symfony\Component\HttpClient\HttpClient; |
| 11 | + |
| 12 | +require_once \dirname(__DIR__).'/vendor/autoload.php'; |
| 13 | + |
| 14 | +class ValidateUrlDoc |
| 15 | +{ |
| 16 | + private const AVAILABLE_EXTENSIONS = ['php', 'md']; |
| 17 | + |
| 18 | + private const DIR_AND_FILES_TO_CHECK = [ |
| 19 | + __DIR__.'/../src/Builder/Pdf', |
| 20 | + __DIR__.'/../src/Builder/Screenshot', |
| 21 | + __DIR__.'/../src/Builder/Behaviors', |
| 22 | + __DIR__, |
| 23 | + __DIR__.'/../README.md', |
| 24 | + __DIR__.'/../src/DependencyInjection/Configuration.php', |
| 25 | + ]; |
| 26 | + |
| 27 | + public function validate(OutputInterface $output, SymfonyStyle $io): int |
| 28 | + { |
| 29 | + $progressBar = new ProgressIndicator($output); |
| 30 | + $progressBar->start('Processing...'); |
| 31 | + |
| 32 | + $urls = []; |
| 33 | + $files = $this->getFiles(self::DIR_AND_FILES_TO_CHECK); |
| 34 | + foreach ($files as $file) { |
| 35 | + array_push($urls, ...$this->extractUrls($file)); |
| 36 | + } |
| 37 | + |
| 38 | + $urls = array_unique($urls); |
| 39 | + |
| 40 | + $client = HttpClient::create(); |
| 41 | + $allResponses = []; |
| 42 | + foreach ($urls as $url) { |
| 43 | + $allResponses[] = $client->request('GET', $url); |
| 44 | + } |
| 45 | + |
| 46 | + $failedUrls = []; |
| 47 | + foreach ($allResponses as $response) { |
| 48 | + try { |
| 49 | + $url = $response->getInfo('url'); |
| 50 | + |
| 51 | + $statusCode = $response->getStatusCode(); |
| 52 | + if (200 !== $statusCode) { |
| 53 | + $failedUrls[$url] = "HTTP {$statusCode} error for: {$url}"; |
| 54 | + } |
| 55 | + |
| 56 | + if (!\array_key_exists($url, $failedUrls)) { |
| 57 | + $checkError = $this->checkContentResponse($response->getInfo('url'), $response->getContent()); |
| 58 | + if (\is_string($checkError)) { |
| 59 | + $failedUrls[$url] = $checkError; |
| 60 | + } |
| 61 | + } |
| 62 | + } catch (Throwable $e) { |
| 63 | + $url = $response->getInfo('url'); |
| 64 | + $failedUrls[$url] = "Error for {$url}: {$e->getMessage()}"; |
| 65 | + } finally { |
| 66 | + $progressBar->advance(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + $progressBar->finish('Finished'); |
| 71 | + $failedUrls = array_unique($failedUrls); |
| 72 | + |
| 73 | + if (\count($failedUrls) > 0) { |
| 74 | + $io->error('Some external links are invalid:'); |
| 75 | + $io->listing($failedUrls); |
| 76 | + |
| 77 | + return Command::FAILURE; |
| 78 | + } |
| 79 | + |
| 80 | + $io->success('All external links are valid.'); |
| 81 | + |
| 82 | + return Command::SUCCESS; |
| 83 | + } |
| 84 | + |
| 85 | + private function getFiles(array $paths): Generator |
| 86 | + { |
| 87 | + foreach ($paths as $path) { |
| 88 | + if (is_file($path)) { |
| 89 | + $splInfo = new SplFileInfo($path); |
| 90 | + if (!\in_array($splInfo->getExtension(), self::AVAILABLE_EXTENSIONS, true)) { |
| 91 | + throw new RuntimeException(\sprintf('File "%s" with "%s" extension is not allowed.', $splInfo->getFilename(), $splInfo->getExtension())); |
| 92 | + } |
| 93 | + |
| 94 | + yield $splInfo; |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); |
| 99 | + foreach ($iterator as $file) { |
| 100 | + if ($file->isFile() && \in_array(pathinfo($file, \PATHINFO_EXTENSION), self::AVAILABLE_EXTENSIONS, true)) { |
| 101 | + yield $file; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private function extractUrls(SplFileInfo $file): array |
| 108 | + { |
| 109 | + $content = file_get_contents($file->getPathname()); |
| 110 | + |
| 111 | + // https://regex101.com/r/t5uiUp/1 |
| 112 | + // https://regex101.com/r/p62cij/1 |
| 113 | + match ($file->getExtension()) { |
| 114 | + 'php' => preg_match_all('/(?:@see\s+|->info\([^)]*)(https?:\/\/[^\s\'")]+)(?:[^)]*\))?/', $content, $matches), |
| 115 | + 'md' => preg_match_all('/\[[^\]]+\]\((https?:\/\/[^\s\*)]+)\)/', $content, $matches), |
| 116 | + }; |
| 117 | + |
| 118 | + return $matches[1] ?? []; |
| 119 | + } |
| 120 | + |
| 121 | + private function checkContentResponse(string $url, string $content): string|null |
| 122 | + { |
| 123 | + $crawler = new Crawler($content); |
| 124 | + $parsedUrl = parse_url($url); |
| 125 | + |
| 126 | + if (\array_key_exists('fragment', $parsedUrl)) { |
| 127 | + $fragment = $parsedUrl['fragment']; |
| 128 | + if ($crawler->filter('#'.$fragment)->count() > 0 || $crawler->filter('a[name="'.$fragment.'"]')->count() > 0) { |
| 129 | + return null; |
| 130 | + } |
| 131 | + |
| 132 | + return \sprintf('Cannot find anchor "%s" for "%s", remove or update the link in the PHPdoc', $fragment, $url); |
| 133 | + } |
| 134 | + |
| 135 | + return null; |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +$application = new Application(); |
| 140 | +$application->register('check') |
| 141 | + ->setCode(function (OutputInterface $output, SymfonyStyle $io) { |
| 142 | + return (new ValidateUrlDoc())->validate($output, $io); |
| 143 | + }) |
| 144 | +; |
| 145 | + |
| 146 | +$application->setDefaultCommand('check'); |
| 147 | +$application->run(); |
0 commit comments