HgBitbucketDriver.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 HgBitbucketDriver 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. public function __construct($url, IOInterface $io)
  26. {
  27. preg_match('#^https://bitbucket\.org/([^/]+)/([^/]+)/?$#', $url, $match);
  28. $this->owner = $match[1];
  29. $this->repository = $match[2];
  30. parent::__construct($url, $io);
  31. }
  32. /**
  33. * {@inheritDoc}
  34. */
  35. public function initialize()
  36. {
  37. }
  38. /**
  39. * {@inheritDoc}
  40. */
  41. public function getRootIdentifier()
  42. {
  43. if (null === $this->rootIdentifier) {
  44. $repoData = json_decode($this->getContents($this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/tags'), true);
  45. $this->rootIdentifier = $repoData['tip']['raw_node'];
  46. }
  47. return $this->rootIdentifier;
  48. }
  49. /**
  50. * {@inheritDoc}
  51. */
  52. public function getUrl()
  53. {
  54. return $this->url;
  55. }
  56. /**
  57. * {@inheritDoc}
  58. */
  59. public function getSource($identifier)
  60. {
  61. $label = array_search($identifier, $this->getTags()) ?: $identifier;
  62. return array('type' => 'hg', 'url' => $this->getUrl(), 'reference' => $label);
  63. }
  64. /**
  65. * {@inheritDoc}
  66. */
  67. public function getDist($identifier)
  68. {
  69. $label = array_search($identifier, $this->getTags()) ?: $identifier;
  70. $url = $this->getScheme() . '://bitbucket.org/'.$this->owner.'/'.$this->repository.'/get/'.$label.'.zip';
  71. return array('type' => 'zip', 'url' => $url, 'reference' => $label, 'shasum' => '');
  72. }
  73. /**
  74. * {@inheritDoc}
  75. */
  76. public function getComposerInformation($identifier)
  77. {
  78. if (!isset($this->infoCache[$identifier])) {
  79. $composer = $this->getContents($this->getScheme() . '://bitbucket.org/'.$this->owner.'/'.$this->repository.'/raw/'.$identifier.'/composer.json');
  80. if (!$composer) {
  81. throw new \UnexpectedValueException('Failed to retrieve composer information for identifier '.$identifier.' in '.$this->getUrl());
  82. }
  83. $composer = JsonFile::parseJson($composer);
  84. if (!isset($composer['time'])) {
  85. $changeset = json_decode($this->getContents($this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/changesets/'.$identifier), true);
  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. $tagsData = json_decode($this->getContents($this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/tags'), true);
  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. $branchData = json_decode($this->getContents($this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/branches'), true);
  113. $this->branches = array();
  114. foreach ($branchData as $branch => $data) {
  115. $this->branches[$branch] = $data['raw_node'];
  116. }
  117. }
  118. return $this->branches;
  119. }
  120. /**
  121. * {@inheritDoc}
  122. */
  123. public function hasComposerFile($identifier)
  124. {
  125. try {
  126. $this->getComposerInformation($identifier);
  127. return true;
  128. } catch (\Exception $e) {
  129. }
  130. return false;
  131. }
  132. /**
  133. * {@inheritDoc}
  134. */
  135. public static function supports($url, $deep = false)
  136. {
  137. return preg_match('#^https://bitbucket\.org/([^/]+)/([^/]+)/?$#', $url);
  138. }
  139. }