GitHubRepository.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace Packagist\WebBundle\Repository\Repository;
  3. class GitHubRepository implements RepositoryInterface
  4. {
  5. protected $owner;
  6. protected $repository;
  7. protected $repositoryData;
  8. protected $tags;
  9. protected $branches;
  10. protected $infoCache = array();
  11. public function __construct($url)
  12. {
  13. preg_match('#^(?:https?|git)://github\.com/([^/]+)/(.+?)(?:\.git)?$#', $url, $match);
  14. $this->owner = $match[1];
  15. $this->repository = $match[2];
  16. }
  17. /**
  18. * {@inheritDoc}
  19. */
  20. public function getType()
  21. {
  22. return 'git';
  23. }
  24. /**
  25. * {@inheritDoc}
  26. */
  27. public function getRootIdentifier()
  28. {
  29. return 'master';
  30. }
  31. /**
  32. * {@inheritDoc}
  33. */
  34. public function getUrl()
  35. {
  36. return 'http://github.com/'.$this->owner.'/'.$this->repository.'.git';
  37. }
  38. /**
  39. * {@inheritDoc}
  40. */
  41. public function getDist($identifier)
  42. {
  43. $repoData = $this->getRepositoryData();
  44. if ($repoData['repository']['has_downloads']) {
  45. $label = array_search($identifier, (array) $this->tags) ?: array_search($identifier, (array) $this->branches) ?: $identifier;
  46. $url = 'https://github.com/'.$this->owner.'/'.$this->repository.'/zipball/'.$label;
  47. $checksum = hash_file('sha1', $url);
  48. return array('type' => 'zip', 'url' => $url, 'shasum' => $checksum ?: '');
  49. }
  50. // TODO clone the repo and build/host a zip ourselves. Not sure if this can happen, but it'll be needed for non-GitHub repos anyway
  51. throw new \LogicException('Not implemented yet.');
  52. }
  53. /**
  54. * {@inheritDoc}
  55. */
  56. public function getComposerInformation($identifier)
  57. {
  58. if (!isset($this->infoCache[$identifier])) {
  59. $composer = json_decode(@file_get_contents('https://raw.github.com/'.$this->owner.'/'.$this->repository.'/'.$identifier.'/composer.json'), true);
  60. if (!$composer) {
  61. throw new \UnexpectedValueException('Failed to download retrieve composer information for identifier '.$identifier.' in '.$this->getUrl());
  62. }
  63. if (!isset($composer['time'])) {
  64. $commit = json_decode(file_get_contents('http://github.com/api/v2/json/commits/show/'.$this->owner.'/'.$this->repository.'/'.$identifier), true);
  65. $composer['time'] = $commit['commit']['committed_date'];
  66. }
  67. $this->infoCache[$identifier] = $composer;
  68. }
  69. return $this->infoCache[$identifier];
  70. }
  71. /**
  72. * {@inheritDoc}
  73. */
  74. public function getTags()
  75. {
  76. if (null === $this->tags) {
  77. $tagsData = json_decode(file_get_contents('http://github.com/api/v2/json/repos/show/'.$this->owner.'/'.$this->repository.'/tags'), true);
  78. $this->tags = $tagsData['tags'];
  79. }
  80. return $this->tags;
  81. }
  82. /**
  83. * {@inheritDoc}
  84. */
  85. public function getBranches()
  86. {
  87. if (null === $this->branches) {
  88. $branchesData = json_decode(file_get_contents('http://github.com/api/v2/json/repos/show/'.$this->owner.'/'.$this->repository.'/branches'), true);
  89. $this->branches = $branchesData['branches'];
  90. }
  91. return $this->branches;
  92. }
  93. /**
  94. * {@inheritDoc}
  95. */
  96. public function hasComposerFile($identifier)
  97. {
  98. return (false !== @fopen('https://raw.github.com/'.$this->owner.'/'.$this->repository.'/'.$identifier.'/composer.json', 'r'));
  99. }
  100. protected function getRepositoryData()
  101. {
  102. if (null === $this->repositoryData) {
  103. $url = 'http://github.com/api/v2/json/repos/show/'.$this->owner.'/'.$this->repository;
  104. $this->repositoryData = json_decode(@file_get_contents($url), true);
  105. if (!$this->repositoryData) {
  106. throw new \UnexpectedValueException('Failed to download from '.$url);
  107. }
  108. }
  109. return $this->repositoryData;
  110. }
  111. }