FileDownloader.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. throw new \RuntimeException('You must enable the openssl extension to download files via https');
  47. }
  48. copy($url, $fileName);
  49. if (!file_exists($fileName)) {
  50. throw new \UnexpectedValueException($url.' could not be saved to '.$fileName.', make sure the'
  51. .' directory is writable and you have internet connectivity');
  52. }
  53. if ($checksum && hash_file('sha1', $fileName) !== $checksum) {
  54. throw new \UnexpectedValueException('The checksum verification of the archive failed (downloaded from '.$url.')');
  55. }
  56. echo 'Unpacking archive'.PHP_EOL;
  57. $this->extract($fileName, $path);
  58. echo 'Cleaning up'.PHP_EOL;
  59. unlink($fileName);
  60. // If we have only a one dir inside it suppose to be a package itself
  61. $contentDir = glob($path . '/*');
  62. if (1 === count($contentDir)) {
  63. $contentDir = $contentDir[0];
  64. foreach (array_merge(glob($contentDir . '/.*'), glob($contentDir . '/*')) as $file) {
  65. if (trim(basename($file), '.')) {
  66. rename($file, $path . '/' . basename($file));
  67. }
  68. }
  69. rmdir($contentDir);
  70. }
  71. }
  72. /**
  73. * {@inheritDoc}
  74. */
  75. public function update(PackageInterface $initial, PackageInterface $target, $path)
  76. {
  77. $fs = new Util\Filesystem();
  78. $fs->remove($path);
  79. $this->download($target, $path);
  80. }
  81. /**
  82. * {@inheritDoc}
  83. */
  84. public function remove(PackageInterface $package, $path)
  85. {
  86. $fs = new Util\Filesystem();
  87. $fs->remove($path);
  88. }
  89. /**
  90. * Extract file to directory
  91. *
  92. * @param string $file Extracted file
  93. * @param string $path Directory
  94. */
  95. protected abstract function extract($file, $path);
  96. }