/src/Composer/Repository/Vcs/GitBitbucketDriver.php

https://github.com/ecoleman/composer · PHP · 162 lines · 99 code · 24 blank · 39 comment · 9 complexity · bc6e4cee8ddd234060deaba1503c07bd MD5 · raw file

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