GitBitbucketDriver.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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\Vcs;
  12. use Composer\Config;
  13. use Composer\Json\JsonFile;
  14. use Composer\IO\IOInterface;
  15. /**
  16. * @author Per Bernhardt <plb@webfactory.de>
  17. */
  18. class GitBitbucketDriver extends VcsDriver implements VcsDriverInterface
  19. {
  20. protected $owner;
  21. protected $repository;
  22. protected $tags;
  23. protected $branches;
  24. protected $rootIdentifier;
  25. protected $infoCache = array();
  26. /**
  27. * {@inheritDoc}
  28. */
  29. public function initialize()
  30. {
  31. preg_match('#^https?://bitbucket\.org/([^/]+)/(.+?)\.git$#', $this->url, $match);
  32. $this->owner = $match[1];
  33. $this->repository = $match[2];
  34. $this->originUrl = 'bitbucket.org';
  35. }
  36. /**
  37. * {@inheritDoc}
  38. */
  39. public function getRootIdentifier()
  40. {
  41. if (null === $this->rootIdentifier) {
  42. $resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository;
  43. $repoData = JsonFile::parseJson($this->getContents($resource), $resource);
  44. $this->rootIdentifier = !empty($repoData['main_branch']) ? $repoData['main_branch'] : 'master';
  45. }
  46. return $this->rootIdentifier;
  47. }
  48. /**
  49. * {@inheritDoc}
  50. */
  51. public function getUrl()
  52. {
  53. return $this->url;
  54. }
  55. /**
  56. * {@inheritDoc}
  57. */
  58. public function getSource($identifier)
  59. {
  60. return array('type' => 'git', 'url' => $this->getUrl(), 'reference' => $identifier);
  61. }
  62. /**
  63. * {@inheritDoc}
  64. */
  65. public function getDist($identifier)
  66. {
  67. $url = $this->getScheme() . '://bitbucket.org/'.$this->owner.'/'.$this->repository.'/get/'.$identifier.'.zip';
  68. return array('type' => 'zip', 'url' => $url, 'reference' => $identifier, 'shasum' => '');
  69. }
  70. /**
  71. * {@inheritDoc}
  72. */
  73. public function getComposerInformation($identifier)
  74. {
  75. if (!isset($this->infoCache[$identifier])) {
  76. $resource = $this->getScheme() . '://bitbucket.org/'.$this->owner.'/'.$this->repository.'/raw/'.$identifier.'/composer.json';
  77. $composer = $this->getContents($resource);
  78. if (!$composer) {
  79. return;
  80. }
  81. $composer = JsonFile::parseJson($composer, $resource);
  82. if (empty($composer['time'])) {
  83. $resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/changesets/'.$identifier;
  84. $changeset = JsonFile::parseJson($this->getContents($resource), $resource);
  85. $composer['time'] = $changeset['timestamp'];
  86. }
  87. $this->infoCache[$identifier] = $composer;
  88. }
  89. return $this->infoCache[$identifier];
  90. }
  91. /**
  92. * {@inheritDoc}
  93. */
  94. public function getTags()
  95. {
  96. if (null === $this->tags) {
  97. $resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/tags';
  98. $tagsData = JsonFile::parseJson($this->getContents($resource), $resource);
  99. $this->tags = array();
  100. foreach ($tagsData as $tag => $data) {
  101. $this->tags[$tag] = $data['raw_node'];
  102. }
  103. }
  104. return $this->tags;
  105. }
  106. /**
  107. * {@inheritDoc}
  108. */
  109. public function getBranches()
  110. {
  111. if (null === $this->branches) {
  112. $resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/branches';
  113. $branchData = JsonFile::parseJson($this->getContents($resource), $resource);
  114. $this->branches = array();
  115. foreach ($branchData as $branch => $data) {
  116. $this->branches[$branch] = $data['raw_node'];
  117. }
  118. }
  119. return $this->branches;
  120. }
  121. /**
  122. * {@inheritDoc}
  123. */
  124. public static function supports(IOInterface $io, Config $config, $url, $deep = false)
  125. {
  126. if (!preg_match('#^https?://bitbucket\.org/([^/]+)/(.+?)\.git$#', $url)) {
  127. return false;
  128. }
  129. if (!extension_loaded('openssl')) {
  130. if ($io->isVerbose()) {
  131. $io->writeError('Skipping Bitbucket git driver for '.$url.' because the OpenSSL PHP extension is missing.');
  132. }
  133. return false;
  134. }
  135. return true;
  136. }
  137. }