DownloadManager.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. use Composer\Downloader\DownloaderInterface;
  14. use Composer\Util\Filesystem;
  15. /**
  16. * Downloaders manager.
  17. *
  18. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  19. */
  20. class DownloadManager
  21. {
  22. private $preferSource = false;
  23. private $filesystem;
  24. private $downloaders = array();
  25. /**
  26. * Initializes download manager.
  27. *
  28. * @param bool $preferSource prefer downloading from source
  29. */
  30. public function __construct($preferSource = false, Filesystem $filesystem = null)
  31. {
  32. $this->preferSource = $preferSource;
  33. $this->filesystem = $filesystem ?: new Filesystem();
  34. }
  35. /**
  36. * Makes downloader prefer source installation over the dist.
  37. *
  38. * @param bool $preferSource prefer downloading from source
  39. */
  40. public function setPreferSource($preferSource)
  41. {
  42. $this->preferSource = $preferSource;
  43. return $this;
  44. }
  45. /**
  46. * Sets installer downloader for a specific installation type.
  47. *
  48. * @param string $type installation type
  49. * @param DownloaderInterface $downloader downloader instance
  50. */
  51. public function setDownloader($type, DownloaderInterface $downloader)
  52. {
  53. $type = strtolower($type);
  54. $this->downloaders[$type] = $downloader;
  55. return $this;
  56. }
  57. /**
  58. * Returns downloader for a specific installation type.
  59. *
  60. * @param string $type installation type
  61. *
  62. * @return DownloaderInterface
  63. *
  64. * @throws UnexpectedValueException if downloader for provided type is not registeterd
  65. */
  66. public function getDownloader($type)
  67. {
  68. $type = strtolower($type);
  69. if (!isset($this->downloaders[$type])) {
  70. throw new \InvalidArgumentException('Unknown downloader type: '.$type);
  71. }
  72. return $this->downloaders[$type];
  73. }
  74. /**
  75. * Returns downloader for already installed package.
  76. *
  77. * @param PackageInterface $package package instance
  78. *
  79. * @return DownloaderInterface
  80. *
  81. * @throws InvalidArgumentException if package has no installation source specified
  82. * @throws LogicException if specific downloader used to load package with
  83. * wrong type
  84. */
  85. public function getDownloaderForInstalledPackage(PackageInterface $package)
  86. {
  87. $installationSource = $package->getInstallationSource();
  88. if ('dist' === $installationSource) {
  89. $downloader = $this->getDownloader($package->getDistType());
  90. } elseif ('source' === $installationSource) {
  91. $downloader = $this->getDownloader($package->getSourceType());
  92. } else {
  93. throw new \InvalidArgumentException(
  94. 'Package '.$package.' seems not been installed properly'
  95. );
  96. }
  97. if ($installationSource !== $downloader->getInstallationSource()) {
  98. throw new \LogicException(sprintf(
  99. 'Downloader "%s" is a %s type downloader and can not be used to download %s',
  100. get_class($downloader), $downloader->getInstallationSource(), $installationSource
  101. ));
  102. }
  103. return $downloader;
  104. }
  105. /**
  106. * Downloads package into target dir.
  107. *
  108. * @param PackageInterface $package package instance
  109. * @param string $targetDir target dir
  110. * @param bool $preferSource prefer installation from source
  111. *
  112. * @throws InvalidArgumentException if package have no urls to download from
  113. */
  114. public function download(PackageInterface $package, $targetDir, $preferSource = null)
  115. {
  116. $preferSource = null !== $preferSource ? $preferSource : $this->preferSource;
  117. $sourceType = $package->getSourceType();
  118. $distType = $package->getDistType();
  119. if (!$package->isDev() && !($preferSource && $sourceType) && $distType) {
  120. $package->setInstallationSource('dist');
  121. } elseif ($sourceType) {
  122. $package->setInstallationSource('source');
  123. } elseif ($package->isDev()) {
  124. throw new \InvalidArgumentException('Dev package '.$package.' must have a source specified');
  125. } else {
  126. throw new \InvalidArgumentException('Package '.$package.' must have a source or dist specified');
  127. }
  128. $this->filesystem->ensureDirectoryExists($targetDir);
  129. $downloader = $this->getDownloaderForInstalledPackage($package);
  130. $downloader->download($package, $targetDir);
  131. }
  132. /**
  133. * Updates package from initial to target version.
  134. *
  135. * @param PackageInterface $initial initial package version
  136. * @param PackageInterface $target target package version
  137. * @param string $targetDir target dir
  138. *
  139. * @throws InvalidArgumentException if initial package is not installed
  140. */
  141. public function update(PackageInterface $initial, PackageInterface $target, $targetDir)
  142. {
  143. $downloader = $this->getDownloaderForInstalledPackage($initial);
  144. $installationSource = $initial->getInstallationSource();
  145. if ('dist' === $installationSource) {
  146. $initialType = $initial->getDistType();
  147. $targetType = $target->getDistType();
  148. } else {
  149. $initialType = $initial->getSourceType();
  150. $targetType = $target->getSourceType();
  151. }
  152. // upgrading from a dist stable package to a dev package, force source reinstall
  153. if ($target->isDev() && 'dist' === $installationSource) {
  154. $downloader->remove($initial, $targetDir);
  155. $this->download($target, $targetDir);
  156. return;
  157. }
  158. if ($initialType === $targetType) {
  159. $target->setInstallationSource($installationSource);
  160. $downloader->update($initial, $target, $targetDir);
  161. } else {
  162. $downloader->remove($initial, $targetDir);
  163. $this->download($target, $targetDir, 'source' === $installationSource);
  164. }
  165. }
  166. /**
  167. * Removes package from target dir.
  168. *
  169. * @param PackageInterface $package package instance
  170. * @param string $targetDir target dir
  171. */
  172. public function remove(PackageInterface $package, $targetDir)
  173. {
  174. $downloader = $this->getDownloaderForInstalledPackage($package);
  175. $downloader->remove($package, $targetDir);
  176. }
  177. }