ArchiveDownloader.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. * @throws \RuntimeException
  27. * @throws \UnexpectedValueException
  28. */
  29. public function download(PackageInterface $package, $path, $output = true)
  30. {
  31. $temporaryDir = $this->config->get('vendor-dir').'/composer/'.substr(md5(uniqid('', true)), 0, 8);
  32. $retries = 3;
  33. while ($retries--) {
  34. $fileName = parent::download($package, $path, $output);
  35. if ($output) {
  36. $this->io->writeError(' Extracting archive', false, IOInterface::VERBOSE);
  37. }
  38. try {
  39. $this->filesystem->ensureDirectoryExists($temporaryDir);
  40. try {
  41. $this->extract($fileName, $temporaryDir);
  42. } catch (\Exception $e) {
  43. // remove cache if the file was corrupted
  44. parent::clearLastCacheWrite($package);
  45. throw $e;
  46. }
  47. $this->filesystem->unlink($fileName);
  48. $contentDir = $this->getFolderContent($temporaryDir);
  49. // only one dir in the archive, extract its contents out of it
  50. if (1 === count($contentDir) && is_dir(reset($contentDir))) {
  51. $contentDir = $this->getFolderContent((string) reset($contentDir));
  52. }
  53. // move files back out of the temp dir
  54. foreach ($contentDir as $file) {
  55. $file = (string) $file;
  56. $this->filesystem->rename($file, $path . '/' . basename($file));
  57. }
  58. $this->filesystem->removeDirectory($temporaryDir);
  59. if ($this->filesystem->isDirEmpty($this->config->get('vendor-dir').'/composer/')) {
  60. $this->filesystem->removeDirectory($this->config->get('vendor-dir').'/composer/');
  61. }
  62. if ($this->filesystem->isDirEmpty($this->config->get('vendor-dir'))) {
  63. $this->filesystem->removeDirectory($this->config->get('vendor-dir'));
  64. }
  65. } catch (\Exception $e) {
  66. // clean up
  67. $this->filesystem->removeDirectory($path);
  68. $this->filesystem->removeDirectory($temporaryDir);
  69. // retry downloading if we have an invalid zip file
  70. if ($retries && $e instanceof \UnexpectedValueException && class_exists('ZipArchive') && $e->getCode() === \ZipArchive::ER_NOZIP) {
  71. $this->io->writeError('');
  72. if ($this->io->isDebug()) {
  73. $this->io->writeError(' Invalid zip file ('.$e->getMessage().'), retrying...');
  74. } else {
  75. $this->io->writeError(' Invalid zip file, retrying...');
  76. }
  77. usleep(500000);
  78. continue;
  79. }
  80. throw $e;
  81. }
  82. break;
  83. }
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. protected function getFileName(PackageInterface $package, $path)
  89. {
  90. return rtrim($path.'/'.md5($path.spl_object_hash($package)).'.'.pathinfo(parse_url($package->getDistUrl(), PHP_URL_PATH), PATHINFO_EXTENSION), '.');
  91. }
  92. /**
  93. * Extract file to directory
  94. *
  95. * @param string $file Extracted file
  96. * @param string $path Directory
  97. *
  98. * @throws \UnexpectedValueException If can not extract downloaded file to path
  99. */
  100. abstract protected function extract($file, $path);
  101. /**
  102. * Returns the folder content, excluding dotfiles
  103. *
  104. * @param string $dir Directory
  105. * @return \SplFileInfo[]
  106. */
  107. private function getFolderContent($dir)
  108. {
  109. $finder = Finder::create()
  110. ->ignoreVCS(false)
  111. ->ignoreDotFiles(false)
  112. ->notName('.DS_Store')
  113. ->depth(0)
  114. ->in($dir);
  115. return iterator_to_array($finder);
  116. }
  117. }