ArchiveDownloader.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. $this->io->writeError(' Invalid zip file, retrying...');
  69. usleep(500000);
  70. continue;
  71. }
  72. throw $e;
  73. }
  74. break;
  75. }
  76. $this->io->writeError('');
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. protected function getFileName(PackageInterface $package, $path)
  82. {
  83. return rtrim($path.'/'.md5($path.spl_object_hash($package)).'.'.pathinfo(parse_url($package->getDistUrl(), PHP_URL_PATH), PATHINFO_EXTENSION), '.');
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. protected function processUrl(PackageInterface $package, $url)
  89. {
  90. if ($package->getDistReference() && strpos($url, 'github.com')) {
  91. if (preg_match('{^https?://(?:www\.)?github\.com/([^/]+)/([^/]+)/(zip|tar)ball/(.+)$}i', $url, $match)) {
  92. // update legacy github archives to API calls with the proper reference
  93. $url = 'https://api.github.com/repos/' . $match[1] . '/'. $match[2] . '/' . $match[3] . 'ball/' . $package->getDistReference();
  94. } elseif ($package->getDistReference() && preg_match('{^https?://(?:www\.)?github\.com/([^/]+)/([^/]+)/archive/.+\.(zip|tar)(?:\.gz)?$}i', $url, $match)) {
  95. // update current github web 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?://api\.github\.com/repos/([^/]+)/([^/]+)/(zip|tar)ball(?:/.+)?$}i', $url, $match)) {
  98. // update api archives to the proper reference
  99. $url = 'https://api.github.com/repos/' . $match[1] . '/'. $match[2] . '/' . $match[3] . 'ball/' . $package->getDistReference();
  100. }
  101. } else if ($package->getDistReference() && strpos($url, 'bitbucket.org')) {
  102. if (preg_match('{^https?://(?:www\.)?bitbucket\.org/([^/]+)/([^/]+)/get/(.+)\.(zip|tar\.gz|tar\.bz2)$}i', $url, $match)) {
  103. // update Bitbucket archives to the proper reference
  104. $url = 'https://bitbucket.org/' . $match[1] . '/'. $match[2] . '/get/' . $package->getDistReference() . '.' . $match[4];
  105. }
  106. }
  107. return parent::processUrl($package, $url);
  108. }
  109. /**
  110. * Extract file to directory
  111. *
  112. * @param string $file Extracted file
  113. * @param string $path Directory
  114. *
  115. * @throws \UnexpectedValueException If can not extract downloaded file to path
  116. */
  117. abstract protected function extract($file, $path);
  118. /**
  119. * Returns the folder content, excluding dotfiles
  120. *
  121. * @param string $dir Directory
  122. * @return \SplFileInfo[]
  123. */
  124. private function getFolderContent($dir)
  125. {
  126. $finder = Finder::create()
  127. ->ignoreVCS(false)
  128. ->ignoreDotFiles(false)
  129. ->depth(0)
  130. ->in($dir);
  131. return iterator_to_array($finder);
  132. }
  133. }