Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Static Protected Member Functions | Protected Attributes
Zend_Cache_Backend_Static Class Reference
Inheritance diagram for Zend_Cache_Backend_Static:
Zend_Cache_Backend Zend_Cache_Backend_Interface

Public Member Functions

 setOption ($name, $value)
 
 getOption ($name)
 
 load ($id, $doNotTestCacheValidity=false)
 
 test ($id)
 
 save ($data, $id, $tags=array(), $specificLifetime=false)
 
 remove ($id)
 
 removeRecursively ($id)
 
 clean ($mode=Zend_Cache::CLEANING_MODE_ALL, $tags=array())
 
 setInnerCache (Zend_Cache_Core $cache)
 
 getInnerCache ()
 
- Public Member Functions inherited from Zend_Cache_Backend
 __construct (array $options=array())
 
 setDirectives ($directives)
 
 setOption ($name, $value)
 
 getOption ($name)
 
 getLifetime ($specificLifetime)
 
 isAutomaticCleaningAvailable ()
 
 getTmpDir ()
 
- Public Member Functions inherited from Zend_Cache_Backend_Interface
 setDirectives ($directives)
 

Data Fields

const INNER_CACHE_NAME = 'zend_cache_backend_static_tagcache'
 

Protected Member Functions

 _createDirectoriesFor ($path)
 
 _isSerialized ($data)
 
 _verifyPath ($path)
 
 _detectId ()
 
 _octdec ($val)
 
 _decodeId ($id)
 
- Protected Member Functions inherited from Zend_Cache_Backend
 _isGoodTmpDir ($dir)
 
 _loggerSanity ()
 
 _log ($message, $priority=4)
 

Static Protected Member Functions

static _validateIdOrTag ($string)
 

Protected Attributes

 $_options
 
 $_tagCache = null
 
 $_tagged = null
 
- Protected Attributes inherited from Zend_Cache_Backend
 $_directives
 
 $_options = array()
 

Detailed Description

Definition at line 39 of file Static.php.

Member Function Documentation

◆ _createDirectoriesFor()

_createDirectoriesFor (   $path)
protected

Recursively create the directories needed to write the static file

Definition at line 276 of file Static.php.

277  {
278  if (!is_dir($path)) {
279  $oldUmask = umask(0);
280  if ( !@mkdir($path, $this->_octdec($this->_options['cache_directory_perm']), true)) {
281  $lastErr = error_get_last();
282  umask($oldUmask);
283  Zend_Cache::throwException("Can't create directory: {$lastErr['message']}");
284  }
285  umask($oldUmask);
286  }
287  }
static throwException($msg, Exception $e=null)
Definition: Cache.php:205
mkdir($pathname, $mode=0777, $recursive=false, $context=null)
Definition: ioMock.php:25

◆ _decodeId()

_decodeId (   $id)
protected

Decode a request URI from the provided ID

Parameters
string$id
Returns
string

Definition at line 575 of file Static.php.

576  {
577  return pack('H*', $id);
578  }
$id
Definition: fieldset.phtml:14

◆ _detectId()

_detectId ( )
protected

Determine the page to save from the request

Returns
string

Definition at line 518 of file Static.php.

519  {
520  return $_SERVER['REQUEST_URI'];
521  }

◆ _isSerialized()

_isSerialized (   $data)
protected

Detect serialization of data (cannot predict since this is the only way to obey the interface yet pass in another parameter).

In future, ZF 2.0, check if we can just avoid the interface restraints.

This format is the only valid one possible for the class, so it's simple to just run a regular expression for the starting serialized format.

Definition at line 298 of file Static.php.

299  {
300  return preg_match("/a:2:\{i:0;s:\d+:\"/", $data);
301  }

◆ _octdec()

_octdec (   $val)
protected

Detect an octal string and return its octal value for file permission ops otherwise return the non-string (assumed octal or decimal int already)

Parameters
string$valThe potential octal in need of conversion
Returns
int

Definition at line 561 of file Static.php.

562  {
563  if (is_string($val) && decoct(octdec($val)) == $val) {
564  return octdec($val);
565  }
566  return $val;
567  }

◆ _validateIdOrTag()

static _validateIdOrTag (   $string)
staticprotected

Validate a cache id or a tag (security, reliable filenames, reserved prefixes...)

Throw an exception if a problem is found

