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