ProblemChecker.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace Grav\Plugin\Problems\Base;
  3. use Grav\Common\Cache;
  4. use Grav\Common\Grav;
  5. use RocketTheme\Toolbox\Event\Event;
  6. class ProblemChecker
  7. {
  8. const PROBLEMS_PREFIX = 'problem-check-';
  9. protected $problems = [];
  10. protected $status_file;
  11. public function __construct()
  12. {
  13. /** @var Cache $cache */
  14. $cache = Grav::instance()['cache'];
  15. $this->status_file = CACHE_DIR . $this::PROBLEMS_PREFIX . $cache->getKey() . '.json';
  16. }
  17. public function load()
  18. {
  19. if ($this->statusFileExists()) {
  20. $json = file_get_contents($this->status_file) ?: '';
  21. $data = json_decode($json, true);
  22. if (!is_array($data)) {
  23. return false;
  24. }
  25. foreach ($data as $problem) {
  26. $class = $problem['class'];
  27. $this->problems[] = new $class($problem);
  28. }
  29. }
  30. return true;
  31. }
  32. public function getStatusFile()
  33. {
  34. return $this->status_file;
  35. }
  36. public function statusFileExists()
  37. {
  38. return file_exists($this->status_file);
  39. }
  40. public function storeStatusFile()
  41. {
  42. $problems = $this->getProblemsSerializable();
  43. $json = json_encode($problems);
  44. file_put_contents($this->status_file, $json);
  45. }
  46. public function check($problems_dir = null)
  47. {
  48. $problems_dir = $problems_dir ?: dirname(__DIR__);
  49. $problems = [];
  50. $problems_found = false;
  51. foreach (new \DirectoryIterator($problems_dir) as $file) {
  52. if ($file->isDot() || $file->isDir()) {
  53. continue;
  54. }
  55. $classname = 'Grav\\Plugin\\Problems\\' . $file->getBasename('.php');
  56. /** @var Problem $problem */
  57. $problem = new $classname();
  58. $problems[$problem->getId()] = $problem;
  59. }
  60. // Fire event to allow other plugins to add problems
  61. Grav::instance()->fireEvent('onProblemsInitialized', new Event(['problems' => $problems]));
  62. // Get the problems in order
  63. usort($problems, function($a, $b) {
  64. /** @var Problem $a */
  65. /** @var Problem $b */
  66. return $b->getOrder() - $a->getOrder();
  67. });
  68. // run the process methods in new order
  69. foreach ($problems as $problem) {
  70. $problem->process();
  71. if ($problem->getStatus() === false && $problem->getLevel() === Problem::LEVEL_CRITICAL) {
  72. $problems_found = true;
  73. }
  74. }
  75. $this->problems = $problems;
  76. return $problems_found;
  77. }
  78. public function getProblems()
  79. {
  80. if (empty($this->problems)) {
  81. $this->check();
  82. }
  83. $problems = $this->problems;
  84. // Put the failed ones first
  85. usort($problems, function($a, $b) {
  86. /** @var Problem $a */
  87. /** @var Problem $b */
  88. return $a->getStatus() - $b->getStatus();
  89. });
  90. return $problems;
  91. }
  92. public function getProblemsSerializable()
  93. {
  94. if (empty($this->problems)) {
  95. $this->getProblems();
  96. }
  97. $problems = [];
  98. foreach ($this->problems as $problem) {
  99. $problems[] = $problem->toArray();
  100. }
  101. return $problems;
  102. }
  103. }