VcsRepository.php 10 KB

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