File.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * Class Minify_Cache_File
  4. * @package Minify
  5. */
  6. class Minify_Cache_File {
  7. public function __construct($path = '', $fileLocking = false)
  8. {
  9. if (! $path) {
  10. $path = self::tmp();
  11. }
  12. $this->_locking = $fileLocking;
  13. $this->_path = $path;
  14. }
  15. /**
  16. * Write data to cache.
  17. *
  18. * @param string $id cache id (e.g. a filename)
  19. *
  20. * @param string $data
  21. *
  22. * @return bool success
  23. */
  24. public function store($id, $data)
  25. {
  26. $flag = $this->_locking
  27. ? LOCK_EX
  28. : null;
  29. $file = $this->_path . '/' . $id;
  30. if (! @file_put_contents($file, $data, $flag)) {
  31. $this->_log("Minify_Cache_File: Write failed to '$file'");
  32. }
  33. // write control
  34. if ($data !== $this->fetch($id)) {
  35. @unlink($file);
  36. $this->_log("Minify_Cache_File: Post-write read failed for '$file'");
  37. return false;
  38. }
  39. return true;
  40. }
  41. /**
  42. * Get the size of a cache entry
  43. *
  44. * @param string $id cache id (e.g. a filename)
  45. *
  46. * @return int size in bytes
  47. */
  48. public function getSize($id)
  49. {
  50. return filesize($this->_path . '/' . $id);
  51. }
  52. /**
  53. * Does a valid cache entry exist?
  54. *
  55. * @param string $id cache id (e.g. a filename)
  56. *
  57. * @param int $srcMtime mtime of the original source file(s)
  58. *
  59. * @return bool exists
  60. */
  61. public function isValid($id, $srcMtime)
  62. {
  63. $file = $this->_path . '/' . $id;
  64. return (is_file($file) && (filemtime($file) >= $srcMtime));
  65. }
  66. /**
  67. * Send the cached content to output
  68. *
  69. * @param string $id cache id (e.g. a filename)
  70. */
  71. public function display($id)
  72. {
  73. if ($this->_locking) {
  74. $fp = fopen($this->_path . '/' . $id, 'rb');
  75. flock($fp, LOCK_SH);
  76. fpassthru($fp);
  77. flock($fp, LOCK_UN);
  78. fclose($fp);
  79. } else {
  80. readfile($this->_path . '/' . $id);
  81. }
  82. }
  83. /**
  84. * Fetch the cached content
  85. *
  86. * @param string $id cache id (e.g. a filename)
  87. *
  88. * @return string
  89. */
  90. public function fetch($id)
  91. {
  92. if ($this->_locking) {
  93. $fp = fopen($this->_path . '/' . $id, 'rb');
  94. flock($fp, LOCK_SH);
  95. $ret = stream_get_contents($fp);
  96. flock($fp, LOCK_UN);
  97. fclose($fp);
  98. return $ret;
  99. } else {
  100. return file_get_contents($this->_path . '/' . $id);
  101. }
  102. }
  103. /**
  104. * Fetch the cache path used
  105. *
  106. * @return string
  107. */
  108. public function getPath()
  109. {
  110. return $this->_path;
  111. }
  112. /**
  113. * Get a usable temp directory
  114. *
  115. * Adapted from Solar/Dir.php
  116. * @author Paul M. Jones <pmjones@solarphp.com>
  117. * @license http://opensource.org/licenses/bsd-license.php BSD
  118. * @link http://solarphp.com/trac/core/browser/trunk/Solar/Dir.php
  119. *
  120. * @return string
  121. */
  122. public static function tmp()
  123. {
  124. static $tmp = null;
  125. if (! $tmp) {
  126. $tmp = function_exists('sys_get_temp_dir')
  127. ? sys_get_temp_dir()
  128. : self::_tmp();
  129. $tmp = rtrim($tmp, DIRECTORY_SEPARATOR);
  130. }
  131. return $tmp;
  132. }
  133. /**
  134. * Returns the OS-specific directory for temporary files
  135. *
  136. * @author Paul M. Jones <pmjones@solarphp.com>
  137. * @license http://opensource.org/licenses/bsd-license.php BSD
  138. * @link http://solarphp.com/trac/core/browser/trunk/Solar/Dir.php
  139. *
  140. * @return string
  141. */
  142. protected static function _tmp()
  143. {
  144. // non-Windows system?
  145. if (strtolower(substr(PHP_OS, 0, 3)) != 'win') {
  146. $tmp = empty($_ENV['TMPDIR']) ? getenv('TMPDIR') : $_ENV['TMPDIR'];
  147. if ($tmp) {
  148. return $tmp;
  149. } else {
  150. return '/tmp';
  151. }
  152. }
  153. // Windows 'TEMP'
  154. $tmp = empty($_ENV['TEMP']) ? getenv('TEMP') : $_ENV['TEMP'];
  155. if ($tmp) {
  156. return $tmp;
  157. }
  158. // Windows 'TMP'
  159. $tmp = empty($_ENV['TMP']) ? getenv('TMP') : $_ENV['TMP'];
  160. if ($tmp) {
  161. return $tmp;
  162. }
  163. // Windows 'windir'
  164. $tmp = empty($_ENV['windir']) ? getenv('windir') : $_ENV['windir'];
  165. if ($tmp) {
  166. return $tmp;
  167. }
  168. // final fallback for Windows
  169. return getenv('SystemRoot') . '\\temp';
  170. }
  171. /**
  172. * Send message to the Minify logger
  173. * @param string $msg
  174. * @return null
  175. */
  176. protected function _log($msg)
  177. {
  178. require_once 'Minify/Logger.php';
  179. Minify_Logger::log($msg);
  180. }
  181. private $_path = null;
  182. private $_locking = null;
  183. }