problems.php 3.9 KB

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