YamlLinterCommand.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * @package Grav\Console\Cli
  4. *
  5. * @copyright Copyright (C) 2015 - 2019 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\Grav;
  10. use Grav\Common\Helpers\LogViewer;
  11. use Grav\Common\Helpers\YamlLinter;
  12. use Grav\Common\Utils;
  13. use Grav\Console\ConsoleCommand;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Style\SymfonyStyle;
  16. class YamlLinterCommand extends ConsoleCommand
  17. {
  18. protected function configure()
  19. {
  20. $this
  21. ->setName('yamllinter')
  22. ->addOption(
  23. 'env',
  24. 'e',
  25. InputOption::VALUE_OPTIONAL,
  26. 'The environment to trigger a specific configuration. For example: localhost, mysite.dev, www.mysite.com'
  27. )
  28. ->setDescription('Checks various files for YAML errors')
  29. ->setHelp("Checks various files for YAML errors");
  30. }
  31. protected function serve()
  32. {
  33. $grav = Grav::instance();
  34. $grav->setup();
  35. $io = new SymfonyStyle($this->input, $this->output);
  36. $io->title('Yaml Linter');
  37. $io->section('User Configuration');
  38. $errors = YamlLinter::lintConfig();
  39. if (empty($errors)) {
  40. $io->success('No YAML Linting issues with configuration');
  41. } else {
  42. $this->displayErrors($errors, $io);
  43. }
  44. $io->section('Pages Frontmatter');
  45. $errors = YamlLinter::lintPages();
  46. if (empty($errors)) {
  47. $io->success('No YAML Linting issues with pages');
  48. } else {
  49. $this->displayErrors($errors, $io);
  50. }
  51. $io->section('Page Blueprints');
  52. $errors = YamlLinter::lintBlueprints();
  53. if (empty($errors)) {
  54. $io->success('No YAML Linting issues with blueprints');
  55. } else {
  56. $this->displayErrors($errors, $io);
  57. }
  58. }
  59. protected function displayErrors($errors, $io)
  60. {
  61. $io->error("YAML Linting issues found...");
  62. foreach ($errors as $path => $error) {
  63. $io->writeln("<yellow>$path</yellow> - $error");
  64. }
  65. }
  66. }