PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Cache/Frontend/File.php

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