FileDownloader.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 file packages
  15. *
  16. * @author Kirill chEbba Chebunin <iam@chebba.org>
  17. * @author Jordi Boggiano <j.boggiano@seld.be>
  18. */
  19. abstract class FileDownloader implements DownloaderInterface
  20. {
  21. /**
  22. * {@inheritDoc}
  23. */
  24. public function getInstallationSource()
  25. {
  26. return 'dist';
  27. }
  28. /**
  29. * {@inheritDoc}
  30. */
  31. public function download(PackageInterface $package, $path)
  32. {
  33. $url = $package->getDistUrl();
  34. $checksum = $package->getDistSha1Checksum();
  35. if (!is_dir($path)) {
  36. if (file_exists($path)) {
  37. throw new \UnexpectedValueException($path.' exists and is not a directory');
  38. }
  39. if (!mkdir($path, 0777, true)) {
  40. throw new \UnexpectedValueException($path.' does not exist and could not be created');
  41. }
  42. }
  43. $fileName = rtrim($path.'/'.md5(time().rand()).'.'.pathinfo($url, PATHINFO_EXTENSION), '.');
  44. echo 'Downloading '.$url.' to '.$fileName.PHP_EOL;
  45. if (0 === strpos($url, 'https:') && !extension_loaded('openssl')) {
  46. // bypass https for github if openssl is disabled
  47. if (preg_match('{^https://(github.com/[^/]+/[^/]+/(zip|tar)ball/[^/]+)$}i', $url, $match)) {
  48. $url = 'http://nodeload.'.$match[1];
  49. } else {
  50. throw new \RuntimeException('You must enable the openssl extension to download files via https');
  51. }
  52. }
  53. copy($url, $fileName);
  54. if (!file_exists($fileName)) {
  55. throw new \UnexpectedValueException($url.' could not be saved to '.$fileName.', make sure the'
  56. .' directory is writable and you have internet connectivity');
  57. }
  58. if ($checksum && hash_file('sha1', $fileName) !== $checksum) {
  59. throw new \UnexpectedValueException('The checksum verification of the archive failed (downloaded from '.$url.')');
  60. }
  61. echo 'Unpacking archive'.PHP_EOL;
  62. $this->extract($fileName, $path);
  63. echo 'Cleaning up'.PHP_EOL;
  64. unlink($fileName);
  65. // If we have only a one dir inside it suppose to be a package itself
  66. $contentDir = glob($path . '/*');
  67. if (1 === count($contentDir)) {
  68. $contentDir = $contentDir[0];
  69. foreach (array_merge(glob($contentDir . '/.*'), glob($contentDir . '/*')) as $file) {
  70. if (trim(basename($file), '.')) {
  71. rename($file, $path . '/' . basename($file));
  72. }
  73. }
  74. rmdir($contentDir);
  75. }
  76. }
  77. /**
  78. * {@inheritDoc}
  79. */
  80. public function update(PackageInterface $initial, PackageInterface $target, $path)
  81. {
  82. $fs = new Util\Filesystem();
  83. $fs->removeDirectory($path);
  84. $this->download($target, $path);
  85. }
  86. /**
  87. * {@inheritDoc}
  88. */
  89. public function remove(PackageInterface $package, $path)
  90. {
  91. $fs = new Util\Filesystem();
  92. $fs->removeDirectory($path);
  93. }
  94. /**
  95. * Extract file to directory
  96. *
  97. * @param string $file Extracted file
  98. * @param string $path Directory
  99. *
  100. * @throws \UnexpectedValueException If can not extract downloaded file to path
  101. */
  102. protected abstract function extract($file, $path);
  103. }