File.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. if (!$fp) {
  95. return false;
  96. }
  97. flock($fp, LOCK_SH);
  98. $ret = stream_get_contents($fp);
  99. flock($fp, LOCK_UN);
  100. fclose($fp);
  101. return $ret;
  102. } else {
  103. return file_get_contents($this->_path . '/' . $id);
  104. }
  105. }
  106. /**
  107. * Fetch the cache path used
  108. *
  109. * @return string
  110. */
  111. public function getPath()
  112. {
  113. return $this->_path;
  114. }
  115. /**
  116. * Get a usable temp directory
  117. *
  118. * Adapted from Solar/Dir.php
  119. * @author Paul M. Jones <pmjones@solarphp.com>
  120. * @license http://opensource.org/licenses/bsd-license.php BSD
  121. * @link http://solarphp.com/trac/core/browser/trunk/Solar/Dir.php
  122. *
  123. * @return string
  124. */
  125. public static function tmp()
  126. {
  127. static $tmp = null;
  128. if (! $tmp) {
  129. $tmp = function_exists('sys_get_temp_dir')
  130. ? sys_get_temp_dir()
  131. : self::_tmp();
  132. $tmp = rtrim($tmp, DIRECTORY_SEPARATOR);
  133. }
  134. return $tmp;
  135. }
  136. /**
  137. * Returns the OS-specific directory for temporary files
  138. *
  139. * @author Paul M. Jones <pmjones@solarphp.com>
  140. * @license http://opensource.org/licenses/bsd-license.php BSD
  141. * @link http://solarphp.com/trac/core/browser/trunk/Solar/Dir.php
  142. *
  143. * @return string
  144. */
  145. protected static function _tmp()
  146. {
  147. // non-Windows system?
  148. if (strtolower(substr(PHP_OS, 0, 3)) != 'win') {
  149. $tmp = empty($_ENV['TMPDIR']) ? getenv('TMPDIR') : $_ENV['TMPDIR'];
  150. if ($tmp) {
  151. return $tmp;
  152. } else {
  153. return '/tmp';
  154. }
  155. }
  156. // Windows 'TEMP'
  157. $tmp = empty($_ENV['TEMP']) ? getenv('TEMP') : $_ENV['TEMP'];
  158. if ($tmp) {
  159. return $tmp;
  160. }
  161. // Windows 'TMP'
  162. $tmp = empty($_ENV['TMP']) ? getenv('TMP') : $_ENV['TMP'];
  163. if ($tmp) {
  164. return $tmp;
  165. }
  166. // Windows 'windir'
  167. $tmp = empty($_ENV['windir']) ? getenv('windir') : $_ENV['windir'];
  168. if ($tmp) {
  169. return $tmp;
  170. }
  171. // final fallback for Windows
  172. return getenv('SystemRoot') . '\\temp';
  173. }
  174. /**
  175. * Send message to the Minify logger
  176. * @param string $msg
  177. * @return null
  178. */
  179. protected function _log($msg)
  180. {
  181. Minify_Logger::log($msg);
  182. }
  183. private $_path = null;
  184. private $_locking = null;
  185. }