Parameters
string$stringCache id or tag
Exceptions
Zend_Cache_Exception
Returns
void
Deprecated:
Not usable until perhaps ZF 2.0

Definition at line 533 of file Static.php.

534  {
535  if (!is_string($string)) {
536  Zend_Cache::throwException('Invalid id or tag : must be a string');
537  }
538 
539  // Internal only checked in Frontend - not here!
540  if (substr($string, 0, 9) == 'internal-') {
541  return;
542  }
543 
544  // Validation assumes no query string, fragments or scheme included - only the path
545  if (!preg_match(
546  '/^(?:\/(?:(?:%[[:xdigit:]]{2}|[A-Za-z0-9-_.!~*\'()\[\]:@&=+$,;])*)?)+$/',
547  $string
548  )
549  ) {
550  Zend_Cache::throwException("Invalid id or tag '$string' : must be a valid URL path");
551  }
552  }
static throwException($msg, Exception $e=null)
Definition: Cache.php:205

◆ _verifyPath()

_verifyPath (   $path)
protected

Verify path exists and is non-empty

Parameters
string$path
Returns
bool

Definition at line 506 of file Static.php.

507  {
508  $path = realpath($path);
509  $base = realpath($this->_options['public_dir']);
510  return strncmp($path, $base, strlen($base)) !== 0;
511  }

◆ clean()

clean (   $mode = Zend_Cache::CLEANING_MODE_ALL,
  $tags = array() 
)

Clean some cache records

Available modes are : Zend_Cache::CLEANING_MODE_ALL (default) => remove all cache entries ($tags is not used) Zend_Cache::CLEANING_MODE_OLD => remove too old cache entries ($tags is not used) Zend_Cache::CLEANING_MODE_MATCHING_TAG => remove cache entries matching all given tags ($tags can be an array of strings or a single string) Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => remove cache entries not {matching one of the given tags} ($tags can be an array of strings or a single string) Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG => remove cache entries matching any given tags ($tags can be an array of strings or a single string)

Parameters
string$modeClean mode
array$tagsArray of tags
Returns
boolean true if no problem
Exceptions
Zend_Exception

Implements Zend_Cache_Backend_Interface.

Definition at line 398 of file Static.php.

