-
Notifications
You must be signed in to change notification settings - Fork 2
/
precache.php
85 lines (69 loc) · 2.28 KB
/
precache.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
<?php
namespace Grav\Plugin;
use Grav\Common\Plugin;
use Grav\Common\Utils;
class PreCachePlugin extends Plugin
{
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0]
];
}
/**
* Initialize configuration
*/
public function onPluginsInitialized()
{
$this->config = $this->grav['config'];
// don't continue if this is admin and plugin is disabled for admin
if (!$this->config->get('plugins.precache.enabled_admin') && $this->isAdmin()) {
return;
}
// don't continue if Grav cache is not enabled
if (!$this->config->get('system.cache.enabled')) {
return false;
}
$this->enable([
'onShutdown' => ['onShutdown', 0]
]);
}
public function onShutdown()
{
/** @var Cache $cache */
$cache = $this->grav['cache'];
$cache_id = md5('preacache'.$cache->getKey());
$precached = $cache->fetch($cache_id);
if (!$precached) {
$log_pages = $this->config->get('plugins.precache.log_pages', true);
// check if this function is available, if so use it to stop any timeouts
try {
if (!Utils::isFunctionDisabled('set_time_limit') && !ini_get('safe_mode') && function_exists('set_time_limit')) {
set_time_limit(0);
}
} catch (\Exception $e) {}
/** @var Pages $pages */
$pages = $this->grav['pages'];
$routes = $pages->routes();
foreach ($routes as $route => $path) {
// Log our progress
if ($log_pages) {
$this->grav['log']->addInfo('precache: '.$route);
}
try {
$page = $pages->get($path);
unset($this->grav['page']);
$this->grav['page'] = $page;
// call the content to load/cache it
$page->content();
} catch (\Exception $e) {
// do nothing on error
}
}
$cache->save($cache_id, true);
}
}
}