PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Atomic Projects/vendor/composer/composer/src/Composer/Downloader/VcsDownloader.php

https://gitlab.com/imamul68e/137619_PHP31
PHP | 284 lines | 162 code | 34 blank | 88 comment | 27 complexity | 87301c234b890d7004beb29771aaf556 MD5 | raw file
  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>)");
  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. if (0 === strpos($url, $needle)) {
  65. $url = substr($url, strlen($needle));
  66. }
  67. $url = realpath($url);
  68. }
  69. $this->doDownload($package, $path, $url);
  70. break;
  71. } catch (\Exception $e) {
  72. // rethrow phpunit exceptions to avoid hard to debug bug failures
  73. if ($e instanceof \PHPUnit_Framework_Exception) {
  74. throw $e;
  75. }
  76. if ($this->io->isDebug()) {
  77. $this->io->writeError('Failed: ['.get_class($e).'] '.$e->getMessage());
  78. } elseif (count($urls)) {
  79. $this->io->writeError(' Failed, trying the next URL');
  80. }
  81. if (!count($urls)) {
  82. throw $e;
  83. }
  84. }
  85. }
  86. $this->io->writeError('');
  87. }
  88. /**
  89. * {@inheritDoc}
  90. */
  91. public function update(PackageInterface $initial, PackageInterface $target, $path)
  92. {
  93. if (!$target->getSourceReference()) {
  94. throw new \InvalidArgumentException('Package '.$target->getPrettyName().' is missing reference information');
  95. }
  96. $name = $target->getName();
  97. if ($initial->getPrettyVersion() == $target->getPrettyVersion()) {
  98. if ($target->getSourceType() === 'svn') {
  99. $from = $initial->getSourceReference();
  100. $to = $target->getSourceReference();
  101. } else {
  102. $from = substr($initial->getSourceReference(), 0, 7);
  103. $to = substr($target->getSourceReference(), 0, 7);
  104. }
  105. $name .= ' '.$initial->getPrettyVersion();
  106. } else {
  107. $from = $initial->getFullPrettyVersion();
  108. $to = $target->getFullPrettyVersion();
  109. }
  110. $this->io->writeError(" - Updating <info>" . $name . "</info> (<comment>" . $from . "</comment> => <comment>" . $to . "</comment>)");
  111. $this->cleanChanges($initial, $path, true);
  112. $urls = $target->getSourceUrls();
  113. $exception = null;
  114. while ($url = array_shift($urls)) {
  115. try {
  116. if (Filesystem::isLocalPath($url)) {
  117. $url = realpath($url);
  118. }
  119. $this->doUpdate($initial, $target, $path, $url);
  120. $exception = null;
  121. break;
  122. } catch (\Exception $exception) {
  123. // rethrow phpunit exceptions to avoid hard to debug bug failures
  124. if ($exception instanceof \PHPUnit_Framework_Exception) {
  125. throw $exception;
  126. }
  127. if ($this->io->isDebug()) {
  128. $this->io->writeError('Failed: ['.get_class($exception).'] '.$exception->getMessage());
  129. } elseif (count($urls)) {
  130. $this->io->writeError(' Failed, trying the next URL');
  131. }
  132. }
  133. }
  134. $this->reapplyChanges($path);
  135. // print the commit logs if in verbose mode and VCS metadata is present
  136. // because in case of missing metadata code would trigger another exception
  137. if (!$exception && $this->io->isVerbose() && $this->hasMetadataRepository($path)) {
  138. $message = 'Pulling in changes:';
  139. $logs = $this->getCommitLogs($initial->getSourceReference(), $target->getSourceReference(), $path);
  140. if (!trim($logs)) {
  141. $message = 'Rolling back changes:';
  142. $logs = $this->getCommitLogs($target->getSourceReference(), $initial->getSourceReference(), $path);
  143. }
  144. if (trim($logs)) {
  145. $logs = implode("\n", array_map(function ($line) {
  146. return ' ' . $line;
  147. }, explode("\n", $logs)));
  148. // escape angle brackets for proper output in the console
  149. $logs = str_replace('<', '\<', $logs);
  150. $this->io->writeError(' '.$message);
  151. $this->io->writeError($logs);
  152. }
  153. }
  154. if (!$urls && $exception) {
  155. throw $exception;
  156. }
  157. $this->io->writeError('');
  158. }
  159. /**
  160. * {@inheritDoc}
  161. */
  162. public function remove(PackageInterface $package, $path)
  163. {
  164. $this->io->writeError(" - Removing <info>" . $package->getName() . "</info> (<comment>" . $package->getPrettyVersion() . "</comment>)");
  165. $this->cleanChanges($package, $path, false);
  166. if (!$this->filesystem->removeDirectory($path)) {
  167. throw new \RuntimeException('Could not completely delete '.$path.', aborting.');
  168. }
  169. }
  170. /**
  171. * Download progress information is not available for all VCS downloaders.
  172. * {@inheritDoc}
  173. */
  174. public function setOutputProgress($outputProgress)
  175. {
  176. return $this;
  177. }
  178. /**
  179. * {@inheritDoc}
  180. */
  181. public function getVcsReference(PackageInterface $package, $path)
  182. {
  183. $parser = new VersionParser;
  184. $guesser = new VersionGuesser($this->config, $this->process, $parser);
  185. $dumper = new ArrayDumper;
  186. $packageConfig = $dumper->dump($package);
  187. if ($packageVersion = $guesser->guessVersion($packageConfig, $path)) {
  188. return $packageVersion['commit'];
  189. }
  190. }
  191. /**
  192. * Prompt the user to check if changes should be stashed/removed or the operation aborted
  193. *
  194. * @param PackageInterface $package
  195. * @param string $path
  196. * @param bool $update if true (update) the changes can be stashed and reapplied after an update,
  197. * if false (remove) the changes should be assumed to be lost if the operation is not aborted
  198. * @throws \RuntimeException in case the operation must be aborted
  199. */
  200. protected function cleanChanges(PackageInterface $package, $path, $update)
  201. {
  202. // the default implementation just fails if there are any changes, override in child classes to provide stash-ability
  203. if (null !== $this->getLocalChanges($package, $path)) {
  204. throw new \RuntimeException('Source directory ' . $path . ' has uncommitted changes.');
  205. }
  206. }
  207. /**
  208. * Guarantee that no changes have been made to the local copy
  209. *
  210. * @param string $path
  211. * @throws \RuntimeException in case the operation must be aborted or the patch does not apply cleanly
  212. */
  213. protected function reapplyChanges($path)
  214. {
  215. }
  216. /**
  217. * Downloads specific package into specific folder.
  218. *
  219. * @param PackageInterface $package package instance
  220. * @param string $path download path
  221. * @param string $url package url
  222. */
  223. abstract protected function doDownload(PackageInterface $package, $path, $url);
  224. /**
  225. * Updates specific package in specific folder from initial to target version.
  226. *
  227. * @param PackageInterface $initial initial package
  228. * @param PackageInterface $target updated package
  229. * @param string $path download path
  230. * @param string $url package url
  231. */
  232. abstract protected function doUpdate(PackageInterface $initial, PackageInterface $target, $path, $url);
  233. /**
  234. * Fetches the commit logs between two commits
  235. *
  236. * @param string $fromReference the source reference
  237. * @param string $toReference the target reference
  238. * @param string $path the package path
  239. * @return string
  240. */
  241. abstract protected function getCommitLogs($fromReference, $toReference, $path);
  242. /**
  243. * Checks if VCS metadata repository has been initialized
  244. * repository example: .git|.svn|.hg
  245. *
  246. * @param string $path
  247. * @return bool
  248. */
  249. abstract protected function hasMetadataRepository($path);
  250. }