-
Notifications
You must be signed in to change notification settings - Fork 8
/
xtremecache.php
233 lines (203 loc) · 6.95 KB
/
xtremecache.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/**
* Full page cache for Prestashop
*
* @author Simone Salerno
*/
class Xtremecache extends Module
{
/** @var int cache expire time in seconds **/
const TTL = 3600*24*7;
/** @var boolean wether cache should be cleaned on catalog updates **/
const REACTIVE = true;
/** @var boolean wether a custom header should be sent **/
const CUSTOMER_HEADER = true;
/** @var array of int Don"t cache certain languages **/
const EXCLUDED_LANGS = [];
/** @var array of int Don"t cache certain countries **/
const EXCLUDE_COUNTRIES = [];
/** @var array of int Don"t cache certain currencies **/
const EXCLUDE_CURRENCIES = [];
/** @var array of int Don"t cache certain shops **/
const EXCLUDE_SHOPS = [];
/** @var CacheCore **/
private $cache;
public function __construct()
{
$this->name = "xtremecache";
$this->tab = "front_office_features";
$this->version = "1.0.0";
$this->author = "Simone Salerno";
$this->need_instance = 0;
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l("Xtreme cache");
$this->description = $this->l("Full page cache for Prestashop.");
$this->ps_versions_compliancy = array("min" => "1.6", "max" => "1.6.99.99");
$this->cache = Cache::getInstance();
}
/**
*
* @return boolean
*/
public function install()
{
$this->createHooks();
return parent::install() &&
$this->registerHook("actionCategoryAdd") &&
$this->registerHook("actionCategoryUpdate") &&
$this->registerHook("actionCategoryDelete") &&
$this->registerHook("actionProductAdd") &&
$this->registerHook("actionProductUpdate") &&
$this->registerHook("actionProductDelete") &&
$this->registerHook("actionProductSave") &&
$this->registerHook("displayHeader") &&
$this->registerHook("actionResponse");
}
/**
*
* @return boolean
*/
public function uninstall()
{
return parent::uninstall() &&
$this->unregisterHook("actionCategoryAdd") &&
$this->unregisterHook("actionCategoryUpdate") &&
$this->unregisterHook("actionCategoryDelete") &&
$this->unregisterHook("actionProductAdd") &&
$this->unregisterHook("actionProductUpdate") &&
$this->unregisterHook("actionProductDelete") &&
$this->unregisterHook("actionProductSave") &&
$this->unregisterHook("displayHeader") &&
$this->unregisterHook("actionResponse");
}
/**
* If a cached page exists for the current request
* return it and abort
*/
public function hookDisplayHeader()
{
if ($this->isActive() && ($html = $this->load())) {
ob_clean();
if (static::CUSTOMER_HEADER) {
header("X-Xtremecached: True");
}
die($html);
}
}
/**
* Store response in cache
*
* @param array $params
*/
public function hookActionResponse(array $params)
{
if ($this->isActive()) {
$this->store($params["html"]);
}
}
/**
* Handle reactive hooks
*
* @param string $name
* @param array $arguments
* @return mixed
*/
public function __call($name, $arguments)
{
if (static::REACTIVE && (0 === strpos(strtolower($name), 'hookaction'))) {
$this->cache->flush();
}
else {
return parent::__call($name, $arguments);
}
}
/**
* Test if current page is to be cached
*
* @return boolean
*/
private function isActive()
{
$cart = $this->context->cart;
return !_PS_DEBUG_PROFILING_ // skip when profiling
&& !_PS_MODE_DEV_ // skip when debugging
&& is_a($this->context->controller, FrontControllerCore::class) // skip on back-end
&& Configuration::get("PS_SHOP_ENABLE") // skip on catalogue mode
&& !Tools::getValue("ajax") // skip on AJAX requests
&& filter_input(INPUT_SERVER, "REQUEST_METHOD") === "GET" // skip on POST requests
&& $cart->id_customer < 1 // skip if user is logged in
&& $cart->nbProducts() < 1 // skip if cart is not empty
&& $this->isNotExcluded($cart->id_lang, static::EXCLUDED_LANGS)
&& $this->isNotExcluded($cart->id_shop, static::EXCLUDE_SHOPS)
&& $this->isNotExcluded($cart->id_currency, static::EXCLUDE_CURRENCIES)
&& $this->isNotExcluded($this->context->country->id, static::EXCLUDE_COUNTRIES);
}
/**
* Get cached response
*
* @return string
*/
private function load()
{
return $this->cache->get($this->key());
}
/**
* Store response
*
* @param string $html
* @return mixed
*/
private function store($html)
{
$key = $this->key();
$reponse = sprintf("<!-- cached on %s -->\n%s", date("Y-m-d H:i:s"), $html);
return $this->cache->set($key, $reponse, static::TTL);
}
/**
* Get unique key for current request
*
* @return string
*/
private function key()
{
return implode("-", [
filter_input(INPUT_SERVER, "REQUEST_URI"),
(int) $this->context->cart->id_currency,
(int) $this->context->cart->id_lang,
(int) $this->context->cart->id_shop,
(int) $this->context->country->id,
(int) $this->context->getDevice()
]);
}
/**
* Check if given id is excluded from cache
*
* @param int $id
* @param array $pool
* @return bool
*/
private function isNotExcluded($id, array $pool)
{
return array_search($id, $pool) === false;
}
/**
* Create needed hooks in DB
*
*/
private function createHooks()
{
$id = Hook::getIdByName("actionResponse");
if (!$id) {
$hook = new Hook();
$hook->hydrate([
"name" => "actionResponse",
"title" => "actionResponse",
"description" => "Run before sending response to the browser",
"position" => 1,
"live_edit" => 0
]);
$hook->save();
}
}
}