399  {
400  $result = false;
401  switch ($mode) {
404  if (empty($tags)) {
405  throw new Zend_Exception('Cannot use tag matching modes as no tags were defined');
406  }
407  if ($this->_tagged === null && $tagged = $this->getInnerCache()->load(self::INNER_CACHE_NAME)) {
408  $this->_tagged = $tagged;
409  } elseif (!$this->_tagged) {
410  return true;
411  }
412  foreach ($tags as $tag) {
413  $urls = array_keys($this->_tagged);
414  foreach ($urls as $url) {
415  if (isset($this->_tagged[$url]['tags']) && in_array($tag, $this->_tagged[$url]['tags'])) {
416  $this->remove($url);
417  unset($this->_tagged[$url]);
418  }
419  }
420  }
421  $this->getInnerCache()->save($this->_tagged, self::INNER_CACHE_NAME);
422  $result = true;
423  break;
425  if ($this->_tagged === null) {
426  $tagged = $this->getInnerCache()->load(self::INNER_CACHE_NAME);
427  $this->_tagged = $tagged;
428  }
429  if ($this->_tagged === null || empty($this->_tagged)) {
430  return true;
431  }
432  $urls = array_keys($this->_tagged);
433  foreach ($urls as $url) {
434  $this->remove($url);
435  unset($this->_tagged[$url]);
436  }
437  $this->getInnerCache()->save($this->_tagged, self::INNER_CACHE_NAME);
438  $result = true;
439  break;
441  $this->_log("Zend_Cache_Backend_Static : Selected Cleaning Mode Currently Unsupported By This Backend");
442  break;
444  if (empty($tags)) {
445  throw new Zend_Exception('Cannot use tag matching modes as no tags were defined');
446  }
447  if ($this->_tagged === null) {
448  $tagged = $this->getInnerCache()->load(self::INNER_CACHE_NAME);
449  $this->_tagged = $tagged;
450  }
451  if ($this->_tagged === null || empty($this->_tagged)) {
452  return true;
453  }
454  $urls = array_keys($this->_tagged);
455  foreach ($urls as $url) {
456  $difference = array_diff($tags, $this->_tagged[$url]['tags']);
457  if (count($tags) == count($difference)) {
458  $this->remove($url);
459  unset($this->_tagged[$url]);
460  }
461  }
462  $this->getInnerCache()->save($this->_tagged, self::INNER_CACHE_NAME);
463  $result = true;
464  break;
465  default:
466  Zend_Cache::throwException('Invalid mode for clean() method');
467  break;
468  }
469  return $result;
470  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
const CLEANING_MODE_OLD
Definition: Cache.php:73
_log($message, $priority=4)
Definition: Backend.php:273
const CLEANING_MODE_NOT_MATCHING_TAG
Definition: Cache.php:75
load($id, $doNotTestCacheValidity=false)
Definition: Static.php:138
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
const CLEANING_MODE_ALL
Definition: Cache.php:72
static throwException($msg, Exception $e=null)
Definition: Cache.php:205
const CLEANING_MODE_MATCHING_ANY_TAG
Definition: Cache.php:76
const CLEANING_MODE_MATCHING_TAG
Definition: Cache.php:74

◆ getInnerCache()

getInnerCache ( )

Get the Inner Cache if set

Returns
Zend_Cache_Core

Definition at line 492 of file Static.php.

493  {
494  if ($this->_tagCache === null) {
495  Zend_Cache::throwException('An Inner Cache has not been set; use setInnerCache()');
496  }
497  return $this->_tagCache;
498  }
static throwException($msg, Exception $e=null)
Definition: Cache.php:205

◆ getOption()

getOption (   $name)

Retrieve any option via interception of the parent's statically held options including the local option for a tag cache.

Parameters
string$name
Returns
mixed

Definition at line 118 of file Static.php.

119  {
120  $name = strtolower($name);
121 
122  if ($name == 'tag_cache') {
123  return $this->getInnerCache();
124  }
125 
126  return parent::getOption($name);
127  }
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ load()

load (   $id,
  $doNotTestCacheValidity = false 
)

Test if a cache is available for the given id and (if yes) return it (false else)

Note : return value is always "string" (unserialization is done by the core not by the backend)

Parameters
string$idCache id
boolean$doNotTestCacheValidityIf set to true, the cache validity won't be tested
Returns
string|false cached datas

Implements Zend_Cache_Backend_Interface.

Definition at line 138 of file Static.php.

139  {
140  if (($id = (string)$id) === '') {
141  $id = $this->_detectId();
142  } else {
143  $id = $this->_decodeId($id);
144  }
145  if (!$this->_verifyPath($id)) {
146  Zend_Cache::throwException('Invalid cache id: does not match expected public_dir path');
147  }
148  if ($doNotTestCacheValidity) {
149  $this->_log("Zend_Cache_Backend_Static::load() : \$doNotTestCacheValidity=true is unsupported by the Static backend");
150  }
151 
152  $fileName = basename($id);
153  if ($fileName === '') {
154  $fileName = $this->_options['index_filename'];
155  }
156  $pathName = $this->_options['public_dir'] . dirname($id);
157  $file = rtrim($pathName, '/') . '/' . $fileName . $this->_options['file_extension'];
158  if (file_exists($file)) {
159  $content = file_get_contents($file);
160  return $content;
161  }
162 
163  return false;
164  }
$id
Definition: fieldset.phtml:14
_log($message, $priority=4)
Definition: Backend.php:273
$fileName
Definition: translate.phtml:15
static throwException($msg, Exception $e=null)
Definition: Cache.php:205

◆ remove()

remove (   $id)

Remove a cache record

Parameters
string$idCache id
Returns
boolean True if no problem

Implements Zend_Cache_Backend_Interface.

Definition at line 309 of file Static.php.

310  {
311  if (!$this->_verifyPath($id)) {
312  Zend_Cache::throwException('Invalid cache id: does not match expected public_dir path');
313  }
314  $fileName = basename($id);
315  if ($this->_tagged === null && $tagged = $this->getInnerCache()->load(self::INNER_CACHE_NAME)) {
316  $this->_tagged = $tagged;
317  } elseif (!$this->_tagged) {
318  return false;
319  }
320  if (isset($this->_tagged[$id])) {
321  $extension = $this->_tagged[$id]['extension'];
322  } else {
323  $extension = $this->_options['file_extension'];
324  }
325  if ($fileName === '') {
326  $fileName = $this->_options['index_filename'];
327  }
328  $pathName = $this->_options['public_dir'] . dirname($id);
329  $file = realpath($pathName) . '/' . $fileName . $extension;
330  if (!file_exists($file)) {
331  return false;
332  }
333  return unlink($file);
334  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$id
Definition: fieldset.phtml:14
$fileName
Definition: translate.phtml:15
load($id, $doNotTestCacheValidity=false)
Definition: Static.php:138
static throwException($msg, Exception $e=null)
Definition: Cache.php:205

◆ removeRecursively()

removeRecursively (   $id)

Remove a cache record recursively for the given directory matching a REQUEST_URI based relative path (deletes the actual file matching this in addition to the matching directory)

Parameters
string$idCache id
Returns
boolean True if no problem

Definition at line 344 of file Static.php.

345  {
346  if (!$this->_verifyPath($id)) {
347  Zend_Cache::throwException('Invalid cache id: does not match expected public_dir path');
348  }
349  $fileName = basename($id);
350  if ($fileName === '') {
351  $fileName = $this->_options['index_filename'];
352  }
353  $pathName = $this->_options['public_dir'] . dirname($id);
354  $file = $pathName . '/' . $fileName . $this->_options['file_extension'];
355  $directory = $pathName . '/' . $fileName;
356  if (file_exists($directory)) {
357  if (!is_writable($directory)) {
358  return false;
359  }
360  if (is_dir($directory)) {
361  foreach (new DirectoryIterator($directory) as $file) {
362  if (true === $file->isFile()) {
363  if (false === unlink($file->getPathName())) {
364  return false;
365  }
366  }
367  }
368  }
369  rmdir($directory);
370  }
371  if (file_exists($file)) {
372  if (!is_writable($file)) {
373  return false;
374  }
375  return unlink($file);
376  }
377  return true;
378  }
$id
Definition: fieldset.phtml:14
$fileName
Definition: translate.phtml:15
static throwException($msg, Exception $e=null)
Definition: Cache.php:205
is_writable($path)
Definition: io.php:25

◆ save()

save (   $data,
  $id,
  $tags = array(),
  $specificLifetime = false 
)

Save some string datas into a cache record

Note : $data is always "string" (serialization is done by the core not by the backend)

Parameters
string$dataDatas to cache
string$idCache id
array$tagsArray of strings, the cache record will be tagged by each string entry
int$specificLifetimeIf != false, set a specific lifetime for this cache record (null => infinite lifetime)
Returns
boolean true if no problem

Implements Zend_Cache_Backend_Interface.

Definition at line 215 of file Static.php.

216  {
217  if ($this->_options['disable_caching']) {
218  return true;
219  }
220  $extension = null;
221  if ($this->_isSerialized($data)) {
222  $data = unserialize($data);
223  $extension = '.' . ltrim($data[1], '.');
224  $data = $data[0];
225  }
226 
227  clearstatcache();
228  if (($id = (string)$id) === '') {
229  $id = $this->_detectId();
230  } else {
231  $id = $this->_decodeId($id);
232  }
233 
234  $fileName = basename($id);
235  if ($fileName === '') {
236  $fileName = $this->_options['index_filename'];
237  }
238 
239  $pathName = realpath($this->_options['public_dir']) . dirname($id);
240  $this->_createDirectoriesFor($pathName);
241 
242  if ($id === null || strlen($id) == 0) {
243  $dataUnserialized = unserialize($data);
244  $data = $dataUnserialized['data'];
245  }
246  $ext = $this->_options['file_extension'];
247  if ($extension) $ext = $extension;
248  $file = rtrim($pathName, '/') . '/' . $fileName . $ext;
249  if ($this->_options['file_locking']) {
250  $result = file_put_contents($file, $data, LOCK_EX);
251  } else {
252  $result = file_put_contents($file, $data);
253  }
254  @chmod($file, $this->_octdec($this->_options['cache_file_perm']));
255 
256  if ($this->_tagged === null && $tagged = $this->getInnerCache()->load(self::INNER_CACHE_NAME)) {
257  $this->_tagged = $tagged;
258  } elseif ($this->_tagged === null) {
259  $this->_tagged = array();
260  }
261  if (!isset($this->_tagged[$id])) {
262  $this->_tagged[$id] = array();
263  }
264  if (!isset($this->_tagged[$id]['tags'])) {
265  $this->_tagged[$id]['tags'] = array();
266  }
267  $this->_tagged[$id]['tags'] = array_unique(array_merge($this->_tagged[$id]['tags'], $tags));
268  $this->_tagged[$id]['extension'] = $ext;
269  $this->getInnerCache()->save($this->_tagged, self::INNER_CACHE_NAME);
270  return (bool) $result;
271  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$id
Definition: fieldset.phtml:14
$fileName
Definition: translate.phtml:15
load($id, $doNotTestCacheValidity=false)
Definition: Static.php:138

◆ setInnerCache()

setInnerCache ( Zend_Cache_Core  $cache)

Set an Inner Cache, used here primarily to store Tags associated with caches created by this backend. Note: If Tags are lost, the cache should be completely cleaned as the mapping of tags to caches will have been irrevocably lost.

Parameters
Zend_Cache_Core
Returns
void

Definition at line 481 of file Static.php.

482  {
483  $this->_tagCache = $cache;
484  $this->_options['tag_cache'] = $cache;
485  }

◆ setOption()

setOption (   $name,
  $value 
)

Interceptor child method to handle the case where an Inner Cache object is being set since it's not supported by the standard backend interface

Parameters
string$name
mixed$value
Returns
Zend_Cache_Backend_Static

Definition at line 83 of file Static.php.

84  {
85  if ($name == 'tag_cache') {
86  $this->setInnerCache($value);
87  } else {
88  // See #ZF-12047 and #GH-91
89  if ($name == 'cache_file_umask') {
90  trigger_error(
91  "'cache_file_umask' is deprecated -> please use 'cache_file_perm' instead",
92  E_USER_NOTICE
93  );
94 
95  $name = 'cache_file_perm';
96  }
97  if ($name == 'cache_directory_umask') {
98  trigger_error(
99  "'cache_directory_umask' is deprecated -> please use 'cache_directory_perm' instead",
100  E_USER_NOTICE
101  );
102 
103  $name = 'cache_directory_perm';
104  }
105 
106  parent::setOption($name, $value);
107  }
108  return $this;
109  }
setInnerCache(Zend_Cache_Core $cache)
Definition: Static.php:481
$value
Definition: gender.phtml:16
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ test()

test (   $id)

Test if a cache is available or not (for the given id)

Parameters
string$idcache id
Returns
bool

Implements Zend_Cache_Backend_Interface.

Definition at line 172 of file Static.php.

173  {
174  $id = $this->_decodeId($id);
175  if (!$this->_verifyPath($id)) {
176  Zend_Cache::throwException('Invalid cache id: does not match expected public_dir path');
177  }
178 
179  $fileName = basename($id);
180  if ($fileName === '') {
181  $fileName = $this->_options['index_filename'];
182  }
183  if ($this->_tagged === null && $tagged = $this->getInnerCache()->load(self::INNER_CACHE_NAME)) {
184  $this->_tagged = $tagged;
185  } elseif (!$this->_tagged) {
186  return false;
187  }
188  $pathName = $this->_options['public_dir'] . dirname($id);
189 
190  // Switch extension if needed
191  if (isset($this->_tagged[$id])) {
192  $extension = $this->_tagged[$id]['extension'];
193  } else {
194  $extension = $this->_options['file_extension'];
195  }
196  $file = $pathName . '/' . $fileName . $extension;
197  if (file_exists($file)) {
198  return true;
199  }
200  return false;
201  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$id
Definition: fieldset.phtml:14
$fileName
Definition: translate.phtml:15
load($id, $doNotTestCacheValidity=false)
Definition: Static.php:138
static throwException($msg, Exception $e=null)
Definition: Cache.php:205

Field Documentation

◆ $_options

$_options
protected
Initial value:
= array(
'public_dir' => null,
'sub_dir' => 'html',
'file_extension' => '.html',
'index_filename' => 'index',
'file_locking' => true,
'cache_file_perm' => 0600,
'cache_directory_perm' => 0700,
'debug_header' => false,
'tag_cache' => null,
'disable_caching' => false
)

Definition at line 49 of file Static.php.

◆ $_tagCache

$_tagCache = null
protected

Definition at line 66 of file Static.php.

◆ $_tagged

$_tagged = null
protected

Definition at line 72 of file Static.php.

◆ INNER_CACHE_NAME

const INNER_CACHE_NAME = 'zend_cache_backend_static_tagcache'

Definition at line 43 of file Static.php.


The documentation for this class was generated from the following file: