DownloadManager.php 7.4 KB

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