Cache.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer;
  12. use Composer\IO\IOInterface;
  13. use Composer\Util\Filesystem;
  14. use Symfony\Component\Finder\Finder;
  15. /**
  16. * Reads/writes to a filesystem cache
  17. *
  18. * @author Jordi Boggiano <j.boggiano@seld.be>
  19. */
  20. class Cache
  21. {
  22. private static $cacheCollected = false;
  23. private $io;
  24. private $root;
  25. private $enabled = true;
  26. private $whitelist;
  27. private $filesystem;
  28. /**
  29. * @param IOInterface $io
  30. * @param string $cacheDir location of the cache
  31. * @param string $whitelist List of characters that are allowed in path names (used in a regex character class)
  32. * @param Filesystem $filesystem optional filesystem instance
  33. */
  34. public function __construct(IOInterface $io, $cacheDir, $whitelist = 'a-z0-9.', Filesystem $filesystem = null)
  35. {
  36. $this->io = $io;
  37. $this->root = rtrim($cacheDir, '/\\') . '/';
  38. $this->whitelist = $whitelist;
  39. $this->filesystem = $filesystem ?: new Filesystem();
  40. if (!is_dir($this->root)) {
  41. if (!@mkdir($this->root, 0777, true)) {
  42. $this->enabled = false;
  43. }
  44. }
  45. }
  46. public function isEnabled()
  47. {
  48. return $this->enabled;
  49. }
  50. public function getRoot()
  51. {
  52. return $this->root;
  53. }
  54. public function read($file)
  55. {
  56. $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
  57. if ($this->enabled && file_exists($this->root . $file)) {
  58. if ($this->io->isDebug()) {
  59. $this->io->writeError('Reading '.$this->root . $file.' from cache');
  60. }
  61. return file_get_contents($this->root . $file);
  62. }
  63. return false;
  64. }
  65. public function write($file, $contents)
  66. {
  67. if ($this->enabled) {
  68. $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
  69. if ($this->io->isDebug()) {
  70. $this->io->writeError('Writing '.$this->root . $file.' into cache');
  71. }
  72. try {
  73. return file_put_contents($this->root . $file, $contents);
  74. } catch (\ErrorException $e) {
  75. if (preg_match('{^file_put_contents\(\): Only ([0-9]+) of ([0-9]+) bytes written}', $e->getMessage(), $m)) {
  76. // Remove partial file.
  77. unlink($this->root . $file);
  78. $message = sprintf(
  79. '<warning>Writing %1$s into cache failed after %2$u of %3$u bytes written, only %4$u bytes of free space available</warning>',
  80. $this->root . $file,
  81. $m[1],
  82. $m[2],
  83. @disk_free_space($this->root . dirname($file))
  84. );
  85. $this->io->writeError($message);
  86. return false;
  87. }
  88. throw $e;
  89. }
  90. }
  91. return false;
  92. }
  93. /**
  94. * Copy a file into the cache
  95. */
  96. public function copyFrom($file, $source)
  97. {
  98. if ($this->enabled) {
  99. $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
  100. $this->filesystem->ensureDirectoryExists(dirname($this->root . $file));
  101. if ($this->io->isDebug()) {
  102. $this->io->writeError('Writing '.$this->root . $file.' into cache');
  103. }
  104. return copy($source, $this->root . $file);
  105. }
  106. return false;
  107. }
  108. /**
  109. * Copy a file out of the cache
  110. */
  111. public function copyTo($file, $target)
  112. {
  113. $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
  114. if ($this->enabled && file_exists($this->root . $file)) {
  115. touch($this->root . $file, filemtime($this->root . $file), time());
  116. if ($this->io->isDebug()) {
  117. $this->io->writeError('Reading '.$this->root . $file.' from cache');
  118. }
  119. return copy($this->root . $file, $target);
  120. }
  121. return false;
  122. }
  123. public function gcIsNecessary()
  124. {
  125. return (!self::$cacheCollected && !mt_rand(0, 50));
  126. }
  127. public function remove($file)
  128. {
  129. $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
  130. if ($this->enabled && file_exists($this->root . $file)) {
  131. return $this->filesystem->unlink($this->root . $file);
  132. }
  133. return false;
  134. }
  135. public function gc($ttl, $maxSize)
  136. {
  137. if ($this->enabled) {
  138. $expire = new \DateTime();
  139. $expire->modify('-'.$ttl.' seconds');
  140. $finder = $this->getFinder()->date('until '.$expire->format('Y-m-d H:i:s'));
  141. foreach ($finder as $file) {
  142. $this->filesystem->unlink($file->getPathname());
  143. }
  144. $totalSize = $this->filesystem->size($this->root);
  145. if ($totalSize > $maxSize) {
  146. $iterator = $this->getFinder()->sortByAccessedTime()->getIterator();
  147. while ($totalSize > $maxSize && $iterator->valid()) {
  148. $filepath = $iterator->current()->getPathname();
  149. $totalSize -= $this->filesystem->size($filepath);
  150. $this->filesystem->unlink($filepath);
  151. $iterator->next();
  152. }
  153. }
  154. self::$cacheCollected = true;
  155. return true;
  156. }
  157. return false;
  158. }
  159. public function sha1($file)
  160. {
  161. $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
  162. if ($this->enabled && file_exists($this->root . $file)) {
  163. return sha1_file($this->root . $file);
  164. }
  165. return false;
  166. }
  167. public function sha256($file)
  168. {
  169. $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
  170. if ($this->enabled && file_exists($this->root . $file)) {
  171. return hash_file('sha256', $this->root . $file);
  172. }
  173. return false;
  174. }
  175. protected function getFinder()
  176. {
  177. return Finder::create()->in($this->root)->files();
  178. }
  179. }