HgBitbucketDriver.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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\Cache;
  13. use Composer\Config;
  14. use Composer\Json\JsonFile;
  15. use Composer\IO\IOInterface;
  16. /**
  17. * @author Per Bernhardt <plb@webfactory.de>
  18. */
  19. class HgBitbucketDriver extends VcsDriver
  20. {
  21. protected $cache;
  22. protected $owner;
  23. protected $repository;
  24. protected $tags;
  25. protected $branches;
  26. protected $rootIdentifier;
  27. protected $infoCache = array();
  28. /**
  29. * {@inheritDoc}
  30. */
  31. public function initialize()
  32. {
  33. preg_match('#^https?://bitbucket\.org/([^/]+)/([^/]+)/?$#', $this->url, $match);
  34. $this->owner = $match[1];
  35. $this->repository = $match[2];
  36. $this->originUrl = 'bitbucket.org';
  37. $this->cache = new Cache($this->io, $this->config->get('cache-repo-dir').'/'.$this->originUrl.'/'.$this->owner.'/'.$this->repository);
  38. }
  39. /**
  40. * {@inheritDoc}
  41. */
  42. public function getRootIdentifier()
  43. {
  44. if (null === $this->rootIdentifier) {
  45. $resource = $this->getScheme() . '://bitbucket.org/api/1.0/repositories/'.$this->owner.'/'.$this->repository.'/tags';
  46. $repoData = JsonFile::parseJson($this->getContents($resource), $resource);
  47. if (array() === $repoData || !isset($repoData['tip'])) {
  48. throw new \RuntimeException($this->url.' does not appear to be a mercurial repository, use '.$this->url.'.git if this is a git bitbucket repository');
  49. }
  50. $this->rootIdentifier = $repoData['tip']['raw_node'];
  51. }
  52. return $this->rootIdentifier;
  53. }
  54. /**
  55. * {@inheritDoc}
  56. */
  57. public function getUrl()
  58. {
  59. return $this->url;
  60. }
  61. /**
  62. * {@inheritDoc}
  63. */
  64. public function getSource($identifier)
  65. {
  66. return array('type' => 'hg', 'url' => $this->getUrl(), 'reference' => $identifier);
  67. }
  68. /**
  69. * {@inheritDoc}
  70. */
  71. public function getDist($identifier)
  72. {
  73. $url = $this->getScheme() . '://bitbucket.org/'.$this->owner.'/'.$this->repository.'/get/'.$identifier.'.zip';
  74. return array('type' => 'zip', 'url' => $url, 'reference' => $identifier, 'shasum' => '');
  75. }
  76. /**
  77. * {@inheritDoc}
  78. */
  79. public function getComposerInformation($identifier)
  80. {
  81. if (preg_match('{[a-f0-9]{40}}i', $identifier) && $res = $this->cache->read($identifier)) {
  82. $this->infoCache[$identifier] = JsonFile::parseJson($res);
  83. }
  84. if (!isset($this->infoCache[$identifier])) {
  85. $resource = $this->getScheme() . '://bitbucket.org/api/1.0/repositories/'.$this->owner.'/'.$this->repository.'/src/'.$identifier.'/composer.json';
  86. $repoData = JsonFile::parseJson($this->getContents($resource), $resource);
  87. // Bitbucket does not send different response codes for found and
  88. // not found files, so we have to check the response structure.
  89. // found: {node: ..., data: ..., size: ..., ...}
  90. // not found: {node: ..., files: [...], directories: [...], ...}
  91. if (!array_key_exists('data', $repoData)) {
  92. return;
  93. }
  94. $composer = JsonFile::parseJson($repoData['data'], $resource);
  95. if (empty($composer['time'])) {
  96. $resource = $this->getScheme() . '://bitbucket.org/api/1.0/repositories/'.$this->owner.'/'.$this->repository.'/changesets/'.$identifier;
  97. $changeset = JsonFile::parseJson($this->getContents($resource), $resource);
  98. $composer['time'] = $changeset['timestamp'];
  99. }
  100. if (preg_match('{[a-f0-9]{40}}i', $identifier)) {
  101. $this->cache->write($identifier, json_encode($composer));
  102. }
  103. $this->infoCache[$identifier] = $composer;
  104. }
  105. return $this->infoCache[$identifier];
  106. }
  107. /**
  108. * {@inheritDoc}
  109. */
  110. public function getTags()
  111. {
  112. if (null === $this->tags) {
  113. $resource = $this->getScheme() . '://bitbucket.org/api/1.0/repositories/'.$this->owner.'/'.$this->repository.'/tags';
  114. $tagsData = JsonFile::parseJson($this->getContents($resource), $resource);
  115. $this->tags = array();
  116. foreach ($tagsData as $tag => $data) {
  117. $this->tags[$tag] = $data['raw_node'];
  118. }
  119. unset($this->tags['tip']);
  120. }
  121. return $this->tags;
  122. }
  123. /**
  124. * {@inheritDoc}
  125. */
  126. public function getBranches()
  127. {
  128. if (null === $this->branches) {
  129. $resource = $this->getScheme() . '://bitbucket.org/api/1.0/repositories/'.$this->owner.'/'.$this->repository.'/branches';
  130. $branchData = JsonFile::parseJson($this->getContents($resource), $resource);
  131. $this->branches = array();
  132. foreach ($branchData as $branch => $data) {
  133. $this->branches[$branch] = $data['raw_node'];
  134. }
  135. }
  136. return $this->branches;
  137. }
  138. /**
  139. * {@inheritDoc}
  140. */
  141. public static function supports(IOInterface $io, Config $config, $url, $deep = false)
  142. {
  143. if (!preg_match('#^https?://bitbucket\.org/([^/]+)/([^/]+)/?$#', $url)) {
  144. return false;
  145. }
  146. if (!extension_loaded('openssl')) {
  147. $io->writeError('Skipping Bitbucket hg driver for '.$url.' because the OpenSSL PHP extension is missing.', true, IOInterface::VERBOSE);
  148. return false;
  149. }
  150. return true;
  151. }
  152. }