/lib/Zend/Cache/Frontend/File.php

https://gitlab.com/florianocomercial/centreon · PHP · 209 lines · 88 code · 15 blank · 106 comment · 20 complexity · 29e693a4fefb4a6f9202169af216114e MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Cache
  17. * @subpackage Zend_Cache_Frontend
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: File.php 20096 2010-01-06 02:05:09Z bkarwin $
  21. */
  22. /**
  23. * @see Zend_Cache_Core
  24. */
  25. require_once 'Zend/Cache/Core.php';
  26. /**
  27. * @package Zend_Cache
  28. * @subpackage Zend_Cache_Frontend
  29. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Cache_Frontend_File extends Zend_Cache_Core
  33. {
  34. /**
  35. * Consts for master_files_mode
  36. */
  37. const MODE_AND = 'AND';
  38. const MODE_OR = 'OR';
  39. /**
  40. * Available options
  41. *
  42. * ====> (string) master_file :
  43. * - a complete path of the master file
  44. * - deprecated (see master_files)
  45. *
  46. * ====> (array) master_files :
  47. * - an array of complete path of master files
  48. * - this option has to be set !
  49. *
  50. * ====> (string) master_files_mode :
  51. * - Zend_Cache_Frontend_File::MODE_AND or Zend_Cache_Frontend_File::MODE_OR
  52. * - if MODE_AND, then all master files have to be touched to get a cache invalidation
  53. * - if MODE_OR (default), then a single touched master file is enough to get a cache invalidation
  54. *
  55. * ====> (boolean) ignore_missing_master_files
  56. * - if set to true, missing master files are ignored silently
  57. * - if set to false (default), an exception is thrown if there is a missing master file
  58. * @var array available options
  59. */
  60. protected $_specificOptions = array(
  61. 'master_file' => null,
  62. 'master_files' => null,
  63. 'master_files_mode' => 'OR',
  64. 'ignore_missing_master_files' => false
  65. );
  66. /**
  67. * Master file mtimes
  68. *
  69. * Array of int
  70. *
  71. * @var array
  72. */
  73. private $_masterFile_mtimes = null;
  74. /**
  75. * Constructor
  76. *
  77. * @param array $options Associative array of options
  78. * @throws Zend_Cache_Exception
  79. * @return void
  80. */
  81. public function __construct(array $options = array())
  82. {
  83. while (list($name, $value) = each($options)) {
  84. $this->setOption($name, $value);
  85. }
  86. if (!isset($this->_specificOptions['master_files'])) {
  87. Zend_Cache::throwException('master_files option must be set');
  88. }
  89. }
  90. /**
  91. * Change the master_file option
  92. *
  93. * @param string $masterFile the complete path and name of the master file
  94. */
  95. public function setMasterFiles($masterFiles)
  96. {
  97. clearstatcache();
  98. $this->_specificOptions['master_file'] = $masterFiles[0]; // to keep a compatibility
  99. $this->_specificOptions['master_files'] = $masterFiles;
  100. $this->_masterFile_mtimes = array();
  101. $i = 0;
  102. foreach ($masterFiles as $masterFile) {
  103. $this->_masterFile_mtimes[$i] = @filemtime($masterFile);
  104. if ((!($this->_specificOptions['ignore_missing_master_files'])) && (!($this->_masterFile_mtimes[$i]))) {
  105. Zend_Cache::throwException('Unable to read master_file : '.$masterFile);
  106. }
  107. $i++;
  108. }
  109. }
  110. /**
  111. * Change the master_file option
  112. *
  113. * To keep the compatibility
  114. *
  115. * @deprecated
  116. * @param string $masterFile the complete path and name of the master file
  117. */
  118. public function setMasterFile($masterFile)
  119. {
  120. $this->setMasterFiles(array(0 => $masterFile));
  121. }
  122. /**
  123. * Public frontend to set an option
  124. *
  125. * Just a wrapper to get a specific behaviour for master_file
  126. *
  127. * @param string $name Name of the option
  128. * @param mixed $value Value of the option
  129. * @throws Zend_Cache_Exception
  130. * @return void
  131. */
  132. public function setOption($name, $value)
  133. {
  134. if ($name == 'master_file') {
  135. $this->setMasterFile($value);
  136. } else if ($name == 'master_files') {
  137. $this->setMasterFiles($value);
  138. } else {
  139. parent::setOption($name, $value);
  140. }
  141. }
  142. /**
  143. * Test if a cache is available for the given id and (if yes) return it (false else)
  144. *
  145. * @param string $id Cache id
  146. * @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
  147. * @param boolean $doNotUnserialize Do not serialize (even if automatic_serialization is true) => for internal use
  148. * @return mixed|false Cached datas
  149. */
  150. public function load($id, $doNotTestCacheValidity = false, $doNotUnserialize = false)
  151. {
  152. if (!$doNotTestCacheValidity) {
  153. if ($this->test($id)) {
  154. return parent::load($id, true, $doNotUnserialize);
  155. }
  156. return false;
  157. }
  158. return parent::load($id, true, $doNotUnserialize);
  159. }
  160. /**
  161. * Test if a cache is available for the given id
  162. *
  163. * @param string $id Cache id
  164. * @return int|false Last modified time of cache entry if it is available, false otherwise
  165. */
  166. public function test($id)
  167. {
  168. $lastModified = parent::test($id);
  169. if ($lastModified) {
  170. if ($this->_specificOptions['master_files_mode'] == self::MODE_AND) {
  171. // MODE_AND
  172. foreach($this->_masterFile_mtimes as $masterFileMTime) {
  173. if ($masterFileMTime) {
  174. if ($lastModified > $masterFileMTime) {
  175. return $lastModified;
  176. }
  177. }
  178. }
  179. } else {
  180. // MODE_OR
  181. $res = true;
  182. foreach($this->_masterFile_mtimes as $masterFileMTime) {
  183. if ($masterFileMTime) {
  184. if ($lastModified <= $masterFileMTime) {
  185. return false;
  186. }
  187. }
  188. }
  189. return $lastModified;
  190. }
  191. }
  192. return false;
  193. }
  194. }