FileDownloader.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. /**
  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. if (!$url) {
  49. throw new \InvalidArgumentException('The given package is missing url information');
  50. }
  51. if (!is_dir($path)) {
  52. if (file_exists($path)) {
  53. throw new \UnexpectedValueException($path.' exists and is not a directory');
  54. }
  55. if (!mkdir($path, 0777, true)) {
  56. throw new \UnexpectedValueException($path.' does not exist and could not be created');
  57. }
  58. }
  59. $fileName = $this->getFileName($package, $path);
  60. $this->io->write(" - Package <info>" . $package->getName() . "</info> (<comment>" . $package->getPrettyVersion() . "</comment>)");
  61. $url = $this->processUrl($url);
  62. $rfs = new RemoteFilesystem($this->io);
  63. $rfs->copy($package->getSourceUrl(), $url, $fileName);
  64. $this->io->write('');
  65. if (!file_exists($fileName)) {
  66. throw new \UnexpectedValueException($url.' could not be saved to '.$fileName.', make sure the'
  67. .' directory is writable and you have internet connectivity');
  68. }
  69. $checksum = $package->getDistSha1Checksum();
  70. if ($checksum && hash_file('sha1', $fileName) !== $checksum) {
  71. throw new \UnexpectedValueException('The checksum verification of the file failed (downloaded from '.$url.')');
  72. }
  73. }
  74. /**
  75. * {@inheritDoc}
  76. */
  77. public function update(PackageInterface $initial, PackageInterface $target, $path)
  78. {
  79. $this->remove($initial, $path);
  80. $this->download($target, $path);
  81. }
  82. /**
  83. * {@inheritDoc}
  84. */
  85. public function remove(PackageInterface $package, $path)
  86. {
  87. $fs = new Filesystem();
  88. $fs->removeDirectory($path);
  89. }
  90. /**
  91. * Gets file name for specific package
  92. *
  93. * @param PackageInterface $package package instance
  94. * @param string $path download path
  95. * @return string file name
  96. */
  97. protected function getFileName(PackageInterface $package, $path)
  98. {
  99. return $path.'/'.pathinfo($package->getDistUrl(), PATHINFO_BASENAME);
  100. }
  101. /**
  102. * Process the download url
  103. *
  104. * @param string $url download url
  105. * @return string url
  106. *
  107. * @throws \RuntimeException If any problem with the url
  108. */
  109. protected function processUrl($url)
  110. {
  111. if (!extension_loaded('openssl') && 0 === strpos($url, 'https:')) {
  112. throw new \RuntimeException('You must enable the openssl extension to download files via https');
  113. }
  114. return $url;
  115. }
  116. }