GitHubDriver.php 8.7 KB

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