problems.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace Grav\Plugin;
  3. use Grav\Common\Plugin;
  4. use Grav\Common\Uri;
  5. use Grav\Plugin\Problems\Base\ProblemChecker;
  6. use RocketTheme\Toolbox\Event\Event;
  7. class ProblemsPlugin extends Plugin
  8. {
  9. protected $checker;
  10. protected $problems = [];
  11. /**
  12. * @return array
  13. */
  14. public static function getSubscribedEvents()
  15. {
  16. return [
  17. 'onPluginsInitialized' => ['onPluginsInitialized', 100001],
  18. 'onFatalException' => ['onFatalException', 0],
  19. 'onAdminGenerateReports' => ['onAdminGenerateReports', 0],
  20. ];
  21. }
  22. public function onFatalException()
  23. {
  24. if (\defined('GRAV_CLI') || $this->isAdmin()) {
  25. return;
  26. }
  27. // Run through potential issues
  28. if ($this->problemsFound()) {
  29. $this->renderProblems();
  30. }
  31. }
  32. public function onPluginsInitialized()
  33. {
  34. require __DIR__ . '/vendor/autoload.php';
  35. if (\defined('GRAV_CLI') || $this->isAdmin()) {
  36. return;
  37. }
  38. $this->checker = new ProblemChecker();
  39. if (!$this->checker->statusFileExists()) {
  40. // If no issues remain, save a state file in the cache
  41. if (!$this->problemsFound()) {
  42. // delete any existing validated files
  43. foreach (new \GlobIterator(CACHE_DIR . ProblemChecker::PROBLEMS_PREFIX . '*') as $fileInfo) {
  44. @unlink($fileInfo->getPathname());
  45. }
  46. // create a file in the cache dir so it only runs on cache changes
  47. $this->checker->storeStatusFile();
  48. } else {
  49. $this->renderProblems();
  50. }
  51. }
  52. }
  53. private function renderProblems()
  54. {
  55. /** @var Uri $uri */
  56. $uri = $this->grav['uri'];
  57. /** @var \Twig_Environment $twig */
  58. $twig = $this->getTwig();
  59. $data = [
  60. 'problems' => $this->problems,
  61. 'base_url' => $baseUrlRelative = $uri->rootUrl(false),
  62. 'problems_url' => $baseUrlRelative . '/user/plugins/problems',
  63. ];
  64. echo $twig->render('problems.html.twig', $data);
  65. http_response_code(500);
  66. exit();
  67. }
  68. public function onAdminGenerateReports(Event $e)
  69. {
  70. $reports = $e['reports'];
  71. $this->checker = new ProblemChecker();
  72. // Check for problems
  73. $this->problemsFound();
  74. /** @var Uri $uri */
  75. $uri = $this->grav['uri'];
  76. /** @var \Twig_Environment $twig */
  77. $twig = $this->getTwig();
  78. $data = [
  79. 'problems' => $this->problems,
  80. 'base_url' => $baseUrlRelative = $uri->rootUrl(false),
  81. 'problems_url' => $baseUrlRelative . '/user/plugins/problems',
  82. ];
  83. $reports['Grav Potential Problems'] = $twig->render('reports/problems-report.html.twig', $data);
  84. $this->grav['assets']->addCss('plugins://problems/css/admin.css');
  85. $this->grav['assets']->addCss('plugins://problems/css/spectre-icons.css');
  86. }
  87. private function problemsFound()
  88. {
  89. if (is_null($this->checker)) {
  90. $this->checker = new ProblemChecker();
  91. }
  92. $status = $this->checker->check(__DIR__ . '/classes/Problems');
  93. $this->problems = $this->checker->getProblems();
  94. return $status;
  95. }
  96. private function getTwig()
  97. {
  98. $loader = new \Twig_Loader_Filesystem(__DIR__ . '/templates');
  99. $twig = new \Twig_Environment($loader, ['debug' => true]);
  100. $twig->addExtension(New \Twig_Extension_Debug());
  101. return $twig;
  102. }
  103. }