-
Notifications
You must be signed in to change notification settings - Fork 8
/
problems.php
185 lines (156 loc) · 4.74 KB
/
problems.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
namespace Grav\Plugin;
use Composer\Autoload\ClassLoader;
use Grav\Common\Plugin;
use Grav\Common\Uri;
use Grav\Plugin\Problems\Base\ProblemChecker;
use RocketTheme\Toolbox\Event\Event;
use Twig\Environment;
use Twig\Extension\DebugExtension;
use Twig\Loader\FilesystemLoader;
/**
* Class ProblemsPlugin
* @package Grav\Plugin
*/
class ProblemsPlugin extends Plugin
{
/** @var ProblemChecker|null */
protected $checker;
/** @var array */
protected $problems = [];
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => [
['autoload', 100002],
['onPluginsInitialized', 100001]
],
'onAdminGenerateReports' => ['onAdminGenerateReports', 0],
'onAdminCompilePresetSCSS' => ['onAdminCompilePresetSCSS', 0]
];
}
/**
* [onPluginsInitialized:100000] Composer autoload.
*
* @return ClassLoader
*/
public function autoload(): ClassLoader
{
return require __DIR__ . '/vendor/autoload.php';
}
/**
* @return void
*/
public function onFatalException(): void
{
if (\defined('GRAV_CLI') || $this->isAdmin()) {
return;
}
// Run through potential issues
if ($this->problemsFound()) {
$this->renderProblems();
}
}
/**
* Add Flex-Object's preset.scss to the Admin Preset SCSS compile process
*
* @param Event $event
*/
public function onAdminCompilePresetSCSS(Event $event): void
{
$event['scss']->add($this->grav['locator']->findResource('plugins://problems/scss/_preset.scss'));
}
/**
* @return void
*/
public function onPluginsInitialized(): void
{
if (\defined('GRAV_CLI') || $this->isAdmin()) {
return;
}
$this->enable([
'onFatalException' => ['onFatalException', 0],
]);
$this->checker = new ProblemChecker();
if (!$this->checker->statusFileExists()) {
// If no issues remain, save a state file in the cache
if (!$this->problemsFound()) {
// delete any existing validated files
/** @var \SplFileInfo $fileInfo */
foreach (new \GlobIterator(CACHE_DIR . ProblemChecker::PROBLEMS_PREFIX . '*') as $fileInfo) {
@unlink($fileInfo->getPathname());
}
// create a file in the cache dir so it only runs on cache changes
$this->checker->storeStatusFile();
} else {
$this->renderProblems();
}
}
}
/**
* @return never-return
*/
private function renderProblems(): void
{
/** @var Uri $uri */
$uri = $this->grav['uri'];
/** @var Environment $twig */
$twig = $this->getTwig();
$data = [
'problems' => $this->problems,
'base_url' => $baseUrlRelative = $uri->rootUrl(false),
'problems_url' => $baseUrlRelative . '/user/plugins/problems',
];
echo $twig->render('problems.html.twig', $data);
http_response_code(500);
exit();
}
/**
* @param Event $e
* @return void
*/
public function onAdminGenerateReports(Event $e): void
{
$reports = $e['reports'];
$this->checker = new ProblemChecker();
// Check for problems
$this->problemsFound();
/** @var Uri $uri */
$uri = $this->grav['uri'];
/** @var Environment $twig */
$twig = $this->getTwig();
$data = [
'problems' => $this->problems,
'base_url' => $baseUrlRelative = $uri->rootUrl(false),
'problems_url' => $baseUrlRelative . '/user/plugins/problems',
];
$reports['Grav Potential Problems'] = $twig->render('reports/problems-report.html.twig', $data);
$this->grav['assets']->addCss('plugins://problems/css/admin.css');
$this->grav['assets']->addCss('plugins://problems/css/spectre-icons.css');
}
/**
* @return bool
*/
private function problemsFound(): bool
{
if (null === $this->checker) {
$this->checker = new ProblemChecker();
}
$status = $this->checker->check(__DIR__ . '/classes/Problems');
$this->problems = $this->checker->getProblems();
return $status;
}
/**
* @return Environment
*/
private function getTwig(): Environment
{
$loader = new FilesystemLoader(__DIR__ . '/templates');
$twig = new Environment($loader, ['debug' => true]);
$twig->addExtension(New DebugExtension());
return $twig;
}
}