DownloadManager.php 6.6 KB

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