GitHubDriver.php 4.2 KB

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