Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Page.php
Go to the documentation of this file.
1 <?php
27 #require_once 'Zend/Cache/Core.php';
28 
29 
37 {
84  protected $_specificOptions = array(
85  'http_conditional' => false,
86  'debug_header' => false,
87  'content_type_memorization' => false,
88  'memorize_headers' => array(),
89  'default_options' => array(
90  'cache_with_get_variables' => false,
91  'cache_with_post_variables' => false,
92  'cache_with_session_variables' => false,
93  'cache_with_files_variables' => false,
94  'cache_with_cookie_variables' => false,
95  'make_id_with_get_variables' => true,
96  'make_id_with_post_variables' => true,
97  'make_id_with_session_variables' => true,
98  'make_id_with_files_variables' => true,
99  'make_id_with_cookie_variables' => true,
100  'cache' => true,
101  'specific_lifetime' => false,
102  'tags' => array(),
103  'priority' => null
104  ),
105  'regexps' => array()
106  );
107 
113  protected $_activeOptions = array();
114 
120  protected $_cancel = false;
121 
130  public function __construct(array $options = array())
131  {
132  foreach ($options as $name => $value) {
133  $name = strtolower($name);
134  switch ($name) {
135  case 'regexps':
136  $this->_setRegexps($value);
137  break;
138  case 'default_options':
139  $this->_setDefaultOptions($value);
140  break;
141  case 'content_type_memorization':
143  break;
144  default:
145  $this->setOption($name, $value);
146  }
147  }
148  if (isset($this->_specificOptions['http_conditional'])) {
149  if ($this->_specificOptions['http_conditional']) {
150  Zend_Cache::throwException('http_conditional is not implemented for the moment !');
151  }
152  }
153  $this->setOption('automatic_serialization', true);
154  }
155 
163  protected function _setDefaultOptions($options)
164  {
165  if (!is_array($options)) {
166  Zend_Cache::throwException('default_options must be an array !');
167  }
168  foreach ($options as $key=>$value) {
169  if (!is_string($key)) {
170  Zend_Cache::throwException("invalid option [$key] !");
171  }
172  $key = strtolower($key);
173  if (isset($this->_specificOptions['default_options'][$key])) {
174  $this->_specificOptions['default_options'][$key] = $value;
175  }
176  }
177  }
178 
187  {
188  $found = null;
189  foreach ($this->_specificOptions['memorize_headers'] as $key => $value) {
190  if (strtolower($value) == 'content-type') {
191  $found = $key;
192  }
193  }
194  if ($value) {
195  if (!$found) {
196  $this->_specificOptions['memorize_headers'][] = 'Content-Type';
197  }
198  } else {
199  if ($found) {
200  unset($this->_specificOptions['memorize_headers'][$found]);
201  }
202  }
203  }
204 
212  protected function _setRegexps($regexps)
213  {
214  if (!is_array($regexps)) {
215  Zend_Cache::throwException('regexps option must be an array !');
216  }
217  foreach ($regexps as $regexp=>$conf) {
218  if (!is_array($conf)) {
219  Zend_Cache::throwException('regexps option must be an array of arrays !');
220  }
221  $validKeys = array_keys($this->_specificOptions['default_options']);
222  foreach ($conf as $key=>$value) {
223  if (!is_string($key)) {
224  Zend_Cache::throwException("unknown option [$key] !");
225  }
226  $key = strtolower($key);
227  if (!in_array($key, $validKeys)) {
228  unset($regexps[$regexp][$key]);
229  }
230  }
231  }
232  $this->setOption('regexps', $regexps);
233  }
234 
242  public function start($id = false, $doNotDie = false)
243  {
244  $this->_cancel = false;
245  $lastMatchingRegexp = null;
246  if (isset($_SERVER['REQUEST_URI'])) {
247  foreach ($this->_specificOptions['regexps'] as $regexp => $conf) {
248  if (preg_match("`$regexp`", $_SERVER['REQUEST_URI'])) {
249  $lastMatchingRegexp = $regexp;
250  }
251  }
252  }
253  $this->_activeOptions = $this->_specificOptions['default_options'];
254  if ($lastMatchingRegexp !== null) {
255  $conf = $this->_specificOptions['regexps'][$lastMatchingRegexp];
256  foreach ($conf as $key=>$value) {
257  $this->_activeOptions[$key] = $value;
258  }
259  }
260  if (!($this->_activeOptions['cache'])) {
261  return false;
262  }
263  if (!$id) {
264  $id = $this->_makeId();
265  if (!$id) {
266  return false;
267  }
268  }
269  $array = $this->load($id);
270  if ($array !== false) {
271  $data = $array['data'];
272  $headers = $array['headers'];
273  if (!headers_sent()) {
274  foreach ($headers as $key=>$headerCouple) {
275  $name = $headerCouple[0];
276  $value = $headerCouple[1];
277  header("$name: $value");
278  }
279  }
280  if ($this->_specificOptions['debug_header']) {
281  echo 'DEBUG HEADER : This is a cached page !';
282  }
283  echo $data;
284  if ($doNotDie) {
285  return true;
286  }
287  die();
288  }
289  ob_start(array($this, '_flush'));
290  ob_implicit_flush(false);
291  return false;
292  }
293 
297  public function cancel()
298  {
299  $this->_cancel = true;
300  }
301 
309  public function _flush($data)
310  {
311  if ($this->_cancel) {
312  return $data;
313  }
314  $contentType = null;
315  $storedHeaders = array();
316  $headersList = headers_list();
317  foreach($this->_specificOptions['memorize_headers'] as $key=>$headerName) {
318  foreach ($headersList as $headerSent) {
319  $tmp = explode(':', $headerSent);
320  $headerSentName = trim(array_shift($tmp));
321  if (strtolower($headerName) == strtolower($headerSentName)) {
322  $headerSentValue = trim(implode(':', $tmp));
323  $storedHeaders[] = array($headerSentName, $headerSentValue);
324  }
325  }
326  }
327  $array = array(
328  'data' => $data,
329  'headers' => $storedHeaders
330  );
331  $this->save($array, null, $this->_activeOptions['tags'], $this->_activeOptions['specific_lifetime'], $this->_activeOptions['priority']);
332  return $data;
333  }
334 
340  protected function _makeId()
341  {
342  $tmp = $_SERVER['REQUEST_URI'];
343  $array = explode('?', $tmp, 2);
344  $tmp = $array[0];
345  foreach (array('Get', 'Post', 'Session', 'Files', 'Cookie') as $arrayName) {
346  $tmp2 = $this->_makePartialId($arrayName, $this->_activeOptions['cache_with_' . strtolower($arrayName) . '_variables'], $this->_activeOptions['make_id_with_' . strtolower($arrayName) . '_variables']);
347  if ($tmp2===false) {
348  return false;
349  }
350  $tmp = $tmp . $tmp2;
351  }
352  return md5($tmp);
353  }
354 
363  protected function _makePartialId($arrayName, $bool1, $bool2)
364  {
365  switch ($arrayName) {
366  case 'Get':
367  $var = $_GET;
368  break;
369  case 'Post':
370  $var = $_POST;
371  break;
372  case 'Session':
373  if (isset($_SESSION)) {
374  $var = $_SESSION;
375  } else {
376  $var = null;
377  }
378  break;
379  case 'Cookie':
380  if (isset($_COOKIE)) {
381  $var = $_COOKIE;
382  } else {
383  $var = null;
384  }
385  break;
386  case 'Files':
387  $var = $_FILES;
388  break;
389  default:
390  return false;
391  }
392  if ($bool1) {
393  if ($bool2) {
394  return serialize($var);
395  }
396  return '';
397  }
398  if (is_array($var) && count($var) > 0) {
399  return false;
400  }
401  return '';
402  }
403 
404 }
start($id=false, $doNotDie=false)
Definition: Page.php:242
$id
Definition: fieldset.phtml:14
save($data, $id=null, $tags=array(), $specificLifetime=false, $priority=8)
Definition: Core.php:348
_setContentTypeMemorization($value)
Definition: Page.php:186
setOption($name, $value)
Definition: Core.php:211
$value
Definition: gender.phtml:16
__construct(array $options=array())
Definition: Page.php:130
_makePartialId($arrayName, $bool1, $bool2)
Definition: Page.php:363
_setRegexps($regexps)
Definition: Page.php:212
static throwException($msg, Exception $e=null)
Definition: Cache.php:205
load($id, $doNotTestCacheValidity=false, $doNotUnserialize=false)
Definition: Core.php:296
_setDefaultOptions($options)
Definition: Page.php:163
if(!isset($_GET['name'])) $name
Definition: log.php:14