forked from getgrav/grav-plugin-advanced-pagecache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
advanced-pagecache.php
62 lines (49 loc) · 1.54 KB
/
advanced-pagecache.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
<?php
namespace Grav\Plugin;
use \Grav\Common\Plugin;
use \Grav\Common\Uri;
class AdvancedPageCachePlugin extends Plugin
{
/** @var Config $config */
protected $config;
protected $path;
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0],
'onOutputGenerated' => ['onOutputGenerated', 0]
];
}
/**
* Initialize configuration
*/
public function onPluginsInitialized()
{
$config = $this->grav['config']->get('plugins.advanced-pagecache');
/** @var Uri $uri */
$uri = $this->grav['uri'];
$params = $uri->params(null, true);
$query = $uri->query(null, true);
$this->path = $this->grav['uri']->path();
// do not run in these scenarios
if ($this->isAdmin() ||
!$config['enabled_with_params'] && !empty($params) ||
!$config['enabled_with_query'] && !empty($query) ||
$config['whitelist'] && is_array($config['whitelist']) && !in_array($this->path, $config['whitelist']) ||
$config['blacklist'] && is_array($config['blacklist']) && in_array($this->path, $config['blacklist'])) {
return;
}
$pagecache = $this->grav['cache']->fetch($this->path);
if ($pagecache) {
echo $pagecache;
exit;
}
}
public function onOutputGenerated()
{
$this->grav['cache']->save($this->path, $this->grav->output);
}
}