ArchiveDownloader.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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\Downloader;
  12. use Composer\Package\PackageInterface;
  13. use Symfony\Component\Finder\Finder;
  14. /**
  15. * Base downloader for archives
  16. *
  17. * @author Kirill chEbba Chebunin <iam@chebba.org>
  18. * @author Jordi Boggiano <j.boggiano@seld.be>
  19. * @author François Pluchino <francois.pluchino@opendisplay.com>
  20. */
  21. abstract class ArchiveDownloader extends FileDownloader
  22. {
  23. /**
  24. * {@inheritDoc}
  25. */
  26. public function download(PackageInterface $package, $path)
  27. {
  28. $temporaryDir = $this->config->get('vendor-dir').'/composer/'.substr(md5(uniqid('', true)), 0, 8);
  29. $retries = 3;
  30. while ($retries--) {
  31. $fileName = parent::download($package, $path);
  32. if ($this->io->isVerbose()) {
  33. $this->io->writeError(' Extracting archive');
  34. }
  35. try {
  36. $this->filesystem->ensureDirectoryExists($temporaryDir);
  37. try {
  38. $this->extract($fileName, $temporaryDir);
  39. } catch (\Exception $e) {
  40. // remove cache if the file was corrupted
  41. parent::clearCache($package, $path);
  42. throw $e;
  43. }
  44. $this->filesystem->unlink($fileName);
  45. $contentDir = $this->getFolderContent($temporaryDir);
  46. // only one dir in the archive, extract its contents out of it
  47. if (1 === count($contentDir) && is_dir(reset($contentDir))) {
  48. $contentDir = $this->getFolderContent((string) reset($contentDir));
  49. }
  50. // move files back out of the temp dir
  51. foreach ($contentDir as $file) {
  52. $file = (string) $file;
  53. $this->filesystem->rename($file, $path . '/' . basename($file));
  54. }
  55. $this->filesystem->removeDirectory($temporaryDir);
  56. if ($this->filesystem->isDirEmpty($this->config->get('vendor-dir').'/composer/')) {
  57. $this->filesystem->removeDirectory($this->config->get('vendor-dir').'/composer/');
  58. }
  59. if ($this->filesystem->isDirEmpty($this->config->get('vendor-dir'))) {
  60. $this->filesystem->removeDirectory($this->config->get('vendor-dir'));
  61. }
  62. } catch (\Exception $e) {
  63. // clean up
  64. $this->filesystem->removeDirectory($path);
  65. $this->filesystem->removeDirectory($temporaryDir);
  66. // retry downloading if we have an invalid zip file
  67. if ($retries && $e instanceof \UnexpectedValueException && class_exists('ZipArchive') && $e->getCode() === \ZipArchive::ER_NOZIP) {
  68. if ($this->io->isDebug()) {
  69. $this->io->writeError(' Invalid zip file ('.$e->getMessage().'), retrying...');
  70. } else {
  71. $this->io->writeError(' Invalid zip file, retrying...');
  72. }
  73. usleep(500000);
  74. continue;
  75. }
  76. throw $e;
  77. }
  78. break;
  79. }
  80. $this->io->writeError('');
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. protected function getFileName(PackageInterface $package, $path)
  86. {
  87. return rtrim($path.'/'.md5($path.spl_object_hash($package)).'.'.pathinfo(parse_url($package->getDistUrl(), PHP_URL_PATH), PATHINFO_EXTENSION), '.');
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. protected function processUrl(PackageInterface $package, $url)
  93. {
  94. if ($package->getDistReference() && strpos($url, 'github.com')) {
  95. if (preg_match('{^https?://(?:www\.)?github\.com/([^/]+)/([^/]+)/(zip|tar)ball/(.+)$}i', $url, $match)) {
  96. // update legacy github archives to API calls with the proper reference
  97. $url = 'https://api.github.com/repos/' . $match[1] . '/'. $match[2] . '/' . $match[3] . 'ball/' . $package->getDistReference();
  98. } elseif ($package->getDistReference() && preg_match('{^https?://(?:www\.)?github\.com/([^/]+)/([^/]+)/archive/.+\.(zip|tar)(?:\.gz)?$}i', $url, $match)) {
  99. // update current github web archives to API calls with the proper reference
  100. $url = 'https://api.github.com/repos/' . $match[1] . '/'. $match[2] . '/' . $match[3] . 'ball/' . $package->getDistReference();
  101. } elseif ($package->getDistReference() && preg_match('{^https?://api\.github\.com/repos/([^/]+)/([^/]+)/(zip|tar)ball(?:/.+)?$}i', $url, $match)) {
  102. // update api archives to the proper reference
  103. $url = 'https://api.github.com/repos/' . $match[1] . '/'. $match[2] . '/' . $match[3] . 'ball/' . $package->getDistReference();
  104. }
  105. } elseif ($package->getDistReference() && strpos($url, 'bitbucket.org')) {
  106. if (preg_match('{^https?://(?:www\.)?bitbucket\.org/([^/]+)/([^/]+)/get/(.+)\.(zip|tar\.gz|tar\.bz2)$}i', $url, $match)) {
  107. // update Bitbucket archives to the proper reference
  108. $url = 'https://bitbucket.org/' . $match[1] . '/'. $match[2] . '/get/' . $package->getDistReference() . '.' . $match[4];
  109. }
  110. }
  111. return parent::processUrl($package, $url);
  112. }
  113. /**
  114. * Extract file to directory
  115. *
  116. * @param string $file Extracted file
  117. * @param string $path Directory
  118. *
  119. * @throws \UnexpectedValueException If can not extract downloaded file to path
  120. */
  121. abstract protected function extract($file, $path);
  122. /**
  123. * Returns the folder content, excluding dotfiles
  124. *
  125. * @param string $dir Directory
  126. * @return \SplFileInfo[]
  127. */
  128. private function getFolderContent($dir)
  129. {
  130. $finder = Finder::create()
  131. ->ignoreVCS(false)
  132. ->ignoreDotFiles(false)
  133. ->depth(0)
  134. ->in($dir);
  135. return iterator_to_array($finder);
  136. }
  137. }