GitHubDriver.php 4.1 KB

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