ArchiveDownloader.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. $this->extract($fileName, $path);
  34. if ($this->io->isVerbose()) {
  35. $this->io->write(' Cleaning up');
  36. }
  37. unlink($fileName);
  38. // If we have only a one dir inside it suppose to be a package itself
  39. $contentDir = glob($path . '/*');
  40. if (1 === count($contentDir)) {
  41. $contentDir = $contentDir[0];
  42. if (is_file($contentDir)) {
  43. $this->filesystem->rename($contentDir, $path . '/' . basename($contentDir));
  44. } else {
  45. // Rename the content directory to avoid error when moving up
  46. // a child folder with the same name
  47. $temporaryDir = sys_get_temp_dir().'/'.md5(time().rand());
  48. $this->filesystem->rename($contentDir, $temporaryDir);
  49. $contentDir = $temporaryDir;
  50. foreach (array_merge(glob($contentDir . '/.*'), glob($contentDir . '/*')) as $file) {
  51. if (trim(basename($file), '.')) {
  52. $this->filesystem->rename($file, $path . '/' . basename($file));
  53. }
  54. }
  55. $this->filesystem->removeDirectory($contentDir);
  56. }
  57. }
  58. } catch (\Exception $e) {
  59. // clean up
  60. $this->filesystem->removeDirectory($path);
  61. throw $e;
  62. }
  63. $this->io->write('');
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. protected function getFileName(PackageInterface $package, $path)
  69. {
  70. return rtrim($path.'/'.md5($path.spl_object_hash($package)).'.'.pathinfo($package->getDistUrl(), PATHINFO_EXTENSION), '.');
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. protected function processUrl(PackageInterface $package, $url)
  76. {
  77. if ($package->getDistReference() && preg_match('{^https?://(?:www\.)?github\.com/([^/]+)/([^/]+)/(zip|tar)ball/(.+)$}i', $url, $match)) {
  78. $url = 'https://github.com/' . $match[1] . '/'. $match[2] . '/' . $match[3] . 'ball/' . $package->getDistReference();
  79. }
  80. if (!extension_loaded('openssl') && (0 === strpos($url, 'https:') || 0 === strpos($url, 'http://github.com'))) {
  81. // bypass https for github if openssl is disabled
  82. if (preg_match('{^https?://github.com/([^/]+/[^/]+)/(zip|tar)ball/([^/]+)$}i', $url, $match)) {
  83. $url = 'http://nodeload.github.com/'.$match[1].'/legacy.'.$match[2].'/'.$match[3];
  84. } else {
  85. throw new \RuntimeException('You must enable the openssl extension to download files via https');
  86. }
  87. }
  88. return parent::processUrl($package, $url);
  89. }
  90. /**
  91. * Extract file to directory
  92. *
  93. * @param string $file Extracted file
  94. * @param string $path Directory
  95. *
  96. * @throws \UnexpectedValueException If can not extract downloaded file to path
  97. */
  98. abstract protected function extract($file, $path);
  99. }