Skouter mortgage estimates. Web application with view written in PHP and Vue, but controller and models in Go.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

problems.php 4.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. use Twig\Environment;
  9. use Twig\Extension\DebugExtension;
  10. use Twig\Loader\FilesystemLoader;
  11. /**
  12. * Class ProblemsPlugin
  13. * @package Grav\Plugin
  14. */
  15. class ProblemsPlugin extends Plugin
  16. {
  17. /** @var ProblemChecker|null */
  18. protected $checker;
  19. /** @var array */
  20. protected $problems = [];
  21. /**
  22. * @return array
  23. */
  24. public static function getSubscribedEvents()
  25. {
  26. return [
  27. 'onPluginsInitialized' => [
  28. ['autoload', 100002],
  29. ['onPluginsInitialized', 100001]
  30. ],
  31. 'onAdminGenerateReports' => ['onAdminGenerateReports', 0],
  32. 'onAdminCompilePresetSCSS' => ['onAdminCompilePresetSCSS', 0]
  33. ];
  34. }
  35. /**
  36. * [onPluginsInitialized:100000] Composer autoload.
  37. *
  38. * @return ClassLoader
  39. */
  40. public function autoload(): ClassLoader
  41. {
  42. return require __DIR__ . '/vendor/autoload.php';
  43. }
  44. /**
  45. * @return void
  46. */
  47. public function onFatalException(): void
  48. {
  49. if (\defined('GRAV_CLI') || $this->isAdmin()) {
  50. return;
  51. }
  52. // Run through potential issues
  53. if ($this->problemsFound()) {
  54. $this->renderProblems();
  55. }
  56. }
  57. /**
  58. * Add Flex-Object's preset.scss to the Admin Preset SCSS compile process
  59. *
  60. * @param Event $event
  61. */
  62. public function onAdminCompilePresetSCSS(Event $event): void
  63. {
  64. $event['scss']->add($this->grav['locator']->findResource('plugins://problems/scss/_preset.scss'));
  65. }
  66. /**
  67. * @return void
  68. */
  69. public function onPluginsInitialized(): void
  70. {
  71. if (\defined('GRAV_CLI') || $this->isAdmin()) {
  72. return;
  73. }
  74. $this->enable([
  75. 'onFatalException' => ['onFatalException', 0],
  76. ]);
  77. $this->checker = new ProblemChecker();
  78. if (!$this->checker->statusFileExists()) {
  79. // If no issues remain, save a state file in the cache
  80. if (!$this->problemsFound()) {
  81. // delete any existing validated files
  82. /** @var \SplFileInfo $fileInfo */
  83. foreach (new \GlobIterator(CACHE_DIR . ProblemChecker::PROBLEMS_PREFIX . '*') as $fileInfo) {
  84. @unlink($fileInfo->getPathname());
  85. }
  86. // create a file in the cache dir so it only runs on cache changes
  87. $this->checker->storeStatusFile();
  88. } else {
  89. $this->renderProblems();
  90. }
  91. }
  92. }
  93. /**
  94. * @return never-return
  95. */
  96. private function renderProblems(): void
  97. {
  98. /** @var Uri $uri */
  99. $uri = $this->grav['uri'];
  100. /** @var Environment $twig */
  101. $twig = $this->getTwig();
  102. $data = [
  103. 'problems' => $this->problems,
  104. 'base_url' => $baseUrlRelative = $uri->rootUrl(false),
  105. 'problems_url' => $baseUrlRelative . '/user/plugins/problems',
  106. ];
  107. echo $twig->render('problems.html.twig', $data);
  108. http_response_code(500);
  109. exit();
  110. }
  111. /**
  112. * @param Event $e
  113. * @return void
  114. */
  115. public function onAdminGenerateReports(Event $e): void
  116. {
  117. $reports = $e['reports'];
  118. $this->checker = new ProblemChecker();
  119. // Check for problems
  120. $this->problemsFound();
  121. /** @var Uri $uri */
  122. $uri = $this->grav['uri'];
  123. /** @var Environment $twig */
  124. $twig = $this->getTwig();
  125. $data = [
  126. 'problems' => $this->problems,
  127. 'base_url' => $baseUrlRelative = $uri->rootUrl(false),
  128. 'problems_url' => $baseUrlRelative . '/user/plugins/problems',
  129. ];
  130. $reports['Grav Potential Problems'] = $twig->render('reports/problems-report.html.twig', $data);
  131. $this->grav['assets']->addCss('plugins://problems/css/admin.css');
  132. $this->grav['assets']->addCss('plugins://problems/css/spectre-icons.css');
  133. }
  134. /**
  135. * @return bool
  136. */
  137. private function problemsFound(): bool
  138. {
  139. if (null === $this->checker) {
  140. $this->checker = new ProblemChecker();
  141. }
  142. $status = $this->checker->check(__DIR__ . '/classes/Problems');
  143. $this->problems = $this->checker->getProblems();
  144. return $status;
  145. }
  146. /**
  147. * @return Environment
  148. */
  149. private function getTwig(): Environment
  150. {
  151. $loader = new FilesystemLoader(__DIR__ . '/templates');
  152. $twig = new Environment($loader, ['debug' => true]);
  153. $twig->addExtension(New DebugExtension());
  154. return $twig;
  155. }
  156. }