PageRenderTime 49ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/system/vendor/swift/Swift/Cache/Disk.php

http://github.com/ushahidi/Ushahidi_Web
PHP | 130 lines | 90 code | 4 blank | 36 comment | 1 complexity | 4cfb2a139573bb539da6100b9589228b MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Swift Mailer disk runtime cache
  4. * Please read the LICENSE file
  5. * @author Chris Corbyn <chris@w3style.co.uk>
  6. * @package Swift_Cache
  7. * @license GNU Lesser General Public License
  8. */
  9. require_once dirname(__FILE__) . "/../ClassLoader.php";
  10. Swift_ClassLoader::load("Swift_Cache");
  11. /**
  12. * Caches data in files on disk - this is the best approach if possible
  13. * @package Swift_Cache
  14. * @author Chris Corbyn <chris@w3style.co.uk>
  15. */
  16. class Swift_Cache_Disk extends Swift_Cache
  17. {
  18. /**
  19. * Open file handles
  20. * @var array
  21. */
  22. protected $open = array();
  23. /**
  24. * The prefix to prepend to files
  25. * @var string
  26. */
  27. protected $prefix;
  28. /**
  29. * The save path on disk
  30. * @var string
  31. */
  32. protected static $save_path = "/tmp";
  33. /**
  34. * Ctor
  35. */
  36. public function __construct()
  37. {
  38. $this->prefix = md5(uniqid(microtime(), true));
  39. }
  40. /**
  41. * Set the save path of the disk - this is a global setting and called statically!
  42. * @param string The path to a writable directory
  43. */
  44. public static function setSavePath($path="/tmp")
  45. {
  46. self::$save_path = realpath($path);
  47. }
  48. /**
  49. * Write data to the cache
  50. * @param string The cache key
  51. * @param string The data to write
  52. */
  53. public function write($key, $data)
  54. {
  55. $handle = fopen(self::$save_path . "/" . $this->prefix . $key, "ab");
  56. if (false === fwrite($handle, $data))
  57. {
  58. Swift_ClassLoader::load("Swift_FileException");
  59. throw new Swift_FileException("Disk Caching failed. Tried to write to file at [" .
  60. self::$save_path . "/" . $this->prefix . $key . "] but failed. Check the permissions, or don't use disk caching.");
  61. }
  62. fclose($handle);
  63. }
  64. /**
  65. * Clear the cached data (unlink)
  66. * @param string The cache key
  67. */
  68. public function clear($key)
  69. {
  70. @unlink(self::$save_path . "/" . $this->prefix . $key);
  71. }
  72. /**
  73. * Check if data is cached for $key
  74. * @param string The cache key
  75. * @return boolean
  76. */
  77. public function has($key)
  78. {
  79. return file_exists(self::$save_path . "/" . $this->prefix . $key);
  80. }
  81. /**
  82. * Read data from the cache for $key
  83. * @param string The cache key
  84. * @param int The number of bytes to read
  85. * @return string
  86. */
  87. public function read($key, $size=null)
  88. {
  89. if ($size === null) $size = 8190;
  90. if (!$this->has($key)) return false;
  91. if (!isset($this->open[$key]))
  92. {
  93. $this->open[$key] = fopen(self::$save_path . "/" . $this->prefix . $key, "rb");
  94. }
  95. if (feof($this->open[$key]))
  96. {
  97. fclose($this->open[$key]);
  98. unset($this->open[$key]);
  99. return false;
  100. }
  101. $ret = fread($this->open[$key], $size);
  102. if ($ret !== false)
  103. {
  104. return $ret;
  105. }
  106. else
  107. {
  108. fclose($this->open[$key]);
  109. unset($this->open[$key]);
  110. return false;
  111. }
  112. }
  113. /**
  114. * Dtor.
  115. * Clear out cached data at end of script execution or cache destruction
  116. */
  117. public function __destruct()
  118. {
  119. $list = glob(self::$save_path . "/" . $this->prefix . "*");
  120. foreach ((array)$list as $f)
  121. {
  122. @unlink($f);
  123. }
  124. }
  125. }