/Zend/Cache/Frontend/File.php

https://github.com/MontmereLimited/ZendFramework-v1 · PHP · 222 lines · 96 code · 19 blank · 107 comment · 23 complexity · 8bd98a1e8ca8c193bca8623f90ede646 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-2011 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 24218 2011-07-10 01:22:58Z ramon $
  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-2011 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_files option
  92. *
  93. * @param array $masterFiles the complete paths and name of the master files
  94. */
  95. public function setMasterFiles(array $masterFiles)
  96. {
  97. $this->_specificOptions['master_file'] = null; // to keep a compatibility
  98. $this->_specificOptions['master_files'] = null;
  99. $this->_masterFile_mtimes = array();
  100. clearstatcache();
  101. $i = 0;
  102. foreach ($masterFiles as $masterFile) {
  103. if (file_exists($masterFile)) {
  104. $mtime = filemtime($masterFile);
  105. } else {
  106. $mtime = false;
  107. }
  108. if (!$this->_specificOptions['ignore_missing_master_files'] && !$mtime) {
  109. Zend_Cache::throwException('Unable to read master_file : ' . $masterFile);
  110. }
  111. $this->_masterFile_mtimes[$i] = $mtime;
  112. $this->_specificOptions['master_files'][$i] = $masterFile;
  113. if ($i === 0) { // to keep a compatibility
  114. $this->_specificOptions['master_file'] = $masterFile;
  115. }
  116. $i++;
  117. }
  118. }
  119. /**
  120. * Change the master_file option
  121. *
  122. * To keep the compatibility
  123. *
  124. * @deprecated
  125. * @param string $masterFile the complete path and name of the master file
  126. */
  127. public function setMasterFile($masterFile)
  128. {
  129. $this->setMasterFiles(array($masterFile));
  130. }
  131. /**
  132. * Public frontend to set an option
  133. *
  134. * Just a wrapper to get a specific behaviour for master_file
  135. *
  136. * @param string $name Name of the option
  137. * @param mixed $value Value of the option
  138. * @throws Zend_Cache_Exception
  139. * @return void
  140. */
  141. public function setOption($name, $value)
  142. {
  143. if ($name == 'master_file') {
  144. $this->setMasterFile($value);
  145. } else if ($name == 'master_files') {
  146. $this->setMasterFiles($value);
  147. } else {
  148. parent::setOption($name, $value);
  149. }
  150. }
  151. /**
  152. * Test if a cache is available for the given id and (if yes) return it (false else)
  153. *
  154. * @param string $id Cache id
  155. * @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
  156. * @param boolean $doNotUnserialize Do not serialize (even if automatic_serialization is true) => for internal use
  157. * @return mixed|false Cached datas
  158. */
  159. public function load($id, $doNotTestCacheValidity = false, $doNotUnserialize = false)
  160. {
  161. if (!$doNotTestCacheValidity) {
  162. if ($this->test($id)) {
  163. return parent::load($id, true, $doNotUnserialize);
  164. }
  165. return false;
  166. }
  167. return parent::load($id, true, $doNotUnserialize);
  168. }
  169. /**
  170. * Test if a cache is available for the given id
  171. *
  172. * @param string $id Cache id
  173. * @return int|false Last modified time of cache entry if it is available, false otherwise
  174. */
  175. public function test($id)
  176. {
  177. $lastModified = parent::test($id);
  178. if ($lastModified) {
  179. if ($this->_specificOptions['master_files_mode'] == self::MODE_AND) {
  180. // MODE_AND
  181. foreach($this->_masterFile_mtimes as $masterFileMTime) {
  182. if ($masterFileMTime) {
  183. if ($lastModified > $masterFileMTime) {
  184. return $lastModified;
  185. }
  186. }
  187. }
  188. } else {
  189. // MODE_OR
  190. $res = true;
  191. foreach($this->_masterFile_mtimes as $masterFileMTime) {
  192. if ($masterFileMTime) {
  193. if ($lastModified <= $masterFileMTime) {
  194. return false;
  195. }
  196. }
  197. }
  198. return $lastModified;
  199. }
  200. }
  201. return false;
  202. }
  203. }