FileDownloader.php 3.0 KB

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