GitHubDriver.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace Composer\Repository\Vcs;
  3. use Composer\Json\JsonFile;
  4. use Composer\Console\Helper\WrapperInterface;
  5. /**
  6. * @author Jordi Boggiano <j.boggiano@seld.be>
  7. * @author François Pluchino <francois.pluchino@opendisplay.com>
  8. */
  9. class GitHubDriver extends VcsDriver implements VcsDriverInterface
  10. {
  11. protected $owner;
  12. protected $repository;
  13. protected $tags;
  14. protected $branches;
  15. protected $rootIdentifier;
  16. protected $infoCache = array();
  17. public function __construct($url, WrapperInterface $wrapper)
  18. {
  19. preg_match('#^(?:https?|git)://github\.com/([^/]+)/(.+?)(?:\.git)?$#', $url, $match);
  20. $this->owner = $match[1];
  21. $this->repository = $match[2];
  22. parent::__construct($url, $wrapper);
  23. }
  24. /**
  25. * {@inheritDoc}
  26. */
  27. public function initialize()
  28. {
  29. }
  30. /**
  31. * {@inheritDoc}
  32. */
  33. public function getRootIdentifier()
  34. {
  35. if (null === $this->rootIdentifier) {
  36. $repoData = json_decode(file_get_contents($this->getScheme() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository), true);
  37. $this->rootIdentifier = $repoData['master_branch'] ?: 'master';
  38. }
  39. return $this->rootIdentifier;
  40. }
  41. /**
  42. * {@inheritDoc}
  43. */
  44. public function getUrl()
  45. {
  46. return $this->url;
  47. }
  48. /**
  49. * {@inheritDoc}
  50. */
  51. public function getSource($identifier)
  52. {
  53. $label = array_search($identifier, $this->getTags()) ?: $identifier;
  54. return array('type' => 'git', 'url' => $this->getUrl(), 'reference' => $label);
  55. }
  56. /**
  57. * {@inheritDoc}
  58. */
  59. public function getDist($identifier)
  60. {
  61. $label = array_search($identifier, $this->getTags()) ?: $identifier;
  62. $url = $this->getScheme() . '://github.com/'.$this->owner.'/'.$this->repository.'/zipball/'.$label;
  63. return array('type' => 'zip', 'url' => $url, 'reference' => $label, 'shasum' => '');
  64. }
  65. /**
  66. * {@inheritDoc}
  67. */
  68. public function getComposerInformation($identifier)
  69. {
  70. if (!isset($this->infoCache[$identifier])) {
  71. $composer = @file_get_contents($this->getScheme() . '://raw.github.com/'.$this->owner.'/'.$this->repository.'/'.$identifier.'/composer.json');
  72. if (!$composer) {
  73. throw new \UnexpectedValueException('Failed to retrieve composer information for identifier '.$identifier.' in '.$this->getUrl());
  74. }
  75. $composer = JsonFile::parseJson($composer);
  76. if (!isset($composer['time'])) {
  77. $commit = json_decode(file_get_contents($this->getScheme() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/commits/'.$identifier), true);
  78. $composer['time'] = $commit['commit']['committer']['date'];
  79. }
  80. $this->infoCache[$identifier] = $composer;
  81. }
  82. return $this->infoCache[$identifier];
  83. }
  84. /**
  85. * {@inheritDoc}
  86. */
  87. public function getTags()
  88. {
  89. if (null === $this->tags) {
  90. $tagsData = json_decode(file_get_contents($this->getScheme() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/tags'), true);
  91. $this->tags = array();
  92. foreach ($tagsData as $tag) {
  93. $this->tags[$tag['name']] = $tag['commit']['sha'];
  94. }
  95. }
  96. return $this->tags;
  97. }
  98. /**
  99. * {@inheritDoc}
  100. */
  101. public function getBranches()
  102. {
  103. if (null === $this->branches) {
  104. $branchData = json_decode(file_get_contents($this->getScheme() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/branches'), true);
  105. $this->branches = array();
  106. foreach ($branchData as $branch) {
  107. $this->branches[$branch['name']] = $branch['commit']['sha'];
  108. }
  109. }
  110. return $this->branches;
  111. }
  112. /**
  113. * {@inheritDoc}
  114. */
  115. public function hasComposerFile($identifier)
  116. {
  117. try {
  118. $this->getComposerInformation($identifier);
  119. return true;
  120. } catch (\Exception $e) {
  121. }
  122. return false;
  123. }
  124. /**
  125. * {@inheritDoc}
  126. */
  127. public static function supports($url, $deep = false)
  128. {
  129. return extension_loaded('openssl') && preg_match('#^(?:https?|git)://github\.com/([^/]+)/(.+?)(?:\.git)?$#', $url, $match);
  130. }
  131. }