PageRenderTime 56ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/Group-I/jobeet/lib/vendor/symfony/lib/cache/sfFileCache.class.php

https://bitbucket.org/hosseinzolfi/db-lab-spring-2011/
PHP | 335 lines | 200 code | 43 blank | 92 comment | 31 complexity | 39ab0ef9b44ce68f7de1171abdcfd119 MD5 | raw file
Possible License(s): ISC, AGPL-3.0, LGPL-2.1, BSD-3-Clause, LGPL-3.0
  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 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
  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. $file_path = $this->getFilePath($key);
  49. if (!file_exists($file_path))
  50. {
  51. return $default;
  52. }
  53. $data = $this->read($file_path, self::READ_DATA);
  54. if ($data[self::READ_DATA] === null)
  55. {
  56. return $default;
  57. }
  58. return $data[self::READ_DATA];
  59. }
  60. /**
  61. * @see sfCache
  62. */
  63. public function has($key)
  64. {
  65. $path = $this->getFilePath($key);
  66. return file_exists($path) && $this->isValid($path);
  67. }
  68. /**
  69. * @see sfCache
  70. */
  71. public function set($key, $data, $lifetime = null)
  72. {
  73. if ($this->getOption('automatic_cleaning_factor') > 0 && rand(1, $this->getOption('automatic_cleaning_factor')) == 1)
  74. {
  75. $this->clean(sfCache::OLD);
  76. }
  77. return $this->write($this->getFilePath($key), $data, time() + $this->getLifetime($lifetime));
  78. }
  79. /**
  80. * @see sfCache
  81. */
  82. public function remove($key)
  83. {
  84. return @unlink($this->getFilePath($key));
  85. }
  86. /**
  87. * @see sfCache
  88. */
  89. public function removePattern($pattern)
  90. {
  91. if (false !== strpos($pattern, '**'))
  92. {
  93. $pattern = str_replace(sfCache::SEPARATOR, DIRECTORY_SEPARATOR, $pattern).self::EXTENSION;
  94. $regexp = self::patternToRegexp($pattern);
  95. $paths = array();
  96. foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->getOption('cache_dir'))) as $path)
  97. {
  98. if (preg_match($regexp, str_replace($this->getOption('cache_dir').DIRECTORY_SEPARATOR, '', $path)))
  99. {
  100. $paths[] = $path;
  101. }
  102. }
  103. }
  104. else
  105. {
  106. $paths = glob($this->getOption('cache_dir').DIRECTORY_SEPARATOR.str_replace(sfCache::SEPARATOR, DIRECTORY_SEPARATOR, $pattern).self::EXTENSION);
  107. }
  108. foreach ($paths as $path)
  109. {
  110. if (is_dir($path))
  111. {
  112. sfToolkit::clearDirectory($path);
  113. }
  114. else
  115. {
  116. @unlink($path);
  117. }
  118. }
  119. }
  120. /**
  121. * @see sfCache
  122. */
  123. public function clean($mode = sfCache::ALL)
  124. {
  125. if (!is_dir($this->getOption('cache_dir')))
  126. {
  127. return true;
  128. }
  129. $result = true;
  130. foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->getOption('cache_dir'))) as $file)
  131. {
  132. if (sfCache::ALL == $mode || !$this->isValid($file))
  133. {
  134. $result = @unlink($file) && $result;
  135. }
  136. }
  137. return $result;
  138. }
  139. /**
  140. * @see sfCache
  141. */
  142. public function getTimeout($key)
  143. {
  144. $path = $this->getFilePath($key);
  145. if (!file_exists($path))
  146. {
  147. return 0;
  148. }
  149. $data = $this->read($path, self::READ_TIMEOUT);
  150. return $data[self::READ_TIMEOUT] < time() ? 0 : $data[self::READ_TIMEOUT];
  151. }
  152. /**
  153. * @see sfCache
  154. */
  155. public function getLastModified($key)
  156. {
  157. $path = $this->getFilePath($key);
  158. if (!file_exists($path))
  159. {
  160. return 0;
  161. }
  162. $data = $this->read($path, self::READ_TIMEOUT | self::READ_LAST_MODIFIED);
  163. if ($data[self::READ_TIMEOUT] < time())
  164. {
  165. return 0;
  166. }
  167. return $data[self::READ_LAST_MODIFIED];
  168. }
  169. protected function isValid($path)
  170. {
  171. $data = $this->read($path, self::READ_TIMEOUT);
  172. return time() < $data[self::READ_TIMEOUT];
  173. }
  174. /**
  175. * Converts a cache key to a full path.
  176. *
  177. * @param string $key The cache key
  178. *
  179. * @return string The full path to the cache file
  180. */
  181. protected function getFilePath($key)
  182. {
  183. return $this->getOption('cache_dir').DIRECTORY_SEPARATOR.str_replace(sfCache::SEPARATOR, DIRECTORY_SEPARATOR, $key).self::EXTENSION;
  184. }
  185. /**
  186. * Reads the cache file and returns the content.
  187. *
  188. * @param string $path The file path
  189. * @param mixed $type The type of data you want to be returned
  190. * sfFileCache::READ_DATA: The cache content
  191. * sfFileCache::READ_TIMEOUT: The timeout
  192. * sfFileCache::READ_LAST_MODIFIED: The last modification timestamp
  193. *
  194. * @return array the (meta)data of the cache file. E.g. $data[sfFileCache::READ_DATA]
  195. *
  196. * @throws sfCacheException
  197. */
  198. protected function read($path, $type = self::READ_DATA)
  199. {
  200. if (!$fp = @fopen($path, 'rb'))
  201. {
  202. throw new sfCacheException(sprintf('Unable to read cache file "%s".', $path));
  203. }
  204. @flock($fp, LOCK_SH);
  205. $data[self::READ_TIMEOUT] = intval(@stream_get_contents($fp, 12, 0));
  206. if ($type != self::READ_TIMEOUT && time() < $data[self::READ_TIMEOUT])
  207. {
  208. if ($type & self::READ_LAST_MODIFIED)
  209. {
  210. $data[self::READ_LAST_MODIFIED] = intval(@stream_get_contents($fp, 12, 12));
  211. }
  212. if ($type & self::READ_DATA)
  213. {
  214. fseek($fp, 0, SEEK_END);
  215. $length = ftell($fp) - 24;
  216. fseek($fp, 24);
  217. $data[self::READ_DATA] = @fread($fp, $length);
  218. }
  219. }
  220. else
  221. {
  222. $data[self::READ_LAST_MODIFIED] = null;
  223. $data[self::READ_DATA] = null;
  224. }
  225. @flock($fp, LOCK_UN);
  226. @fclose($fp);
  227. return $data;
  228. }
  229. /**
  230. * Writes the given data in the cache file.
  231. *
  232. * @param string $path The file path
  233. * @param string $data The data to put in cache
  234. * @param integer $timeout The timeout timestamp
  235. *
  236. * @return boolean true if ok, otherwise false
  237. *
  238. * @throws sfCacheException
  239. */
  240. protected function write($path, $data, $timeout)
  241. {
  242. $current_umask = umask();
  243. umask(0000);
  244. if (!is_dir(dirname($path)))
  245. {
  246. // create directory structure if needed
  247. mkdir(dirname($path), 0777, true);
  248. }
  249. $tmpFile = tempnam(dirname($path), basename($path));
  250. if (!$fp = @fopen($tmpFile, 'wb'))
  251. {
  252. throw new sfCacheException(sprintf('Unable to write cache file "%s".', $tmpFile));
  253. }
  254. @fwrite($fp, str_pad($timeout, 12, 0, STR_PAD_LEFT));
  255. @fwrite($fp, str_pad(time(), 12, 0, STR_PAD_LEFT));
  256. @fwrite($fp, $data);
  257. @fclose($fp);
  258. // Hack from Agavi (http://trac.agavi.org/changeset/3979)
  259. // With php < 5.2.6 on win32, renaming to an already existing file doesn't work, but copy does,
  260. // so we simply assume that when rename() fails that we are on win32 and try to use copy()
  261. if (!@rename($tmpFile, $path))
  262. {
  263. if (copy($tmpFile, $path))
  264. {
  265. unlink($tmpFile);
  266. }
  267. }
  268. chmod($path, 0666);
  269. umask($current_umask);
  270. return true;
  271. }
  272. /**
  273. * Sets the cache root directory.
  274. *
  275. * @param string $cache_dir The directory where to put the cache files
  276. */
  277. protected function setcache_dir($cache_dir)
  278. {
  279. // remove last DIRECTORY_SEPARATOR
  280. if (DIRECTORY_SEPARATOR == substr($cache_dir, -1))
  281. {
  282. $cache_dir = substr($cache_dir, 0, -1);
  283. }
  284. // create cache dir if needed
  285. if (!is_dir($cache_dir))
  286. {
  287. $current_umask = umask(0000);
  288. @mkdir($cache_dir, 0777, true);
  289. umask($current_umask);
  290. }
  291. }
  292. }