Cache.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 $io;
  23. private $root;
  24. private $enabled = true;
  25. private $whitelist;
  26. private $filesystem;
  27. /**
  28. * @param IOInterface $io
  29. * @param string $cacheDir location of the cache
  30. * @param string $whitelist List of characters that are allowed in path names (used in a regex character class)
  31. * @param Filesystem $filesystem optional filesystem instance
  32. */
  33. public function __construct(IOInterface $io, $cacheDir, $whitelist = 'a-z0-9.', Filesystem $filesystem = null)
  34. {
  35. $this->io = $io;
  36. $this->root = rtrim($cacheDir, '/\\') . '/';
  37. $this->whitelist = $whitelist;
  38. $this->filesystem = $filesystem ?: new Filesystem();
  39. if (!is_dir($this->root)) {
  40. if (!@mkdir($this->root, 0777, true)) {
  41. $this->enabled = false;
  42. }
  43. }
  44. }
  45. public function isEnabled()
  46. {
  47. return $this->enabled;
  48. }
  49. public function getRoot()
  50. {
  51. return $this->root;
  52. }
  53. public function read($file)
  54. {
  55. $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
  56. if ($this->enabled && file_exists($this->root . $file)) {
  57. return file_get_contents($this->root . $file);
  58. }
  59. return false;
  60. }
  61. public function write($file, $contents)
  62. {
  63. if ($this->enabled) {
  64. $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
  65. return file_put_contents($this->root . $file, $contents);
  66. }
  67. return false;
  68. }
  69. public function copyFrom($file, $source)
  70. {
  71. if ($this->enabled) {
  72. $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
  73. $this->filesystem->ensureDirectoryExists(dirname($this->root . $file));
  74. return copy($source, $this->root . $file);
  75. }
  76. return false;
  77. }
  78. public function copyTo($file, $target)
  79. {
  80. $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
  81. if ($this->enabled && file_exists($this->root . $file)) {
  82. touch($this->root . $file);
  83. return copy($this->root . $file, $target);
  84. }
  85. return false;
  86. }
  87. public function remove($file)
  88. {
  89. $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
  90. if ($this->enabled && file_exists($this->root . $file)) {
  91. return unlink($this->root . $file);
  92. }
  93. return false;
  94. }
  95. public function gc($ttl, $maxSize)
  96. {
  97. $expire = new \DateTime();
  98. $expire->modify('-'.$ttl.' seconds');
  99. $finder = $this->getFinder()->date('until '.$expire->format('Y-m-d H:i:s'));
  100. foreach ($finder as $file) {
  101. unlink($file->getRealPath());
  102. }
  103. $totalSize = $this->filesystem->size($this->root);
  104. if ($totalSize > $maxSize) {
  105. $iterator = $this->getFinder()->sortByAccessedTime()->getIterator();
  106. while ($totalSize > $maxSize && $iterator->valid()) {
  107. $filepath = $iterator->current()->getRealPath();
  108. $totalSize -= $this->filesystem->size($filepath);
  109. unlink($filepath);
  110. $iterator->next();
  111. }
  112. }
  113. return true;
  114. }
  115. public function sha1($file)
  116. {
  117. $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
  118. if ($this->enabled && file_exists($this->root . $file)) {
  119. return sha1_file($this->root . $file);
  120. }
  121. return false;
  122. }
  123. public function sha256($file)
  124. {
  125. $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
  126. if ($this->enabled && file_exists($this->root . $file)) {
  127. return hash_file('sha256', $this->root . $file);
  128. }
  129. return false;
  130. }
  131. protected function getFinder()
  132. {
  133. return Finder::create()->in($this->root)->files();
  134. }
  135. }