VcsRepository.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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\Repository;
  12. use Composer\Downloader\TransportException;
  13. use Composer\Repository\Vcs\VcsDriverInterface;
  14. use Composer\Package\Version\VersionParser;
  15. use Composer\Package\Loader\ArrayLoader;
  16. use Composer\Package\Loader\ValidatingArrayLoader;
  17. use Composer\Package\Loader\InvalidPackageException;
  18. use Composer\Package\Loader\LoaderInterface;
  19. use Composer\IO\IOInterface;
  20. use Composer\Config;
  21. /**
  22. * @author Jordi Boggiano <j.boggiano@seld.be>
  23. */
  24. class VcsRepository extends ArrayRepository
  25. {
  26. protected $url;
  27. protected $packageName;
  28. protected $verbose;
  29. protected $io;
  30. protected $config;
  31. protected $versionParser;
  32. protected $type;
  33. protected $loader;
  34. protected $repoConfig;
  35. protected $branchErrorOccurred = false;
  36. public function __construct(array $repoConfig, IOInterface $io, Config $config, array $drivers = null)
  37. {
  38. $this->drivers = $drivers ?: array(
  39. 'github' => 'Composer\Repository\Vcs\GitHubDriver',
  40. 'git-bitbucket' => 'Composer\Repository\Vcs\GitBitbucketDriver',
  41. 'git' => 'Composer\Repository\Vcs\GitDriver',
  42. 'hg-bitbucket' => 'Composer\Repository\Vcs\HgBitbucketDriver',
  43. 'hg' => 'Composer\Repository\Vcs\HgDriver',
  44. 'perforce' => 'Composer\Repository\Vcs\PerforceDriver',
  45. // svn must be last because identifying a subversion server for sure is practically impossible
  46. 'svn' => 'Composer\Repository\Vcs\SvnDriver',
  47. );
  48. $this->url = $repoConfig['url'];
  49. $this->io = $io;
  50. $this->type = isset($repoConfig['type']) ? $repoConfig['type'] : 'vcs';
  51. $this->verbose = $io->isVerbose();
  52. $this->config = $config;
  53. $this->repoConfig = $repoConfig;
  54. }
  55. public function getRepoConfig()
  56. {
  57. return $this->repoConfig;
  58. }
  59. public function setLoader(LoaderInterface $loader)
  60. {
  61. $this->loader = $loader;
  62. }
  63. public function getDriver()
  64. {
  65. if (isset($this->drivers[$this->type])) {
  66. $class = $this->drivers[$this->type];
  67. $driver = new $class($this->repoConfig, $this->io, $this->config);
  68. $driver->initialize();
  69. return $driver;
  70. }
  71. foreach ($this->drivers as $driver) {
  72. if ($driver::supports($this->io, $this->url)) {
  73. $driver = new $driver($this->repoConfig, $this->io, $this->config);
  74. $driver->initialize();
  75. return $driver;
  76. }
  77. }
  78. foreach ($this->drivers as $driver) {
  79. if ($driver::supports($this->io, $this->url, true)) {
  80. $driver = new $driver($this->repoConfig, $this->io, $this->config);
  81. $driver->initialize();
  82. return $driver;
  83. }
  84. }
  85. }
  86. public function hadInvalidBranches()
  87. {
  88. return $this->branchErrorOccurred;
  89. }
  90. protected function initialize()
  91. {
  92. parent::initialize();
  93. $verbose = $this->verbose;
  94. $driver = $this->getDriver();
  95. if (!$driver) {
  96. throw new \InvalidArgumentException('No driver found to handle VCS repository '.$this->url);
  97. }
  98. $this->versionParser = new VersionParser;
  99. if (!$this->loader) {
  100. $this->loader = new ArrayLoader($this->versionParser);
  101. }
  102. try {
  103. if ($driver->hasComposerFile($driver->getRootIdentifier())) {
  104. $data = $driver->getComposerInformation($driver->getRootIdentifier());
  105. $this->packageName = !empty($data['name']) ? $data['name'] : null;
  106. }
  107. } catch (\Exception $e) {
  108. if ($verbose) {
  109. $this->io->write('<error>Skipped parsing '.$driver->getRootIdentifier().', '.$e->getMessage().'</error>');
  110. }
  111. }
  112. foreach ($driver->getTags() as $tag => $identifier) {
  113. $msg = 'Reading composer.json of <info>' . ($this->packageName ?: $this->url) . '</info> (<comment>' . $tag . '</comment>)';
  114. if ($verbose) {
  115. $this->io->write($msg);
  116. } else {
  117. $this->io->overwrite($msg, false);
  118. }
  119. // strip the release- prefix from tags if present
  120. $tag = str_replace('release-', '', $tag);
  121. if (!$parsedTag = $this->validateTag($tag)) {
  122. if ($verbose) {
  123. $this->io->write('<warning>Skipped tag '.$tag.', invalid tag name</warning>');
  124. }
  125. continue;
  126. }
  127. try {
  128. if (!$data = $driver->getComposerInformation($identifier)) {
  129. if ($verbose) {
  130. $this->io->write('<warning>Skipped tag '.$tag.', no composer file</warning>');
  131. }
  132. continue;
  133. }
  134. // manually versioned package
  135. if (isset($data['version'])) {
  136. $data['version_normalized'] = $this->versionParser->normalize($data['version']);
  137. } else {
  138. // auto-versioned package, read value from tag
  139. $data['version'] = $tag;
  140. $data['version_normalized'] = $parsedTag;
  141. }
  142. // make sure tag packages have no -dev flag
  143. $data['version'] = preg_replace('{[.-]?dev$}i', '', $data['version']);
  144. $data['version_normalized'] = preg_replace('{(^dev-|[.-]?dev$)}i', '', $data['version_normalized']);
  145. // broken package, version doesn't match tag
  146. if ($data['version_normalized'] !== $parsedTag) {
  147. if ($verbose) {
  148. $this->io->write('<warning>Skipped tag '.$tag.', tag ('.$parsedTag.') does not match version ('.$data['version_normalized'].') in composer.json</warning>');
  149. }
  150. continue;
  151. }
  152. if ($verbose) {
  153. $this->io->write('Importing tag '.$tag.' ('.$data['version_normalized'].')');
  154. }
  155. $this->addPackage($this->loader->load($this->preProcess($driver, $data, $identifier)));
  156. } catch (\Exception $e) {
  157. if ($verbose) {
  158. $this->io->write('<warning>Skipped tag '.$tag.', '.($e instanceof TransportException ? 'no composer file was found' : $e->getMessage()).'</warning>');
  159. }
  160. continue;
  161. }
  162. }
  163. if (!$verbose) {
  164. $this->io->overwrite('', false);
  165. }
  166. foreach ($driver->getBranches() as $branch => $identifier) {
  167. $msg = 'Reading composer.json of <info>' . ($this->packageName ?: $this->url) . '</info> (<comment>' . $branch . '</comment>)';
  168. if ($verbose) {
  169. $this->io->write($msg);
  170. } else {
  171. $this->io->overwrite($msg, false);
  172. }
  173. if (!$parsedBranch = $this->validateBranch($branch)) {
  174. if ($verbose) {
  175. $this->io->write('<warning>Skipped branch '.$branch.', invalid name</warning>');
  176. }
  177. continue;
  178. }
  179. try {
  180. if (!$data = $driver->getComposerInformation($identifier)) {
  181. if ($verbose) {
  182. $this->io->write('<warning>Skipped branch '.$branch.', no composer file</warning>');
  183. }
  184. continue;
  185. }
  186. // branches are always auto-versioned, read value from branch name
  187. $data['version'] = $branch;
  188. $data['version_normalized'] = $parsedBranch;
  189. // make sure branch packages have a dev flag
  190. if ('dev-' === substr($parsedBranch, 0, 4) || '9999999-dev' === $parsedBranch) {
  191. $data['version'] = 'dev-' . $data['version'];
  192. } else {
  193. $data['version'] = preg_replace('{(\.9{7})+}', '.x', $parsedBranch);
  194. }
  195. if ($verbose) {
  196. $this->io->write('Importing branch '.$branch.' ('.$data['version'].')');
  197. }
  198. $packageData = $this->preProcess($driver, $data, $identifier);
  199. $package = $this->loader->load($packageData);
  200. if ($this->loader instanceof ValidatingArrayLoader && $this->loader->getWarnings()) {
  201. throw new InvalidPackageException($this->loader->getErrors(), $this->loader->getWarnings(), $packageData);
  202. }
  203. $this->addPackage($package);
  204. } catch (TransportException $e) {
  205. if ($verbose) {
  206. $this->io->write('<warning>Skipped branch '.$branch.', no composer file was found</warning>');
  207. }
  208. continue;
  209. } catch (\Exception $e) {
  210. if (!$verbose) {
  211. $this->io->write('');
  212. }
  213. $this->branchErrorOccurred = true;
  214. $this->io->write('<error>Skipped branch '.$branch.', '.$e->getMessage().'</error>');
  215. $this->io->write('');
  216. continue;
  217. }
  218. }
  219. $driver->cleanup();
  220. if (!$verbose) {
  221. $this->io->overwrite('', false);
  222. }
  223. if (!$this->getPackages()) {
  224. throw new InvalidRepositoryException('No valid composer.json was found in any branch or tag of '.$this->url.', could not load a package from it.');
  225. }
  226. }
  227. private function preProcess(VcsDriverInterface $driver, array $data, $identifier)
  228. {
  229. // keep the name of the main identifier for all packages
  230. $data['name'] = $this->packageName ?: $data['name'];
  231. if (!isset($data['dist'])) {
  232. $data['dist'] = $driver->getDist($identifier);
  233. }
  234. if (!isset($data['source'])) {
  235. $data['source'] = $driver->getSource($identifier);
  236. }
  237. return $data;
  238. }
  239. private function validateBranch($branch)
  240. {
  241. try {
  242. return $this->versionParser->normalizeBranch($branch);
  243. } catch (\Exception $e) {
  244. }
  245. return false;
  246. }
  247. private function validateTag($version)
  248. {
  249. try {
  250. return $this->versionParser->normalize($version);
  251. } catch (\Exception $e) {
  252. }
  253. return false;
  254. }
  255. }