DownloadManager.php 7.3 KB

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