FileDownloader.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 files
  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. class FileDownloader implements DownloaderInterface
  24. {
  25. protected $io;
  26. protected $rfs;
  27. protected $filesystem;
  28. /**
  29. * Constructor.
  30. *
  31. * @param IOInterface $io The IO instance
  32. */
  33. public function __construct(IOInterface $io, RemoteFilesystem $rfs = null, Filesystem $filesystem = null)
  34. {
  35. $this->io = $io;
  36. $this->rfs = $rfs ?: new RemoteFilesystem($io);
  37. $this->filesystem = $filesystem ?: new Filesystem();
  38. }
  39. /**
  40. * {@inheritDoc}
  41. */
  42. public function getInstallationSource()
  43. {
  44. return 'dist';
  45. }
  46. /**
  47. * {@inheritDoc}
  48. */
  49. public function download(PackageInterface $package, $path)
  50. {
  51. $url = $package->getDistUrl();
  52. if (!$url) {
  53. throw new \InvalidArgumentException('The given package is missing url information');
  54. }
  55. $this->filesystem->ensureDirectoryExists($path);
  56. $fileName = $this->getFileName($package, $path);
  57. $this->io->write(" - Package <info>" . $package->getName() . "</info> (<comment>" . $package->getPrettyVersion() . "</comment>)");
  58. $processUrl = $this->processUrl($url);
  59. try {
  60. $this->rfs->copy($package->getSourceUrl(), $processUrl, $fileName);
  61. if (!file_exists($fileName)) {
  62. throw new \UnexpectedValueException($url.' could not be saved to '.$fileName.', make sure the'
  63. .' directory is writable and you have internet connectivity');
  64. }
  65. $checksum = $package->getDistSha1Checksum();
  66. if ($checksum && hash_file('sha1', $fileName) !== $checksum) {
  67. throw new \UnexpectedValueException('The checksum verification of the file failed (downloaded from '.$url.')');
  68. }
  69. } catch (\Exception $e) {
  70. // clean up
  71. $this->filesystem->removeDirectory($path);
  72. throw $e;
  73. }
  74. }
  75. /**
  76. * {@inheritDoc}
  77. */
  78. public function update(PackageInterface $initial, PackageInterface $target, $path)
  79. {
  80. $this->remove($initial, $path);
  81. $this->download($target, $path);
  82. }
  83. /**
  84. * {@inheritDoc}
  85. */
  86. public function remove(PackageInterface $package, $path)
  87. {
  88. if (!$this->filesystem->removeDirectory($path)) {
  89. throw new \RuntimeException('Could not completely delete '.$path.', aborting.');
  90. }
  91. }
  92. /**
  93. * Gets file name for specific package
  94. *
  95. * @param PackageInterface $package package instance
  96. * @param string $path download path
  97. * @return string file name
  98. */
  99. protected function getFileName(PackageInterface $package, $path)
  100. {
  101. return $path.'/'.pathinfo($package->getDistUrl(), PATHINFO_BASENAME);
  102. }
  103. /**
  104. * Process the download url
  105. *
  106. * @param string $url download url
  107. * @return string url
  108. *
  109. * @throws \RuntimeException If any problem with the url
  110. */
  111. protected function processUrl($url)
  112. {
  113. if (!extension_loaded('openssl') && 0 === strpos($url, 'https:')) {
  114. throw new \RuntimeException('You must enable the openssl extension to download files via https');
  115. }
  116. return $url;
  117. }
  118. }