ArchiveDownloader.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. /**
  14. * Base downloader for archives
  15. *
  16. * @author Kirill chEbba Chebunin <iam@chebba.org>
  17. * @author Jordi Boggiano <j.boggiano@seld.be>
  18. * @author François Pluchino <francois.pluchino@opendisplay.com>
  19. */
  20. abstract class ArchiveDownloader extends FileDownloader
  21. {
  22. /**
  23. * {@inheritDoc}
  24. */
  25. public function download(PackageInterface $package, $path)
  26. {
  27. parent::download($package, $path);
  28. $fileName = $this->getFileName($package, $path);
  29. if ($this->io->isVerbose()) {
  30. $this->io->write(' Unpacking archive');
  31. }
  32. try {
  33. try {
  34. $this->extract($fileName, $path);
  35. } catch (\Exception $e) {
  36. // remove cache if the file was corrupted
  37. parent::clearCache($package, $path);
  38. throw $e;
  39. }
  40. if ($this->io->isVerbose()) {
  41. $this->io->write(' Cleaning up');
  42. }
  43. unlink($fileName);
  44. // If we have only a one dir inside it suppose to be a package itself
  45. $contentDir = glob($path . '/*');
  46. if (1 === count($contentDir)) {
  47. $contentDir = $contentDir[0];
  48. if (is_file($contentDir)) {
  49. $this->filesystem->rename($contentDir, $path . '/' . basename($contentDir));
  50. } else {
  51. // Rename the content directory to avoid error when moving up
  52. // a child folder with the same name
  53. $temporaryDir = sys_get_temp_dir().'/'.md5(time().rand());
  54. $this->filesystem->rename($contentDir, $temporaryDir);
  55. $contentDir = $temporaryDir;
  56. foreach (array_merge(glob($contentDir . '/.*'), glob($contentDir . '/*')) as $file) {
  57. if (trim(basename($file), '.')) {
  58. $this->filesystem->rename($file, $path . '/' . basename($file));
  59. }
  60. }
  61. $this->filesystem->removeDirectory($contentDir);
  62. }
  63. }
  64. } catch (\Exception $e) {
  65. // clean up
  66. $this->filesystem->removeDirectory($path);
  67. throw $e;
  68. }
  69. $this->io->write('');
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. protected function getFileName(PackageInterface $package, $path)
  75. {
  76. return rtrim($path.'/'.md5($path.spl_object_hash($package)).'.'.pathinfo($package->getDistUrl(), PATHINFO_EXTENSION), '.');
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. protected function processUrl(PackageInterface $package, $url)
  82. {
  83. // support for legacy github archives
  84. if ($package->getDistReference() && preg_match('{^https?://(?:www\.)?github\.com/([^/]+)/([^/]+)/(zip|tar)ball/(.+)$}i', $url, $match)) {
  85. $url = 'https://github.com/' . $match[1] . '/'. $match[2] . '/' . $match[3] . 'ball/' . $package->getDistReference();
  86. }
  87. if ($package->getDistReference() && preg_match('{^https?://(?:www\.)?github\.com/([^/]+)/([^/]+)/archive/.+\.(zip|tar\.gz)$}i', $url, $match)) {
  88. $url = 'https://github.com/' . $match[1] . '/'. $match[2] . '/archive/' . $package->getDistReference() . '.' . $match[3];
  89. }
  90. if (!extension_loaded('openssl') && (0 === strpos($url, 'https:') || 0 === strpos($url, 'http://github.com'))) {
  91. // bypass https for github if openssl is disabled
  92. if (preg_match('{^https?://github.com/([^/]+/[^/]+)/(zip|tar)ball/([^/]+)$}i', $url, $match)) {
  93. $url = 'http://nodeload.github.com/'.$match[1].'/'.$match[2].'/'.$match[3];
  94. } elseif (preg_match('{^https?://github.com/([^/]+/[^/]+)/archive/([^/]+)\.(zip|tar\.gz)$}i', $url, $match)) {
  95. $url = 'http://nodeload.github.com/'.$match[1].'/'.$match[3].'/'.$match[2];
  96. } else {
  97. throw new \RuntimeException('You must enable the openssl extension to download files via https');
  98. }
  99. }
  100. return parent::processUrl($package, $url);
  101. }
  102. /**
  103. * Extract file to directory
  104. *
  105. * @param string $file Extracted file
  106. * @param string $path Directory
  107. *
  108. * @throws \UnexpectedValueException If can not extract downloaded file to path
  109. */
  110. abstract protected function extract($file, $path);
  111. }