/library/Zend/Cache/Frontend/File.php

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