VcsRepository.php 9.3 KB

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