YamlLinterCommand.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * @package Grav\Console\Cli
  4. *
  5. * @copyright Copyright (c) 2015 - 2023 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Console\Cli;
  9. use Grav\Common\Helpers\YamlLinter;
  10. use Grav\Console\GravCommand;
  11. use Symfony\Component\Console\Input\InputOption;
  12. use Symfony\Component\Console\Style\SymfonyStyle;
  13. /**
  14. * Class YamlLinterCommand
  15. * @package Grav\Console\Cli
  16. */
  17. class YamlLinterCommand extends GravCommand
  18. {
  19. /**
  20. * @return void
  21. */
  22. protected function configure(): void
  23. {
  24. $this
  25. ->setName('yamllinter')
  26. ->addOption(
  27. 'all',
  28. 'a',
  29. InputOption::VALUE_NONE,
  30. 'Go through the whole Grav installation'
  31. )
  32. ->addOption(
  33. 'folder',
  34. 'f',
  35. InputOption::VALUE_OPTIONAL,
  36. 'Go through specific folder'
  37. )
  38. ->setDescription('Checks various files for YAML errors')
  39. ->setHelp('Checks various files for YAML errors');
  40. }
  41. /**
  42. * @return int
  43. */
  44. protected function serve(): int
  45. {
  46. $input = $this->getInput();
  47. $io = $this->getIO();
  48. $io->title('Yaml Linter');
  49. $error = 0;
  50. if ($input->getOption('all')) {
  51. $io->section('All');
  52. $errors = YamlLinter::lint('');
  53. if (empty($errors)) {
  54. $io->success('No YAML Linting issues found');
  55. } else {
  56. $error = 1;
  57. $this->displayErrors($errors, $io);
  58. }
  59. } elseif ($folder = $input->getOption('folder')) {
  60. $io->section($folder);
  61. $errors = YamlLinter::lint($folder);
  62. if (empty($errors)) {
  63. $io->success('No YAML Linting issues found');
  64. } else {
  65. $error = 1;
  66. $this->displayErrors($errors, $io);
  67. }
  68. } else {
  69. $io->section('User Configuration');
  70. $errors = YamlLinter::lintConfig();
  71. if (empty($errors)) {
  72. $io->success('No YAML Linting issues with configuration');
  73. } else {
  74. $error = 1;
  75. $this->displayErrors($errors, $io);
  76. }
  77. $io->section('Pages Frontmatter');
  78. $errors = YamlLinter::lintPages();
  79. if (empty($errors)) {
  80. $io->success('No YAML Linting issues with pages');
  81. } else {
  82. $error = 1;
  83. $this->displayErrors($errors, $io);
  84. }
  85. $io->section('Page Blueprints');
  86. $errors = YamlLinter::lintBlueprints();
  87. if (empty($errors)) {
  88. $io->success('No YAML Linting issues with blueprints');
  89. } else {
  90. $error = 1;
  91. $this->displayErrors($errors, $io);
  92. }
  93. }
  94. return $error;
  95. }
  96. /**
  97. * @param array $errors
  98. * @param SymfonyStyle $io
  99. * @return void
  100. */
  101. protected function displayErrors(array $errors, SymfonyStyle $io): void
  102. {
  103. $io->error('YAML Linting issues found...');
  104. foreach ($errors as $path => $error) {
  105. $io->writeln("<yellow>{$path}</yellow> - {$error}");
  106. }
  107. }
  108. }