LintCommand.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Yaml\Command;
  11. use Symfony\Component\Console\CI\GithubActionReporter;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Completion\CompletionInput;
  14. use Symfony\Component\Console\Completion\CompletionSuggestions;
  15. use Symfony\Component\Console\Exception\InvalidArgumentException;
  16. use Symfony\Component\Console\Exception\RuntimeException;
  17. use Symfony\Component\Console\Input\InputArgument;
  18. use Symfony\Component\Console\Input\InputInterface;
  19. use Symfony\Component\Console\Input\InputOption;
  20. use Symfony\Component\Console\Output\OutputInterface;
  21. use Symfony\Component\Console\Style\SymfonyStyle;
  22. use Symfony\Component\Yaml\Exception\ParseException;
  23. use Symfony\Component\Yaml\Parser;
  24. use Symfony\Component\Yaml\Yaml;
  25. /**
  26. * Validates YAML files syntax and outputs encountered errors.
  27. *
  28. * @author Grégoire Pineau <lyrixx@lyrixx.info>
  29. * @author Robin Chalas <robin.chalas@gmail.com>
  30. */
  31. class LintCommand extends Command
  32. {
  33. protected static $defaultName = 'lint:yaml';
  34. protected static $defaultDescription = 'Lint a YAML file and outputs encountered errors';
  35. private $parser;
  36. private $format;
  37. private $displayCorrectFiles;
  38. private $directoryIteratorProvider;
  39. private $isReadableProvider;
  40. public function __construct(string $name = null, callable $directoryIteratorProvider = null, callable $isReadableProvider = null)
  41. {
  42. parent::__construct($name);
  43. $this->directoryIteratorProvider = $directoryIteratorProvider;
  44. $this->isReadableProvider = $isReadableProvider;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. protected function configure()
  50. {
  51. $this
  52. ->setDescription(self::$defaultDescription)
  53. ->addArgument('filename', InputArgument::IS_ARRAY, 'A file, a directory or "-" for reading from STDIN')
  54. ->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format')
  55. ->addOption('exclude', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Path(s) to exclude')
  56. ->addOption('parse-tags', null, InputOption::VALUE_NEGATABLE, 'Parse custom tags', null)
  57. ->setHelp(<<<EOF
  58. The <info>%command.name%</info> command lints a YAML file and outputs to STDOUT
  59. the first encountered syntax error.
  60. You can validates YAML contents passed from STDIN:
  61. <info>cat filename | php %command.full_name% -</info>
  62. You can also validate the syntax of a file:
  63. <info>php %command.full_name% filename</info>
  64. Or of a whole directory:
  65. <info>php %command.full_name% dirname</info>
  66. <info>php %command.full_name% dirname --format=json</info>
  67. You can also exclude one or more specific files:
  68. <info>php %command.full_name% dirname --exclude="dirname/foo.yaml" --exclude="dirname/bar.yaml"</info>
  69. EOF
  70. )
  71. ;
  72. }
  73. protected function execute(InputInterface $input, OutputInterface $output)
  74. {
  75. $io = new SymfonyStyle($input, $output);
  76. $filenames = (array) $input->getArgument('filename');
  77. $excludes = $input->getOption('exclude');
  78. $this->format = $input->getOption('format');
  79. $flags = $input->getOption('parse-tags');
  80. if ('github' === $this->format && !class_exists(GithubActionReporter::class)) {
  81. throw new \InvalidArgumentException('The "github" format is only available since "symfony/console" >= 5.3.');
  82. }
  83. if (null === $this->format) {
  84. // Autodetect format according to CI environment
  85. $this->format = class_exists(GithubActionReporter::class) && GithubActionReporter::isGithubActionEnvironment() ? 'github' : 'txt';
  86. }
  87. $flags = $flags ? Yaml::PARSE_CUSTOM_TAGS : 0;
  88. $this->displayCorrectFiles = $output->isVerbose();
  89. if (['-'] === $filenames) {
  90. return $this->display($io, [$this->validate(file_get_contents('php://stdin'), $flags)]);
  91. }
  92. if (!$filenames) {
  93. throw new RuntimeException('Please provide a filename or pipe file content to STDIN.');
  94. }
  95. $filesInfo = [];
  96. foreach ($filenames as $filename) {
  97. if (!$this->isReadable($filename)) {
  98. throw new RuntimeException(sprintf('File or directory "%s" is not readable.', $filename));
  99. }
  100. foreach ($this->getFiles($filename) as $file) {
  101. if (!\in_array($file->getPathname(), $excludes, true)) {
  102. $filesInfo[] = $this->validate(file_get_contents($file), $flags, $file);
  103. }
  104. }
  105. }
  106. return $this->display($io, $filesInfo);
  107. }
  108. private function validate(string $content, int $flags, string $file = null)
  109. {
  110. $prevErrorHandler = set_error_handler(function ($level, $message, $file, $line) use (&$prevErrorHandler) {
  111. if (\E_USER_DEPRECATED === $level) {
  112. throw new ParseException($message, $this->getParser()->getRealCurrentLineNb() + 1);
  113. }
  114. return $prevErrorHandler ? $prevErrorHandler($level, $message, $file, $line) : false;
  115. });
  116. try {
  117. $this->getParser()->parse($content, Yaml::PARSE_CONSTANT | $flags);
  118. } catch (ParseException $e) {
  119. return ['file' => $file, 'line' => $e->getParsedLine(), 'valid' => false, 'message' => $e->getMessage()];
  120. } finally {
  121. restore_error_handler();
  122. }
  123. return ['file' => $file, 'valid' => true];
  124. }
  125. private function display(SymfonyStyle $io, array $files): int
  126. {
  127. switch ($this->format) {
  128. case 'txt':
  129. return $this->displayTxt($io, $files);
  130. case 'json':
  131. return $this->displayJson($io, $files);
  132. case 'github':
  133. return $this->displayTxt($io, $files, true);
  134. default:
  135. throw new InvalidArgumentException(sprintf('The format "%s" is not supported.', $this->format));
  136. }
  137. }
  138. private function displayTxt(SymfonyStyle $io, array $filesInfo, bool $errorAsGithubAnnotations = false): int
  139. {
  140. $countFiles = \count($filesInfo);
  141. $erroredFiles = 0;
  142. $suggestTagOption = false;
  143. if ($errorAsGithubAnnotations) {
  144. $githubReporter = new GithubActionReporter($io);
  145. }
  146. foreach ($filesInfo as $info) {
  147. if ($info['valid'] && $this->displayCorrectFiles) {
  148. $io->comment('<info>OK</info>'.($info['file'] ? sprintf(' in %s', $info['file']) : ''));
  149. } elseif (!$info['valid']) {
  150. ++$erroredFiles;
  151. $io->text('<error> ERROR </error>'.($info['file'] ? sprintf(' in %s', $info['file']) : ''));
  152. $io->text(sprintf('<error> >> %s</error>', $info['message']));
  153. if (false !== strpos($info['message'], 'PARSE_CUSTOM_TAGS')) {
  154. $suggestTagOption = true;
  155. }
  156. if ($errorAsGithubAnnotations) {
  157. $githubReporter->error($info['message'], $info['file'] ?? 'php://stdin', $info['line']);
  158. }
  159. }
  160. }
  161. if (0 === $erroredFiles) {
  162. $io->success(sprintf('All %d YAML files contain valid syntax.', $countFiles));
  163. } else {
  164. $io->warning(sprintf('%d YAML files have valid syntax and %d contain errors.%s', $countFiles - $erroredFiles, $erroredFiles, $suggestTagOption ? ' Use the --parse-tags option if you want parse custom tags.' : ''));
  165. }
  166. return min($erroredFiles, 1);
  167. }
  168. private function displayJson(SymfonyStyle $io, array $filesInfo): int
  169. {
  170. $errors = 0;
  171. array_walk($filesInfo, function (&$v) use (&$errors) {
  172. $v['file'] = (string) $v['file'];
  173. if (!$v['valid']) {
  174. ++$errors;
  175. }
  176. if (isset($v['message']) && false !== strpos($v['message'], 'PARSE_CUSTOM_TAGS')) {
  177. $v['message'] .= ' Use the --parse-tags option if you want parse custom tags.';
  178. }
  179. });
  180. $io->writeln(json_encode($filesInfo, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES));
  181. return min($errors, 1);
  182. }
  183. private function getFiles(string $fileOrDirectory): iterable
  184. {
  185. if (is_file($fileOrDirectory)) {
  186. yield new \SplFileInfo($fileOrDirectory);
  187. return;
  188. }
  189. foreach ($this->getDirectoryIterator($fileOrDirectory) as $file) {
  190. if (!\in_array($file->getExtension(), ['yml', 'yaml'])) {
  191. continue;
  192. }
  193. yield $file;
  194. }
  195. }
  196. private function getParser(): Parser
  197. {
  198. if (!$this->parser) {
  199. $this->parser = new Parser();
  200. }
  201. return $this->parser;
  202. }
  203. private function getDirectoryIterator(string $directory): iterable
  204. {
  205. $default = function ($directory) {
  206. return new \RecursiveIteratorIterator(
  207. new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS),
  208. \RecursiveIteratorIterator::LEAVES_ONLY
  209. );
  210. };
  211. if (null !== $this->directoryIteratorProvider) {
  212. return ($this->directoryIteratorProvider)($directory, $default);
  213. }
  214. return $default($directory);
  215. }
  216. private function isReadable(string $fileOrDirectory): bool
  217. {
  218. $default = function ($fileOrDirectory) {
  219. return is_readable($fileOrDirectory);
  220. };
  221. if (null !== $this->isReadableProvider) {
  222. return ($this->isReadableProvider)($fileOrDirectory, $default);
  223. }
  224. return $default($fileOrDirectory);
  225. }
  226. public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
  227. {
  228. if ($input->mustSuggestOptionValuesFor('format')) {
  229. $suggestions->suggestValues(['txt', 'json', 'github']);
  230. }
  231. }
  232. }