GitHubDriver.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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\Downloader\TransportException;
  13. use Composer\Json\JsonFile;
  14. use Composer\IO\IOInterface;
  15. use Composer\Util\ProcessExecutor;
  16. use Composer\Util\RemoteFilesystem;
  17. /**
  18. * @author Jordi Boggiano <j.boggiano@seld.be>
  19. */
  20. class GitHubDriver extends VcsDriver
  21. {
  22. protected $owner;
  23. protected $repository;
  24. protected $tags;
  25. protected $branches;
  26. protected $rootIdentifier;
  27. protected $infoCache = array();
  28. protected $isPrivate = false;
  29. /**
  30. * Git Driver
  31. *
  32. * @var GitDriver
  33. */
  34. protected $gitDriver;
  35. /**
  36. * Constructor
  37. *
  38. * @param string $url
  39. * @param IOInterface $io
  40. * @param ProcessExecutor $process
  41. * @param RemoteFilesystem $remoteFilesystem
  42. */
  43. public function __construct($url, IOInterface $io, ProcessExecutor $process = null, RemoteFilesystem $remoteFilesystem = null)
  44. {
  45. preg_match('#^(?:https?|git)://github\.com/([^/]+)/(.+?)(?:\.git)?$#', $url, $match);
  46. $this->owner = $match[1];
  47. $this->repository = $match[2];
  48. parent::__construct($url, $io, $process, $remoteFilesystem);
  49. }
  50. /**
  51. * {@inheritDoc}
  52. */
  53. public function initialize()
  54. {
  55. $this->fetchRootIdentifier();
  56. }
  57. /**
  58. * {@inheritDoc}
  59. */
  60. public function getRootIdentifier()
  61. {
  62. if ($this->gitDriver) {
  63. return $this->gitDriver->getRootIdentifier();
  64. }
  65. return $this->rootIdentifier;
  66. }
  67. /**
  68. * {@inheritDoc}
  69. */
  70. public function getUrl()
  71. {
  72. if ($this->gitDriver) {
  73. return $this->gitDriver->getUrl();
  74. }
  75. return $this->url;
  76. }
  77. /**
  78. * {@inheritDoc}
  79. */
  80. public function getSource($identifier)
  81. {
  82. if ($this->gitDriver) {
  83. return $this->gitDriver->getSource($identifier);
  84. }
  85. $label = array_search($identifier, $this->getTags()) ?: $identifier;
  86. if ($this->isPrivate) {
  87. // Private GitHub repositories should be accessed using the
  88. // SSH version of the URL.
  89. $url = $this->generateSshUrl();
  90. } else {
  91. $url = $this->getUrl();
  92. }
  93. return array('type' => 'git', 'url' => $url, 'reference' => $label);
  94. }
  95. /**
  96. * {@inheritDoc}
  97. */
  98. public function getDist($identifier)
  99. {
  100. if ($this->gitDriver) {
  101. return $this->gitDriver->getDist($identifier);
  102. }
  103. $label = array_search($identifier, $this->getTags()) ?: $identifier;
  104. $url = $this->getScheme() . '://github.com/'.$this->owner.'/'.$this->repository.'/zipball/'.$label;
  105. return array('type' => 'zip', 'url' => $url, 'reference' => $label, 'shasum' => '');
  106. }
  107. /**
  108. * {@inheritDoc}
  109. */
  110. public function getComposerInformation($identifier)
  111. {
  112. if ($this->gitDriver) {
  113. return $this->gitDriver->getComposerInformation($identifier);
  114. }
  115. if (!isset($this->infoCache[$identifier])) {
  116. $composer = $this->getContents($this->getScheme() . '://raw.github.com/'.$this->owner.'/'.$this->repository.'/'.$identifier.'/composer.json');
  117. if (!$composer) {
  118. return;
  119. }
  120. $composer = JsonFile::parseJson($composer);
  121. if (!isset($composer['time'])) {
  122. $commit = JsonFile::parseJson($this->getContents($this->getScheme() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/commits/'.$identifier));
  123. $composer['time'] = $commit['commit']['committer']['date'];
  124. }
  125. $this->infoCache[$identifier] = $composer;
  126. }
  127. return $this->infoCache[$identifier];
  128. }
  129. /**
  130. * {@inheritDoc}
  131. */
  132. public function getTags()
  133. {
  134. if ($this->gitDriver) {
  135. return $this->gitDriver->getTags();
  136. }
  137. if (null === $this->tags) {
  138. $tagsData = JsonFile::parseJson($this->getContents($this->getScheme() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/tags'));
  139. $this->tags = array();
  140. foreach ($tagsData as $tag) {
  141. $this->tags[$tag['name']] = $tag['commit']['sha'];
  142. }
  143. }
  144. return $this->tags;
  145. }
  146. /**
  147. * {@inheritDoc}
  148. */
  149. public function getBranches()
  150. {
  151. if ($this->gitDriver) {
  152. return $this->gitDriver->getBranches();
  153. }
  154. if (null === $this->branches) {
  155. $branchData = JsonFile::parseJson($this->getContents($this->getScheme() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/branches'));
  156. $this->branches = array();
  157. foreach ($branchData as $branch) {
  158. $this->branches[$branch['name']] = $branch['commit']['sha'];
  159. }
  160. }
  161. return $this->branches;
  162. }
  163. /**
  164. * {@inheritDoc}
  165. */
  166. public static function supports($url, $deep = false)
  167. {
  168. return extension_loaded('openssl') && preg_match('#^(?:https?|git)://github\.com/([^/]+)/(.+?)(?:\.git)?$#', $url);
  169. }
  170. /**
  171. * Generate an SSH URL
  172. *
  173. * @return string
  174. */
  175. protected function generateSshUrl()
  176. {
  177. return 'git@github.com:'.$this->owner.'/'.$this->repository.'.git';
  178. }
  179. /**
  180. * Fetch root identifier from GitHub
  181. *
  182. * @throws TransportException
  183. */
  184. protected function fetchRootIdentifier()
  185. {
  186. $repoDataUrl = $this->getScheme() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository;
  187. $attemptCounter = 0;
  188. while (null === $this->rootIdentifier) {
  189. if (5 == $attemptCounter++) {
  190. throw new \RuntimeException("Either you have entered invalid credentials or this GitHub repository does not exists (404)");
  191. }
  192. try {
  193. $repoData = JsonFile::parseJson($this->getContents($repoDataUrl));
  194. $this->rootIdentifier = $repoData['master_branch'] ?: 'master';
  195. } catch (TransportException $e) {
  196. switch($e->getCode()) {
  197. case 401:
  198. case 404:
  199. $this->isPrivate = true;
  200. if (!$this->io->isInteractive()) {
  201. // If this repository may be private (hard to say for sure,
  202. // GitHub returns 404 for private repositories) and we
  203. // cannot ask for authentication credentials (because we
  204. // are not interactive) then we fallback to GitDriver.
  205. $this->gitDriver = new GitDriver(
  206. $this->generateSshUrl(),
  207. $this->io,
  208. $this->process,
  209. $this->remoteFilesystem
  210. );
  211. $this->gitDriver->initialize();
  212. return;
  213. }
  214. $this->io->write('Authentication required (<info>'.$this->url.'</info>):');
  215. $username = $this->io->ask('Username: ');
  216. $password = $this->io->askAndHideAnswer('Password: ');
  217. $this->io->setAuthorization($this->url, $username, $password);
  218. break;
  219. default:
  220. throw $e;
  221. break;
  222. }
  223. }
  224. }
  225. }
  226. }