ArchiveDownloader.php 3.1 KB

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