sfFileCache.class.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Cache class that stores content in files.
  11. *
  12. * @package symfony
  13. * @subpackage cache
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfFileCache.class.php 16899 2009-04-02 06:47:33Z fabien $
  16. */
  17. class sfFileCache extends sfCache
  18. {
  19. const READ_DATA = 1;
  20. const READ_TIMEOUT = 2;
  21. const READ_LAST_MODIFIED = 4;
  22. const EXTENSION = '.cache';
  23. /**
  24. * Initializes this sfCache instance.
  25. *
  26. * Available options:
  27. *
  28. * * cache_dir: The directory where to put cache files
  29. *
  30. * * see sfCache for options available for all drivers
  31. *
  32. * @see sfCache
  33. */
  34. public function initialize($options = array())
  35. {
  36. parent::initialize($options);
  37. if (!$this->getOption('cache_dir'))
  38. {
  39. throw new sfInitializationException('You must pass a "cache_dir" option to initialize a sfFileCache object.');
  40. }
  41. $this->setcache_dir($this->getOption('cache_dir'));
  42. }
  43. /**
  44. * @see sfCache
  45. */
  46. public function get($key, $default = null)
  47. {
  48. if (!$this->has($key))
  49. {
  50. return $default;
  51. }
  52. return $this->read($this->getFilePath($key));
  53. }
  54. /**
  55. * @see sfCache
  56. */
  57. public function has($key)
  58. {
  59. return file_exists($this->getFilePath($key)) && time() < $this->read($this->getFilePath($key), self::READ_TIMEOUT);
  60. }
  61. /**
  62. * @see sfCache
  63. */
  64. public function set($key, $data, $lifetime = null)
  65. {
  66. if ($this->getOption('automatic_cleaning_factor') > 0 && rand(1, $this->getOption('automatic_cleaning_factor')) == 1)
  67. {
  68. $this->clean(sfCache::OLD);
  69. }
  70. return $this->write($this->getFilePath($key), $data, time() + $this->getLifetime($lifetime));
  71. }
  72. /**
  73. * @see sfCache
  74. */
  75. public function remove($key)
  76. {
  77. return @unlink($this->getFilePath($key));
  78. }
  79. /**
  80. * @see sfCache
  81. */
  82. public function removePattern($pattern)
  83. {
  84. if (false !== strpos($pattern, '**'))
  85. {
  86. $pattern = str_replace(sfCache::SEPARATOR, DIRECTORY_SEPARATOR, $pattern).self::EXTENSION;
  87. $regexp = self::patternToRegexp($pattern);
  88. $paths = array();
  89. foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->getOption('cache_dir'))) as $path)
  90. {
  91. if (preg_match($regexp, str_replace($this->getOption('cache_dir').DIRECTORY_SEPARATOR, '', $path)))
  92. {
  93. $paths[] = $path;
  94. }
  95. }
  96. }
  97. else
  98. {
  99. $paths = glob($this->getOption('cache_dir').DIRECTORY_SEPARATOR.str_replace(sfCache::SEPARATOR, DIRECTORY_SEPARATOR, $pattern).self::EXTENSION);
  100. }
  101. foreach ($paths as $path)
  102. {
  103. if (is_dir($path))
  104. {
  105. sfToolkit::clearDirectory($path);
  106. }
  107. else
  108. {
  109. @unlink($path);
  110. }
  111. }
  112. }
  113. /**
  114. * @see sfCache
  115. */
  116. public function clean($mode = sfCache::ALL)
  117. {
  118. if (!is_dir($this->getOption('cache_dir')))
  119. {
  120. return true;
  121. }
  122. $result = true;
  123. foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->getOption('cache_dir'))) as $file)
  124. {
  125. if (sfCache::ALL == $mode || time() > $this->read($file, self::READ_TIMEOUT))
  126. {
  127. $result = @unlink($file) && $result;
  128. }
  129. }
  130. return $result;
  131. }
  132. /**
  133. * @see sfCache
  134. */
  135. public function getTimeout($key)
  136. {
  137. $path = $this->getFilePath($key);
  138. if (!file_exists($path))
  139. {
  140. return 0;
  141. }
  142. $timeout = $this->read($path, self::READ_TIMEOUT);
  143. return $timeout < time() ? 0 : $timeout;
  144. }
  145. /**
  146. * @see sfCache
  147. */
  148. public function getLastModified($key)
  149. {
  150. $path = $this->getFilePath($key);
  151. if (!file_exists($path) || $this->read($path, self::READ_TIMEOUT) < time())
  152. {
  153. return 0;
  154. }
  155. return $this->read($path, self::READ_LAST_MODIFIED);
  156. }
  157. /**
  158. * Converts a cache key to a full path.
  159. *
  160. * @param string $key The cache key
  161. *
  162. * @return string The full path to the cache file
  163. */
  164. protected function getFilePath($key)
  165. {
  166. return $this->getOption('cache_dir').DIRECTORY_SEPARATOR.str_replace(sfCache::SEPARATOR, DIRECTORY_SEPARATOR, $key).self::EXTENSION;
  167. }
  168. /**
  169. * Reads the cache file and returns the content.
  170. *
  171. * @param string $path The file path
  172. * @param mixed $type The type of data you want to be returned
  173. * sfFileCache::READ_DATA: The cache content
  174. * sfFileCache::READ_TIMEOUT: The timeout
  175. * sfFileCache::READ_LAST_MODIFIED: The last modification timestamp
  176. *
  177. * @return string The content of the cache file.
  178. *
  179. * @throws sfCacheException
  180. */
  181. protected function read($path, $type = self::READ_DATA)
  182. {
  183. if (!$fp = @fopen($path, 'rb'))
  184. {
  185. throw new sfCacheException(sprintf('Unable to read cache file "%s".', $path));
  186. }
  187. @flock($fp, LOCK_SH);
  188. clearstatcache(); // because the filesize can be cached by PHP itself...
  189. $length = @filesize($path);
  190. $mqr = get_magic_quotes_runtime();
  191. set_magic_quotes_runtime(0);
  192. switch ($type)
  193. {
  194. case self::READ_TIMEOUT:
  195. $data = $length ? intval(@fread($fp, 12)) : 0;
  196. break;
  197. case self::READ_LAST_MODIFIED:
  198. @fseek($fp, 12);
  199. $data = $length ? intval(@fread($fp, 12)) : 0;
  200. break;
  201. case self::READ_DATA:
  202. if ($length)
  203. {
  204. @fseek($fp, 24);
  205. $data = @fread($fp, $length - 24);
  206. }
  207. else
  208. {
  209. $data = '';
  210. }
  211. break;
  212. default:
  213. throw new sfConfigurationException(sprintf('Unknown type "%s".', $type));
  214. }
  215. set_magic_quotes_runtime($mqr);
  216. @flock($fp, LOCK_UN);
  217. @fclose($fp);
  218. return $data;
  219. }
  220. /**
  221. * Writes the given data in the cache file.
  222. *
  223. * @param string $path The file path
  224. * @param string $data The data to put in cache
  225. * @param integer $timeout The timeout timestamp
  226. *
  227. * @return boolean true if ok, otherwise false
  228. *
  229. * @throws sfCacheException
  230. */
  231. protected function write($path, $data, $timeout)
  232. {
  233. $current_umask = umask();
  234. umask(0000);
  235. if (!is_dir(dirname($path)))
  236. {
  237. // create directory structure if needed
  238. mkdir(dirname($path), 0777, true);
  239. }
  240. $tmpFile = tempnam(dirname($path), basename($path));
  241. if (!$fp = @fopen($tmpFile, 'wb'))
  242. {
  243. throw new sfCacheException(sprintf('Unable to write cache file "%s".', $tmpFile));
  244. }
  245. @fwrite($fp, str_pad($timeout, 12, 0, STR_PAD_LEFT));
  246. @fwrite($fp, str_pad(time(), 12, 0, STR_PAD_LEFT));
  247. @fwrite($fp, $data);
  248. @fclose($fp);
  249. // Hack from Agavi (http://trac.agavi.org/changeset/3979)
  250. // With php < 5.2.6 on win32, renaming to an already existing file doesn't work, but copy does,
  251. // so we simply assume that when rename() fails that we are on win32 and try to use copy()
  252. if (!@rename($tmpFile, $path))
  253. {
  254. if (copy($tmpFile, $path))
  255. {
  256. unlink($tmpFile);
  257. }
  258. }
  259. chmod($path, 0666);
  260. umask($current_umask);
  261. return true;
  262. }
  263. /**
  264. * Sets the cache root directory.
  265. *
  266. * @param string $cache_dir The directory where to put the cache files
  267. */
  268. protected function setcache_dir($cache_dir)
  269. {
  270. // remove last DIRECTORY_SEPARATOR
  271. if (DIRECTORY_SEPARATOR == substr($cache_dir, -1))
  272. {
  273. $cache_dir = substr($cache_dir, 0, -1);
  274. }
  275. // create cache dir if needed
  276. if (!is_dir($cache_dir))
  277. {
  278. $current_umask = umask(0000);
  279. @mkdir($cache_dir, 0777, true);
  280. umask($current_umask);
  281. }
  282. }
  283. }