FileDownloader.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. use Composer\Util\Filesystem;
  15. use Composer\Util\RemoteFilesystem;
  16. /**
  17. * Base downloader for file packages
  18. *
  19. * @author Kirill chEbba Chebunin <iam@chebba.org>
  20. * @author Jordi Boggiano <j.boggiano@seld.be>
  21. * @author François Pluchino <francois.pluchino@opendisplay.com>
  22. */
  23. abstract class FileDownloader implements DownloaderInterface
  24. {
  25. protected $io;
  26. /**
  27. * Constructor.
  28. *
  29. * @param IOInterface $io The IO instance
  30. */
  31. public function __construct(IOInterface $io)
  32. {
  33. $this->io = $io;
  34. }
  35. /**
  36. * {@inheritDoc}
  37. */
  38. public function getInstallationSource()
  39. {
  40. return 'dist';
  41. }
  42. /**
  43. * {@inheritDoc}
  44. */
  45. public function download(PackageInterface $package, $path)
  46. {
  47. $url = $package->getDistUrl();
  48. $checksum = $package->getDistSha1Checksum();
  49. if (!is_dir($path)) {
  50. if (file_exists($path)) {
  51. throw new \UnexpectedValueException($path.' exists and is not a directory');
  52. }
  53. if (!mkdir($path, 0777, true)) {
  54. throw new \UnexpectedValueException($path.' does not exist and could not be created');
  55. }
  56. }
  57. $fileName = rtrim($path.'/'.md5(time().rand()).'.'.pathinfo($url, PATHINFO_EXTENSION), '.');
  58. $this->io->write(" - Package <info>" . $package->getName() . "</info> (<comment>" . $package->getPrettyVersion() . "</comment>)");
  59. if (!extension_loaded('openssl') && (0 === strpos($url, 'https:') || 0 === strpos($url, 'http://github.com'))) {
  60. // bypass https for github if openssl is disabled
  61. if (preg_match('{^https?://(github.com/[^/]+/[^/]+/(zip|tar)ball/[^/]+)$}i', $url, $match)) {
  62. $url = 'http://nodeload.'.$match[1];
  63. } else {
  64. throw new \RuntimeException('You must enable the openssl extension to download files via https');
  65. }
  66. }
  67. $rfs = new RemoteFilesystem($this->io);
  68. $rfs->copy($package->getSourceUrl(), $url, $fileName);
  69. $this->io->write('');
  70. if (!file_exists($fileName)) {
  71. throw new \UnexpectedValueException($url.' could not be saved to '.$fileName.', make sure the'
  72. .' directory is writable and you have internet connectivity');
  73. }
  74. if ($checksum && hash_file('sha1', $fileName) !== $checksum) {
  75. throw new \UnexpectedValueException('The checksum verification of the archive failed (downloaded from '.$url.')');
  76. }
  77. $this->io->write(' Unpacking archive');
  78. $this->extract($fileName, $path);
  79. $this->io->write(' Cleaning up');
  80. unlink($fileName);
  81. // If we have only a one dir inside it suppose to be a package itself
  82. $contentDir = glob($path . '/*');
  83. if (1 === count($contentDir)) {
  84. $contentDir = $contentDir[0];
  85. // Rename the content directory to avoid error when moving up
  86. // a child folder with the same name
  87. $temporaryName = md5(time().rand());
  88. rename($contentDir, $temporaryName);
  89. $contentDir = $temporaryName;
  90. foreach (array_merge(glob($contentDir . '/.*'), glob($contentDir . '/*')) as $file) {
  91. if (trim(basename($file), '.')) {
  92. rename($file, $path . '/' . basename($file));
  93. }
  94. }
  95. rmdir($contentDir);
  96. }
  97. $this->io->write('');
  98. }
  99. /**
  100. * {@inheritDoc}
  101. */
  102. public function update(PackageInterface $initial, PackageInterface $target, $path)
  103. {
  104. $fs = new Filesystem();
  105. $fs->removeDirectory($path);
  106. $this->download($target, $path);
  107. }
  108. /**
  109. * {@inheritDoc}
  110. */
  111. public function remove(PackageInterface $package, $path)
  112. {
  113. $fs = new Filesystem();
  114. $fs->removeDirectory($path);
  115. }
  116. /**
  117. * Extract file to directory
  118. *
  119. * @param string $file Extracted file
  120. * @param string $path Directory
  121. *
  122. * @throws \UnexpectedValueException If can not extract downloaded file to path
  123. */
  124. protected abstract function extract($file, $path);
  125. }