Cache.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. try {
  116. touch($this->root . $file, filemtime($this->root . $file), time());
  117. } catch (\ErrorException $e) {
  118. // fallback in case the above failed due to incorrect ownership
  119. // see https://github.com/composer/composer/issues/4070
  120. touch($this->root . $file);
  121. }
  122. if ($this->io->isDebug()) {
  123. $this->io->writeError('Reading '.$this->root . $file.' from cache');
  124. }
  125. return copy($this->root . $file, $target);
  126. }
  127. return false;
  128. }
  129. public function gcIsNecessary()
  130. {
  131. return (!self::$cacheCollected && !mt_rand(0, 50));
  132. }
  133. public function remove($file)
  134. {
  135. $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
  136. if ($this->enabled && file_exists($this->root . $file)) {
  137. return $this->filesystem->unlink($this->root . $file);
  138. }
  139. return false;
  140. }
  141. public function gc($ttl, $maxSize)
  142. {
  143. if ($this->enabled) {
  144. $expire = new \DateTime();
  145. $expire->modify('-'.$ttl.' seconds');
  146. $finder = $this->getFinder()->date('until '.$expire->format('Y-m-d H:i:s'));
  147. foreach ($finder as $file) {
  148. $this->filesystem->unlink($file->getPathname());
  149. }
  150. $totalSize = $this->filesystem->size($this->root);
  151. if ($totalSize > $maxSize) {
  152. $iterator = $this->getFinder()->sortByAccessedTime()->getIterator();
  153. while ($totalSize > $maxSize && $iterator->valid()) {
  154. $filepath = $iterator->current()->getPathname();
  155. $totalSize -= $this->filesystem->size($filepath);
  156. $this->filesystem->unlink($filepath);
  157. $iterator->next();
  158. }
  159. }
  160. self::$cacheCollected = true;
  161. return true;
  162. }
  163. return false;
  164. }
  165. public function sha1($file)
  166. {
  167. $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
  168. if ($this->enabled && file_exists($this->root . $file)) {
  169. return sha1_file($this->root . $file);
  170. }
  171. return false;
  172. }
  173. public function sha256($file)
  174. {
  175. $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
  176. if ($this->enabled && file_exists($this->root . $file)) {
  177. return hash_file('sha256', $this->root . $file);
  178. }
  179. return false;
  180. }
  181. protected function getFinder()
  182. {
  183. return Finder::create()->in($this->root)->files();
  184. }
  185. }