File.php 6.2 KB

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