ProblemChecker.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. foreach ($data as $problem) {
  23. $class = $problem['class'];
  24. $this->problems[] = new $class($problem);
  25. }
  26. }
  27. }
  28. public function getStatusFile()
  29. {
  30. return $this->status_file;
  31. }
  32. public function statusFileExists()
  33. {
  34. return file_exists($this->status_file);
  35. }
  36. public function storeStatusFile()
  37. {
  38. $problems = $this->getProblemsSerializable();
  39. $json = json_encode($problems);
  40. file_put_contents($this->status_file, $json);
  41. }
  42. public function check($problems_dir)
  43. {
  44. $problems = [];
  45. $problems_found = false;
  46. foreach (new \DirectoryIterator($problems_dir) as $file) {
  47. if ($file->isDot() || $file->isDir()) {
  48. continue;
  49. }
  50. $classname = 'Grav\\Plugin\\Problems\\' . $file->getBasename('.php');
  51. /** @var Problem $problem */
  52. $problem = new $classname();
  53. $problems[$problem->getId()] = $problem;
  54. }
  55. // Fire event to allow other plugins to add problems
  56. Grav::instance()->fireEvent('onProblemsInitialized', new Event(['problems' => $problems]));
  57. // Get the problems in order
  58. usort($problems, function($a, $b) {
  59. return $b->getOrder() - $a->getOrder();
  60. });
  61. // run the process methods in new order
  62. foreach ($problems as $problem) {
  63. $problem->process();
  64. if ($problem->getStatus() === false && $problem->getLevel() === Problem::LEVEL_CRITICAL) {
  65. $problems_found = true;
  66. }
  67. }
  68. $this->problems = $problems;
  69. return $problems_found;
  70. }
  71. public function getProblems()
  72. {
  73. if (empty($this->problems)) {
  74. $this->check();
  75. }
  76. $problems = $this->problems;
  77. // Put the failed ones first
  78. usort($problems, function($a, $b) {
  79. return $a->getStatus() - $b->getStatus();
  80. });
  81. return $problems;
  82. }
  83. public function getProblemsSerializable()
  84. {
  85. if (empty($this->problems)) {
  86. $this->getProblems();
  87. }
  88. $problems = [];
  89. foreach ($this->problems as $problem) {
  90. $problems[] = $problem->toArray();
  91. }
  92. return $problems;
  93. }
  94. }