DownloadManager.php 6.6 KB

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