error.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace Grav\Plugin;
  3. use Composer\Autoload\ClassLoader;
  4. use Grav\Common\Page\Interfaces\PageInterface;
  5. use Grav\Common\Plugin;
  6. use Grav\Common\Page\Page;
  7. use Grav\Common\Page\Pages;
  8. use Grav\Common\Page\Types;
  9. use RocketTheme\Toolbox\Event\Event;
  10. class ErrorPlugin extends Plugin
  11. {
  12. /**
  13. * @return array
  14. */
  15. public static function getSubscribedEvents(): array
  16. {
  17. return [
  18. 'onCliInitialize' => [
  19. ['autoload', 100000],
  20. ],
  21. 'onPageNotFound' => [
  22. ['onPageNotFound', 0]
  23. ],
  24. 'onGetPageTemplates' => [
  25. ['onGetPageTemplates', 0]
  26. ],
  27. 'onTwigTemplatePaths' => [
  28. ['onTwigTemplatePaths', -10]
  29. ],
  30. 'onDisplayErrorPage.404'=> [
  31. ['onDisplayErrorPage404', -1]
  32. ]
  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. * @param Event $event
  46. */
  47. public function onDisplayErrorPage404(Event $event): void
  48. {
  49. if ($this->isAdmin()) {
  50. return;
  51. }
  52. $event['page'] = $this->getErrorPage();
  53. $event->stopPropagation();
  54. }
  55. /**
  56. * Display error page if no page was found for the current route.
  57. *
  58. * @param Event $event
  59. */
  60. public function onPageNotFound(Event $event): void
  61. {
  62. $event->page = $this->getErrorPage();
  63. $event->stopPropagation();
  64. }
  65. /**
  66. * @return PageInterface
  67. * @throws \Exception
  68. */
  69. public function getErrorPage(): PageInterface
  70. {
  71. /** @var Pages $pages */
  72. $pages = $this->grav['pages'];
  73. // Try to load user error page.
  74. $page = $pages->dispatch($this->config->get('plugins.error.routes.404', '/error'), true);
  75. if (!$page) {
  76. // If none provided use built in error page.
  77. $page = new Page;
  78. $page->init(new \SplFileInfo(__DIR__ . '/pages/error.md'));
  79. $page->title($this->grav['language']->translate('PLUGIN_ERROR.ERROR') . ' ' . $page->header()->http_response_code);
  80. }
  81. // Login page may not have the correct Cache-Control header set, force no-store for the proxies.
  82. $cacheControl = $page->cacheControl();
  83. if (!$cacheControl) {
  84. $page->cacheControl('private, no-cache, must-revalidate');
  85. }
  86. return $page;
  87. }
  88. /**
  89. * Add page template types.
  90. */
  91. public function onGetPageTemplates(Event $event): void
  92. {
  93. /** @var Types $types */
  94. $types = $event->types;
  95. $types->register('error');
  96. }
  97. /**
  98. * Add current directory to twig lookup paths.
  99. */
  100. public function onTwigTemplatePaths(): void
  101. {
  102. $this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
  103. }
  104. }