1
|
|
|
<?php |
2
|
|
|
namespace Redaxscript\Modules\PageCache; |
3
|
|
|
|
4
|
|
|
use Redaxscript\Filesystem; |
5
|
|
|
use Redaxscript\Module; |
6
|
|
|
use function chmod; |
7
|
|
|
use function is_dir; |
8
|
|
|
use function mkdir; |
9
|
|
|
use function preg_replace; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* high performance caching for pages |
13
|
|
|
* |
14
|
|
|
* @since 3.0.0 |
15
|
|
|
* |
16
|
|
|
* @package Redaxscript |
17
|
|
|
* @category Modules |
18
|
|
|
* @author Henry Ruhs |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
class PageCache extends Module\Notification |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* array of the module |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
protected static $_moduleArray = |
30
|
|
|
[ |
31
|
|
|
'name' => 'Page Cache', |
32
|
|
|
'alias' => 'PageCache', |
33
|
|
|
'author' => 'Redaxmedia', |
34
|
|
|
'description' => 'High performance caching for pages', |
35
|
|
|
'version' => '4.0.0' |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* array of the option |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
protected $_optionArray = |
45
|
|
|
[ |
46
|
|
|
'directory' => |
47
|
|
|
[ |
48
|
|
|
'scripts' => 'cache/scripts', |
49
|
|
|
'styles' => 'cache/styles', |
50
|
|
|
'pages' => 'cache/pages' |
51
|
|
|
], |
52
|
|
|
'extension' => 'phtml', |
53
|
|
|
'lifetime' => 300, |
54
|
|
|
'tokenPlaceholder' => '%TOKEN%' |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* adminNotification |
59
|
|
|
* |
60
|
|
|
* @since 3.0.0 |
61
|
|
|
* |
62
|
|
|
* @return array|null |
63
|
|
|
*/ |
64
|
|
|
|
65
|
|
|
public function adminNotification() : ?array |
66
|
|
|
{ |
67
|
|
|
if (!mkdir($pagesDirectory = $this->_optionArray['directory']['pages']) && !is_dir($pagesDirectory)) |
68
|
|
|
{ |
69
|
|
|
$this->setNotification('error', $this->_language->get('directory_not_found') . $this->_language->get('colon') . ' ' . $this->_optionArray['directory']['pages'] . $this->_language->get('point')); |
70
|
|
|
} |
71
|
|
|
else if (!chmod($this->_optionArray['directory']['pages'], 0777)) |
72
|
|
|
{ |
73
|
|
|
$this->setNotification('error', $this->_language->get('directory_permission_grant') . $this->_language->get('colon') . ' ' . $this->_optionArray['directory']['pages'] . $this->_language->get('point')); |
74
|
|
|
} |
75
|
|
|
return $this->getNotification(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* templateReplace |
80
|
|
|
* |
81
|
|
|
* @since 4.0.0 |
82
|
|
|
* |
83
|
|
|
* @return string|null |
84
|
|
|
*/ |
85
|
|
|
|
86
|
|
|
public function templateReplace() : ?string |
87
|
|
|
{ |
88
|
|
|
$fileSystem = new Filesystem\Filesystem(); |
89
|
|
|
$stylesFileSystem = $fileSystem->copy()->init($this->_optionArray['directory']['styles']); |
90
|
|
|
$scriptsFileSystem = $fileSystem->copy()->init($this->_optionArray['directory']['scripts']); |
91
|
|
|
|
92
|
|
|
/* prevent as needed */ |
93
|
|
|
|
94
|
|
|
if ($stylesFileSystem->countIterator() === 0 || $scriptsFileSystem->countIterator() === 0 || $this->_request->getPost() || $this->_registry->get('noPageCache')) |
95
|
|
|
{ |
96
|
|
|
return null; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/* cache as needed */ |
100
|
|
|
|
101
|
|
|
$cacheFilesystem = new Filesystem\Cache(); |
102
|
|
|
$cacheFilesystem->init($this->_optionArray['directory']['pages'], $this->_optionArray['extension']); |
103
|
|
|
$bundle = $this->_registry->get('root') . $this->_registry->get('fullRoute') . '/' . $this->_registry->get('template') . '/' . $this->_registry->get('language'); |
104
|
|
|
$token = $this->_registry->get('token'); |
105
|
|
|
|
106
|
|
|
/* load from cache */ |
107
|
|
|
|
108
|
|
|
if ($cacheFilesystem->validate($bundle, $this->_optionArray['lifetime'])) |
109
|
|
|
{ |
110
|
|
|
$raw = $cacheFilesystem->retrieve($bundle); |
111
|
|
|
$content = preg_replace('/' . $this->_optionArray['tokenPlaceholder'] . '/', $token, $raw); |
112
|
|
|
return $content; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/* store to cache */ |
116
|
|
|
|
117
|
|
|
$rawFilesystem = new Filesystem\File(); |
118
|
|
|
$rawFilesystem->init('templates' . DIRECTORY_SEPARATOR . $this->_registry->get('template')); |
119
|
|
|
$raw = $rawFilesystem->renderFile('index.phtml'); |
120
|
|
|
$content = preg_replace('/' . $token . '/', $this->_optionArray['tokenPlaceholder'], $raw); |
121
|
|
|
$cacheFilesystem->store($bundle, $content); |
122
|
|
|
return $raw; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|