Cache.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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->write('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->write('Writing '.$this->root . $file.' into cache');
  71. }
  72. return file_put_contents($this->root . $file, $contents);
  73. }
  74. return false;
  75. }
  76. /**
  77. * Copy a file into the cache
  78. */
  79. public function copyFrom($file, $source)
  80. {
  81. if ($this->enabled) {
  82. $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
  83. $this->filesystem->ensureDirectoryExists(dirname($this->root . $file));
  84. if ($this->io->isDebug()) {
  85. $this->io->write('Writing '.$this->root . $file.' into cache');
  86. }
  87. return copy($source, $this->root . $file);
  88. }
  89. return false;
  90. }
  91. /**
  92. * Copy a file out of the cache
  93. */
  94. public function copyTo($file, $target)
  95. {
  96. $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
  97. if ($this->enabled && file_exists($this->root . $file)) {
  98. touch($this->root . $file);
  99. if ($this->io->isDebug()) {
  100. $this->io->write('Reading '.$this->root . $file.' from cache');
  101. }
  102. return copy($this->root . $file, $target);
  103. }
  104. return false;
  105. }
  106. public function gcIsNecessary()
  107. {
  108. return (!self::$cacheCollected && !mt_rand(0, 50));
  109. }
  110. public function remove($file)
  111. {
  112. $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
  113. if ($this->enabled && file_exists($this->root . $file)) {
  114. return unlink($this->root . $file);
  115. }
  116. return false;
  117. }
  118. public function gc($ttl, $maxSize)
  119. {
  120. if ($this->enabled)
  121. {
  122. $expire = new \DateTime();
  123. $expire->modify('-'.$ttl.' seconds');
  124. $finder = $this->getFinder()->date('until '.$expire->format('Y-m-d H:i:s'));
  125. foreach ($finder as $file) {
  126. unlink($file->getPathname());
  127. }
  128. $totalSize = $this->filesystem->size($this->root);
  129. if ($totalSize > $maxSize) {
  130. $iterator = $this->getFinder()->sortByAccessedTime()->getIterator();
  131. while ($totalSize > $maxSize && $iterator->valid()) {
  132. $filepath = $iterator->current()->getPathname();
  133. $totalSize -= $this->filesystem->size($filepath);
  134. unlink($filepath);
  135. $iterator->next();
  136. }
  137. }
  138. self::$cacheCollected = true;
  139. return true;
  140. }
  141. return false;
  142. }
  143. public function sha1($file)
  144. {
  145. $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
  146. if ($this->enabled && file_exists($this->root . $file)) {
  147. return sha1_file($this->root . $file);
  148. }
  149. return false;
  150. }
  151. public function sha256($file)
  152. {
  153. $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
  154. if ($this->enabled && file_exists($this->root . $file)) {
  155. return hash_file('sha256', $this->root . $file);
  156. }
  157. return false;
  158. }
  159. protected function getFinder()
  160. {
  161. return Finder::create()->in($this->root)->files();
  162. }
  163. }