DownloadManager.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. * @return DownloaderInterface
  91. *
  92. * @throws \InvalidArgumentException if downloader for provided type is not registered
  93. */
  94. public function getDownloader($type)
  95. {
  96. $type = strtolower($type);
  97. if (!isset($this->downloaders[$type])) {
  98. throw new \InvalidArgumentException(sprintf('Unknown downloader type: %s. Available types: %s.', $type, implode(', ', array_keys($this->downloaders))));
  99. }
  100. return $this->downloaders[$type];
  101. }
  102. /**
  103. * Returns downloader for already installed package.
  104. *
  105. * @param PackageInterface $package package instance
  106. * @return DownloaderInterface|null
  107. *
  108. * @throws \InvalidArgumentException if package has no installation source specified
  109. * @throws \LogicException if specific downloader used to load package with
  110. * wrong type
  111. */
  112. public function getDownloaderForInstalledPackage(PackageInterface $package)
  113. {
  114. $installationSource = $package->getInstallationSource();
  115. if ('metapackage' === $package->getType()) {
  116. return;
  117. }
  118. if ('dist' === $installationSource) {
  119. $downloader = $this->getDownloader($package->getDistType());
  120. } elseif ('source' === $installationSource) {
  121. $downloader = $this->getDownloader($package->getSourceType());
  122. } else {
  123. throw new \InvalidArgumentException(
  124. 'Package '.$package.' seems not been installed properly'
  125. );
  126. }
  127. if ($installationSource !== $downloader->getInstallationSource()) {
  128. throw new \LogicException(sprintf(
  129. 'Downloader "%s" is a %s type downloader and can not be used to download %s',
  130. get_class($downloader), $downloader->getInstallationSource(), $installationSource
  131. ));
  132. }
  133. return $downloader;
  134. }
  135. /**
  136. * Downloads package into target dir.
  137. *
  138. * @param PackageInterface $package package instance
  139. * @param string $targetDir target dir
  140. * @param bool $preferSource prefer installation from source
  141. *
  142. * @throws \InvalidArgumentException if package have no urls to download from
  143. */
  144. public function download(PackageInterface $package, $targetDir, $preferSource = null)
  145. {
  146. $preferSource = null !== $preferSource ? $preferSource : $this->preferSource;
  147. $sourceType = $package->getSourceType();
  148. $distType = $package->getDistType();
  149. if ((!$package->isDev() || $this->preferDist || !$sourceType) && !($preferSource && $sourceType) && $distType) {
  150. $package->setInstallationSource('dist');
  151. } elseif ($sourceType) {
  152. $package->setInstallationSource('source');
  153. } else {
  154. throw new \InvalidArgumentException('Package '.$package.' must have a source or dist specified');
  155. }
  156. $this->filesystem->ensureDirectoryExists($targetDir);
  157. $downloader = $this->getDownloaderForInstalledPackage($package);
  158. $downloader->download($package, $targetDir);
  159. }
  160. /**
  161. * Updates package from initial to target version.
  162. *
  163. * @param PackageInterface $initial initial package version
  164. * @param PackageInterface $target target package version
  165. * @param string $targetDir target dir
  166. *
  167. * @throws \InvalidArgumentException if initial package is not installed
  168. */
  169. public function update(PackageInterface $initial, PackageInterface $target, $targetDir)
  170. {
  171. $downloader = $this->getDownloaderForInstalledPackage($initial);
  172. $installationSource = $initial->getInstallationSource();
  173. if ('dist' === $installationSource) {
  174. $initialType = $initial->getDistType();
  175. $targetType = $target->getDistType();
  176. } else {
  177. $initialType = $initial->getSourceType();
  178. $targetType = $target->getSourceType();
  179. }
  180. // upgrading from a dist stable package to a dev package, force source reinstall
  181. if ($target->isDev() && 'dist' === $installationSource) {
  182. $downloader->remove($initial, $targetDir);
  183. $this->download($target, $targetDir);
  184. return;
  185. }
  186. if ($initialType === $targetType) {
  187. $target->setInstallationSource($installationSource);
  188. $downloader->update($initial, $target, $targetDir);
  189. } else {
  190. $downloader->remove($initial, $targetDir);
  191. $this->download($target, $targetDir, 'source' === $installationSource);
  192. }
  193. }
  194. /**
  195. * Removes package from target dir.
  196. *
  197. * @param PackageInterface $package package instance
  198. * @param string $targetDir target dir
  199. */
  200. public function remove(PackageInterface $package, $targetDir)
  201. {
  202. $downloader = $this->getDownloaderForInstalledPackage($package);
  203. $downloader->remove($package, $targetDir);
  204. }
  205. }