VcsDownloader.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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\Config;
  13. use Composer\Package\Dumper\ArrayDumper;
  14. use Composer\Package\PackageInterface;
  15. use Composer\Package\Version\VersionGuesser;
  16. use Composer\Package\Version\VersionParser;
  17. use Composer\Util\ProcessExecutor;
  18. use Composer\IO\IOInterface;
  19. use Composer\Util\Filesystem;
  20. /**
  21. * @author Jordi Boggiano <j.boggiano@seld.be>
  22. */
  23. abstract class VcsDownloader implements DownloaderInterface, ChangeReportInterface, VcsCapableDownloaderInterface
  24. {
  25. /** @var IOInterface */
  26. protected $io;
  27. /** @var Config */
  28. protected $config;
  29. /** @var ProcessExecutor */
  30. protected $process;
  31. /** @var Filesystem */
  32. protected $filesystem;
  33. public function __construct(IOInterface $io, Config $config, ProcessExecutor $process = null, Filesystem $fs = null)
  34. {
  35. $this->io = $io;
  36. $this->config = $config;
  37. $this->process = $process ?: new ProcessExecutor($io);
  38. $this->filesystem = $fs ?: new Filesystem($this->process);
  39. }
  40. /**
  41. * {@inheritDoc}
  42. */
  43. public function getInstallationSource()
  44. {
  45. return 'source';
  46. }
  47. /**
  48. * {@inheritDoc}
  49. */
  50. public function download(PackageInterface $package, $path)
  51. {
  52. if (!$package->getSourceReference()) {
  53. throw new \InvalidArgumentException('Package '.$package->getPrettyName().' is missing reference information');
  54. }
  55. $this->io->writeError(" - Installing <info>" . $package->getName() . "</info> (<comment>" . $package->getFullPrettyVersion() . "</comment>): ", false);
  56. $this->filesystem->emptyDirectory($path);
  57. $urls = $package->getSourceUrls();
  58. while ($url = array_shift($urls)) {
  59. try {
  60. if (Filesystem::isLocalPath($url)) {
  61. // realpath() below will not understand
  62. // url that starts with "file://"
  63. $needle = 'file://';
  64. $isFileProtocol = false;
  65. if (0 === strpos($url, $needle)) {
  66. $url = substr($url, strlen($needle));
  67. $isFileProtocol = true;
  68. }
  69. // realpath() below will not understand %20 spaces etc.
  70. if (false !== strpos($url, '%')) {
  71. $url = rawurldecode($url);
  72. }
  73. $url = realpath($url);
  74. if ($isFileProtocol) {
  75. $url = $needle . $url;
  76. }
  77. }
  78. $this->doDownload($package, $path, $url);
  79. break;
  80. } catch (\Exception $e) {
  81. // rethrow phpunit exceptions to avoid hard to debug bug failures
  82. if ($e instanceof \PHPUnit_Framework_Exception) {
  83. throw $e;
  84. }
  85. if ($this->io->isDebug()) {
  86. $this->io->writeError('Failed: ['.get_class($e).'] '.$e->getMessage());
  87. } elseif (count($urls)) {
  88. $this->io->writeError(' Failed, trying the next URL');
  89. }
  90. if (!count($urls)) {
  91. throw $e;
  92. }
  93. }
  94. }
  95. }
  96. /**
  97. * {@inheritDoc}
  98. */
  99. public function update(PackageInterface $initial, PackageInterface $target, $path)
  100. {
  101. if (!$target->getSourceReference()) {
  102. throw new \InvalidArgumentException('Package '.$target->getPrettyName().' is missing reference information');
  103. }
  104. $name = $target->getName();
  105. if ($initial->getPrettyVersion() == $target->getPrettyVersion()) {
  106. if ($target->getSourceType() === 'svn') {
  107. $from = $initial->getSourceReference();
  108. $to = $target->getSourceReference();
  109. } else {
  110. $from = substr($initial->getSourceReference(), 0, 7);
  111. $to = substr($target->getSourceReference(), 0, 7);
  112. }
  113. $name .= ' '.$initial->getPrettyVersion();
  114. } else {
  115. $from = $initial->getFullPrettyVersion();
  116. $to = $target->getFullPrettyVersion();
  117. }
  118. $this->io->writeError(" - Updating <info>" . $name . "</info> (<comment>" . $from . "</comment> => <comment>" . $to . "</comment>): ", false);
  119. $this->cleanChanges($initial, $path, true);
  120. $urls = $target->getSourceUrls();
  121. $exception = null;
  122. while ($url = array_shift($urls)) {
  123. try {
  124. if (Filesystem::isLocalPath($url)) {
  125. $url = realpath($url);
  126. }
  127. $this->doUpdate($initial, $target, $path, $url);
  128. $exception = null;
  129. break;
  130. } catch (\Exception $exception) {
  131. // rethrow phpunit exceptions to avoid hard to debug bug failures
  132. if ($exception instanceof \PHPUnit_Framework_Exception) {
  133. throw $exception;
  134. }
  135. if ($this->io->isDebug()) {
  136. $this->io->writeError('Failed: ['.get_class($exception).'] '.$exception->getMessage());
  137. } elseif (count($urls)) {
  138. $this->io->writeError(' Failed, trying the next URL');
  139. }
  140. }
  141. }
  142. $this->reapplyChanges($path);
  143. // print the commit logs if in verbose mode and VCS metadata is present
  144. // because in case of missing metadata code would trigger another exception
  145. if (!$exception && $this->io->isVerbose() && $this->hasMetadataRepository($path)) {
  146. $message = 'Pulling in changes:';
  147. $logs = $this->getCommitLogs($initial->getSourceReference(), $target->getSourceReference(), $path);
  148. if (!trim($logs)) {
  149. $message = 'Rolling back changes:';
  150. $logs = $this->getCommitLogs($target->getSourceReference(), $initial->getSourceReference(), $path);
  151. }
  152. if (trim($logs)) {
  153. $logs = implode("\n", array_map(function ($line) {
  154. return ' ' . $line;
  155. }, explode("\n", $logs)));
  156. // escape angle brackets for proper output in the console
  157. $logs = str_replace('<', '\<', $logs);
  158. $this->io->writeError(' '.$message);
  159. $this->io->writeError($logs);
  160. }
  161. }
  162. if (!$urls && $exception) {
  163. throw $exception;
  164. }
  165. }
  166. /**
  167. * {@inheritDoc}
  168. */
  169. public function remove(PackageInterface $package, $path)
  170. {
  171. $this->io->writeError(" - Removing <info>" . $package->getName() . "</info> (<comment>" . $package->getPrettyVersion() . "</comment>)");
  172. $this->cleanChanges($package, $path, false);
  173. if (!$this->filesystem->removeDirectory($path)) {
  174. throw new \RuntimeException('Could not completely delete '.$path.', aborting.');
  175. }
  176. }
  177. /**
  178. * Download progress information is not available for all VCS downloaders.
  179. * {@inheritDoc}
  180. */
  181. public function setOutputProgress($outputProgress)
  182. {
  183. return $this;
  184. }
  185. /**
  186. * {@inheritDoc}
  187. */
  188. public function getVcsReference(PackageInterface $package, $path)
  189. {
  190. $parser = new VersionParser;
  191. $guesser = new VersionGuesser($this->config, $this->process, $parser);
  192. $dumper = new ArrayDumper;
  193. $packageConfig = $dumper->dump($package);
  194. if ($packageVersion = $guesser->guessVersion($packageConfig, $path)) {
  195. return $packageVersion['commit'];
  196. }
  197. }
  198. /**
  199. * Prompt the user to check if changes should be stashed/removed or the operation aborted
  200. *
  201. * @param PackageInterface $package
  202. * @param string $path
  203. * @param bool $update if true (update) the changes can be stashed and reapplied after an update,
  204. * if false (remove) the changes should be assumed to be lost if the operation is not aborted
  205. * @throws \RuntimeException in case the operation must be aborted
  206. */
  207. protected function cleanChanges(PackageInterface $package, $path, $update)
  208. {
  209. // the default implementation just fails if there are any changes, override in child classes to provide stash-ability
  210. if (null !== $this->getLocalChanges($package, $path)) {
  211. throw new \RuntimeException('Source directory ' . $path . ' has uncommitted changes.');
  212. }
  213. }
  214. /**
  215. * Guarantee that no changes have been made to the local copy
  216. *
  217. * @param string $path
  218. * @throws \RuntimeException in case the operation must be aborted or the patch does not apply cleanly
  219. */
  220. protected function reapplyChanges($path)
  221. {
  222. }
  223. /**
  224. * Downloads specific package into specific folder.
  225. *
  226. * @param PackageInterface $package package instance
  227. * @param string $path download path
  228. * @param string $url package url
  229. */
  230. abstract protected function doDownload(PackageInterface $package, $path, $url);
  231. /**
  232. * Updates specific package in specific folder from initial to target version.
  233. *
  234. * @param PackageInterface $initial initial package
  235. * @param PackageInterface $target updated package
  236. * @param string $path download path
  237. * @param string $url package url
  238. */
  239. abstract protected function doUpdate(PackageInterface $initial, PackageInterface $target, $path, $url);
  240. /**
  241. * Fetches the commit logs between two commits
  242. *
  243. * @param string $fromReference the source reference
  244. * @param string $toReference the target reference
  245. * @param string $path the package path
  246. * @return string
  247. */
  248. abstract protected function getCommitLogs($fromReference, $toReference, $path);
  249. /**
  250. * Checks if VCS metadata repository has been initialized
  251. * repository example: .git|.svn|.hg
  252. *
  253. * @param string $path
  254. * @return bool
  255. */
  256. abstract protected function hasMetadataRepository($path);
  257. }