PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Composer/Cache.php

https://gitlab.com/tigefa/composer
PHP | 226 lines | 201 code | 5 blank | 20 comment | 0 complexity | 5f45684f2d79f5bc4652b6ca848a324a MD5 | raw file
  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 Composer\Util\Silencer;
  15. use Symfony\Component\Finder\Finder;
  16. /**
  17. * Reads/writes to a filesystem cache
  18. *
  19. * @author Jordi Boggiano <j.boggiano@seld.be>
  20. */
  21. class Cache
  22. {
  23. private static $cacheCollected = false;
  24. private $io;
  25. private $root;
  26. private $enabled = true;
  27. private $whitelist;
  28. private $filesystem;
  29. /**
  30. * @param IOInterface $io
  31. * @param string $cacheDir location of the cache
  32. * @param string $whitelist List of characters that are allowed in path names (used in a regex character class)
  33. * @param Filesystem $filesystem optional filesystem instance
  34. */
  35. public function __construct(IOInterface $io, $cacheDir, $whitelist = 'a-z0-9.', Filesystem $filesystem = null)
  36. {
  37. $this->io = $io;
  38. $this->root = rtrim($cacheDir, '/\\') . '/';
  39. $this->whitelist = $whitelist;
  40. $this->filesystem = $filesystem ?: new Filesystem();
  41. if (
  42. (!is_dir($this->root) && !Silencer::call('mkdir', $this->root, 0777, true))
  43. || !is_writable($this->root)
  44. ) {
  45. $this->io->writeError('<warning>Cannot create cache directory ' . $this->root . ', or directory is not writable. Proceeding without cache</warning>');
  46. $this->enabled = false;
  47. }
  48. }
  49. public function isEnabled()
  50. {
  51. return $this->enabled;
  52. }
  53. public function getRoot()
  54. {
  55. return $this->root;
  56. }
  57. public function read($file)
  58. {
  59. $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
  60. if ($this->enabled && file_exists($this->root . $file)) {
  61. $this->io->writeError('Reading '.$this->root . $file.' from cache', true, IOInterface::DEBUG);
  62. return file_get_contents($this->root . $file);
  63. }
  64. return false;
  65. }
  66. public function write($file, $contents)
  67. {
  68. if ($this->enabled) {
  69. $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
  70. $this->io->writeError('Writing '.$this->root . $file.' into cache', true, IOInterface::DEBUG);
  71. try {
  72. return file_put_contents($this->root . $file, $contents);
  73. } catch (\ErrorException $e) {
  74. $this->io->writeError('<warning>Failed to write into cache: '.$e->getMessage().'</warning>', true, IOInterface::DEBUG);
  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 (!file_exists($source)) {
  102. $this->io->writeError('<error>'.$source.' does not exist, can not write into cache</error>');
  103. } elseif ($this->io->isDebug()) {
  104. $this->io->writeError('Writing '.$this->root . $file.' into cache from '.$source);
  105. }
  106. return copy($source, $this->root . $file);
  107. }
  108. return false;
  109. }
  110. /**
  111. * Copy a file out of the cache
  112. */
  113. public function copyTo($file, $target)
  114. {
  115. $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
  116. if ($this->enabled && file_exists($this->root . $file)) {
  117. try {
  118. touch($this->root . $file, filemtime($this->root . $file), time());
  119. } catch (\ErrorException $e) {
  120. // fallback in case the above failed due to incorrect ownership
  121. // see https://github.com/composer/composer/issues/4070
  122. Silencer::call('touch', $this->root . $file);
  123. }
  124. $this->io->writeError('Reading '.$this->root . $file.' from cache', true, IOInterface::DEBUG);
  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. }