ArchiveDownloader.php 6.1 KB

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