YamlLinter.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * @package Grav\Common\Helpers
  4. *
  5. * @copyright Copyright (c) 2015 - 2021 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Helpers;
  9. use Exception;
  10. use Grav\Common\Grav;
  11. use RecursiveDirectoryIterator;
  12. use RecursiveIteratorIterator;
  13. use RegexIterator;
  14. use RocketTheme\Toolbox\File\MarkdownFile;
  15. use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
  16. use Symfony\Component\Yaml\Yaml;
  17. /**
  18. * Class YamlLinter
  19. * @package Grav\Common\Helpers
  20. */
  21. class YamlLinter
  22. {
  23. /**
  24. * @param string|null $folder
  25. * @return array
  26. */
  27. public static function lint(string $folder = null)
  28. {
  29. if (null !== $folder) {
  30. $folder = $folder ?: GRAV_ROOT;
  31. return static::recurseFolder($folder);
  32. }
  33. return array_merge(static::lintConfig(), static::lintPages(), static::lintBlueprints());
  34. }
  35. /**
  36. * @return array
  37. */
  38. public static function lintPages()
  39. {
  40. return static::recurseFolder('page://');
  41. }
  42. /**
  43. * @return array
  44. */
  45. public static function lintConfig()
  46. {
  47. return static::recurseFolder('config://');
  48. }
  49. /**
  50. * @return array
  51. */
  52. public static function lintBlueprints()
  53. {
  54. /** @var UniformResourceLocator $locator */
  55. $locator = Grav::instance()['locator'];
  56. $current_theme = Grav::instance()['config']->get('system.pages.theme');
  57. $theme_path = 'themes://' . $current_theme . '/blueprints';
  58. $locator->addPath('blueprints', '', [$theme_path]);
  59. return static::recurseFolder('blueprints://');
  60. }
  61. /**
  62. * @param string $path
  63. * @param string $extensions
  64. * @return array
  65. */
  66. public static function recurseFolder($path, $extensions = '(md|yaml)')
  67. {
  68. $lint_errors = [];
  69. /** @var UniformResourceLocator $locator */
  70. $locator = Grav::instance()['locator'];
  71. $flags = RecursiveDirectoryIterator::SKIP_DOTS | RecursiveDirectoryIterator::FOLLOW_SYMLINKS;
  72. if ($locator->isStream($path)) {
  73. $directory = $locator->getRecursiveIterator($path, $flags);
  74. } else {
  75. $directory = new RecursiveDirectoryIterator($path, $flags);
  76. }
  77. $recursive = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::SELF_FIRST);
  78. $iterator = new RegexIterator($recursive, '/^.+\.'.$extensions.'$/ui');
  79. /** @var RecursiveDirectoryIterator $file */
  80. foreach ($iterator as $filepath => $file) {
  81. try {
  82. Yaml::parse(static::extractYaml($filepath));
  83. } catch (Exception $e) {
  84. $lint_errors[str_replace(GRAV_ROOT, '', $filepath)] = $e->getMessage();
  85. }
  86. }
  87. return $lint_errors;
  88. }
  89. /**
  90. * @param string $path
  91. * @return string
  92. */
  93. protected static function extractYaml($path)
  94. {
  95. $extension = pathinfo($path, PATHINFO_EXTENSION);
  96. if ($extension === 'md') {
  97. $file = MarkdownFile::instance($path);
  98. $contents = $file->frontmatter();
  99. $file->free();
  100. } else {
  101. $contents = file_get_contents($path);
  102. }
  103. return $contents;
  104. }
  105. }