VcsRepository.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace Composer\Repository;
  3. use Composer\Repository\Vcs\VcsDriverInterface;
  4. use Composer\Package\Version\VersionParser;
  5. use Composer\Package\Loader\ArrayLoader;
  6. use Composer\IO\IOInterface;
  7. /**
  8. * @author Jordi Boggiano <j.boggiano@seld.be>
  9. */
  10. class VcsRepository extends ArrayRepository
  11. {
  12. protected $url;
  13. protected $packageName;
  14. protected $debug;
  15. protected $io;
  16. public function __construct(array $config, IOInterface $io, array $drivers = null)
  17. {
  18. $this->drivers = $drivers ?: array(
  19. 'Composer\Repository\Vcs\GitHubDriver',
  20. 'Composer\Repository\Vcs\GitBitbucketDriver',
  21. 'Composer\Repository\Vcs\GitDriver',
  22. 'Composer\Repository\Vcs\SvnDriver',
  23. 'Composer\Repository\Vcs\HgBitbucketDriver',
  24. 'Composer\Repository\Vcs\HgDriver',
  25. );
  26. $this->url = $config['url'];
  27. $this->io = $io;
  28. }
  29. public function setDebug($debug)
  30. {
  31. $this->debug = $debug;
  32. }
  33. public function getDriver()
  34. {
  35. foreach ($this->drivers as $driver) {
  36. if ($driver::supports($this->url)) {
  37. $driver = new $driver($this->url, $this->io);
  38. $driver->initialize();
  39. return $driver;
  40. }
  41. }
  42. foreach ($this->drivers as $driver) {
  43. if ($driver::supports($this->url, true)) {
  44. $driver = new $driver($this->url, $this->io);
  45. $driver->initialize();
  46. return $driver;
  47. }
  48. }
  49. }
  50. protected function initialize()
  51. {
  52. parent::initialize();
  53. $debug = $this->debug;
  54. $driver = $this->getDriver();
  55. if (!$driver) {
  56. throw new \InvalidArgumentException('No driver found to handle VCS repository '.$this->url);
  57. }
  58. $versionParser = new VersionParser;
  59. $loader = new ArrayLoader();
  60. if ($driver->hasComposerFile($driver->getRootIdentifier())) {
  61. $data = $driver->getComposerInformation($driver->getRootIdentifier());
  62. $this->packageName = !empty($data['name']) ? $data['name'] : null;
  63. }
  64. foreach ($driver->getTags() as $tag => $identifier) {
  65. $this->io->overwrite('Get composer of <info>' . $this->packageName . '</info> (<comment>' . $tag . '</comment>)', false);
  66. $parsedTag = $this->validateTag($versionParser, $tag);
  67. if ($parsedTag && $driver->hasComposerFile($identifier)) {
  68. try {
  69. $data = $driver->getComposerInformation($identifier);
  70. } catch (\Exception $e) {
  71. if ($debug) {
  72. $this->io->write('Skipped tag '.$tag.', '.$e->getMessage());
  73. }
  74. continue;
  75. }
  76. // manually versioned package
  77. if (isset($data['version'])) {
  78. $data['version_normalized'] = $versionParser->normalize($data['version']);
  79. } else {
  80. // auto-versionned package, read value from tag
  81. $data['version'] = $tag;
  82. $data['version_normalized'] = $parsedTag;
  83. }
  84. // make sure tag packages have no -dev flag
  85. $data['version'] = preg_replace('{[.-]?dev$}i', '', $data['version']);
  86. $data['version_normalized'] = preg_replace('{(^dev-|[.-]?dev$)}i', '', $data['version_normalized']);
  87. // broken package, version doesn't match tag
  88. if ($data['version_normalized'] !== $parsedTag) {
  89. if ($debug) {
  90. $this->io->write('Skipped tag '.$tag.', tag ('.$parsedTag.') does not match version ('.$data['version_normalized'].') in composer.json');
  91. }
  92. continue;
  93. }
  94. if ($debug) {
  95. $this->io->write('Importing tag '.$tag.' ('.$data['version_normalized'].')');
  96. }
  97. $this->addPackage($loader->load($this->preProcess($driver, $data, $identifier)));
  98. } elseif ($debug) {
  99. $this->io->write('Skipped tag '.$tag.', '.($parsedTag ? 'no composer file was found' : 'invalid name'));
  100. }
  101. }
  102. $this->io->overwrite('', false);
  103. foreach ($driver->getBranches() as $branch => $identifier) {
  104. $this->io->overwrite('Get composer of <info>' . $this->packageName . '</info> (<comment>' . $branch . '</comment>)', false);
  105. $parsedBranch = $this->validateBranch($versionParser, $branch);
  106. if ($driver->hasComposerFile($identifier)) {
  107. $data = $driver->getComposerInformation($identifier);
  108. if (!$parsedBranch) {
  109. if ($debug) {
  110. $this->io->write('Skipped branch '.$branch.', invalid name and no composer file was found');
  111. }
  112. continue;
  113. }
  114. // branches are always auto-versionned, read value from branch name
  115. $data['version'] = $branch;
  116. $data['version_normalized'] = $parsedBranch;
  117. // make sure branch packages have a dev flag
  118. if ('dev-' === substr($parsedBranch, 0, 4) || '9999999-dev' === $parsedBranch) {
  119. $data['version'] = 'dev-' . $data['version'];
  120. } else {
  121. $data['version'] = $data['version'] . '-dev';
  122. }
  123. if ($debug) {
  124. $this->io->write('Importing branch '.$branch.' ('.$data['version_normalized'].')');
  125. }
  126. $this->addPackage($loader->load($this->preProcess($driver, $data, $identifier)));
  127. } elseif ($debug) {
  128. $this->io->write('Skipped branch '.$branch.', no composer file was found');
  129. }
  130. }
  131. $this->io->overwrite('', false);
  132. }
  133. private function preProcess(VcsDriverInterface $driver, array $data, $identifier)
  134. {
  135. // keep the name of the main identifier for all packages
  136. $data['name'] = $this->packageName ?: $data['name'];
  137. if (!isset($data['dist'])) {
  138. $data['dist'] = $driver->getDist($identifier);
  139. }
  140. if (!isset($data['source'])) {
  141. $data['source'] = $driver->getSource($identifier);
  142. }
  143. return $data;
  144. }
  145. private function validateBranch($versionParser, $branch)
  146. {
  147. try {
  148. return $versionParser->normalizeBranch($branch);
  149. } catch (\Exception $e) {
  150. }
  151. return false;
  152. }
  153. private function validateTag($versionParser, $version)
  154. {
  155. try {
  156. return $versionParser->normalize($version);
  157. } catch (\Exception $e) {
  158. }
  159. return false;
  160. }
  161. }