123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <?php
- /*
- * This file is part of Composer.
- *
- * (c) Nils Adermann <naderman@naderman.de>
- * Jordi Boggiano <j.boggiano@seld.be>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Composer\Repository\Vcs;
- use Composer\Downloader\TransportException;
- use Composer\Json\JsonFile;
- use Composer\IO\IOInterface;
- use Composer\Util\ProcessExecutor;
- use Composer\Util\RemoteFilesystem;
- /**
- * @author Jordi Boggiano <j.boggiano@seld.be>
- */
- class GitHubDriver extends VcsDriver
- {
- protected $owner;
- protected $repository;
- protected $tags;
- protected $branches;
- protected $rootIdentifier;
- protected $infoCache = array();
- protected $isPrivate = false;
- /**
- * Git Driver
- *
- * @var GitDriver
- */
- protected $gitDriver;
- /**
- * Constructor
- *
- * @param string $url
- * @param IOInterface $io
- * @param ProcessExecutor $process
- * @param RemoteFilesystem $remoteFilesystem
- */
- public function __construct($url, IOInterface $io, ProcessExecutor $process = null, RemoteFilesystem $remoteFilesystem = null)
- {
- preg_match('#^(?:https?|git)://github\.com/([^/]+)/(.+?)(?:\.git)?$#', $url, $match);
- $this->owner = $match[1];
- $this->repository = $match[2];
- parent::__construct($url, $io, $process, $remoteFilesystem);
- }
- /**
- * {@inheritDoc}
- */
- public function initialize()
- {
- $this->fetchRootIdentifier();
- }
- /**
- * {@inheritDoc}
- */
- public function getRootIdentifier()
- {
- if ($this->gitDriver) {
- return $this->gitDriver->getRootIdentifier();
- }
- return $this->rootIdentifier;
- }
- /**
- * {@inheritDoc}
- */
- public function getUrl()
- {
- if ($this->gitDriver) {
- return $this->gitDriver->getUrl();
- }
- return $this->url;
- }
- /**
- * {@inheritDoc}
- */
- public function getSource($identifier)
- {
- if ($this->gitDriver) {
- return $this->gitDriver->getSource($identifier);
- }
- $label = array_search($identifier, $this->getTags()) ?: $identifier;
- if ($this->isPrivate) {
- // Private GitHub repositories should be accessed using the
- // SSH version of the URL.
- $url = $this->generateSshUrl();
- } else {
- $url = $this->getUrl();
- }
- return array('type' => 'git', 'url' => $url, 'reference' => $label);
- }
- /**
- * {@inheritDoc}
- */
- public function getDist($identifier)
- {
- if ($this->gitDriver) {
- return $this->gitDriver->getDist($identifier);
- }
- $label = array_search($identifier, $this->getTags()) ?: $identifier;
- $url = $this->getScheme() . '://github.com/'.$this->owner.'/'.$this->repository.'/zipball/'.$label;
- return array('type' => 'zip', 'url' => $url, 'reference' => $label, 'shasum' => '');
- }
- /**
- * {@inheritDoc}
- */
- public function getComposerInformation($identifier)
- {
- if ($this->gitDriver) {
- return $this->gitDriver->getComposerInformation($identifier);
- }
- if (!isset($this->infoCache[$identifier])) {
- $composer = $this->getContents($this->getScheme() . '://raw.github.com/'.$this->owner.'/'.$this->repository.'/'.$identifier.'/composer.json');
- if (!$composer) {
- return;
- }
- $composer = JsonFile::parseJson($composer);
- if (!isset($composer['time'])) {
- $commit = JsonFile::parseJson($this->getContents($this->getScheme() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/commits/'.$identifier));
- $composer['time'] = $commit['commit']['committer']['date'];
- }
- $this->infoCache[$identifier] = $composer;
- }
- return $this->infoCache[$identifier];
- }
- /**
- * {@inheritDoc}
- */
- public function getTags()
- {
- if ($this->gitDriver) {
- return $this->gitDriver->getTags();
- }
- if (null === $this->tags) {
- $tagsData = JsonFile::parseJson($this->getContents($this->getScheme() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/tags'));
- $this->tags = array();
- foreach ($tagsData as $tag) {
- $this->tags[$tag['name']] = $tag['commit']['sha'];
- }
- }
- return $this->tags;
- }
- /**
- * {@inheritDoc}
- */
- public function getBranches()
- {
- if ($this->gitDriver) {
- return $this->gitDriver->getBranches();
- }
- if (null === $this->branches) {
- $branchData = JsonFile::parseJson($this->getContents($this->getScheme() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/branches'));
- $this->branches = array();
- foreach ($branchData as $branch) {
- $this->branches[$branch['name']] = $branch['commit']['sha'];
- }
- }
- return $this->branches;
- }
- /**
- * {@inheritDoc}
- */
- public static function supports($url, $deep = false)
- {
- return extension_loaded('openssl') && preg_match('#^(?:https?|git)://github\.com/([^/]+)/(.+?)(?:\.git)?$#', $url);
- }
- /**
- * Generate an SSH URL
- *
- * @return string
- */
- protected function generateSshUrl()
- {
- return 'git@github.com:'.$this->owner.'/'.$this->repository.'.git';
- }
- /**
- * Fetch root identifier from GitHub
- *
- * @throws TransportException
- */
- protected function fetchRootIdentifier()
- {
- $repoDataUrl = $this->getScheme() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository;
- $attemptCounter = 0;
- while (null === $this->rootIdentifier) {
- if (5 == $attemptCounter++) {
- throw new \RuntimeException("Either you have entered invalid credentials or this GitHub repository does not exists (404)");
- }
- try {
- $repoData = JsonFile::parseJson($this->getContents($repoDataUrl));
- $this->rootIdentifier = $repoData['master_branch'] ?: 'master';
- } catch (TransportException $e) {
- switch($e->getCode()) {
- case 401:
- case 404:
- $this->isPrivate = true;
- if (!$this->io->isInteractive()) {
- // If this repository may be private (hard to say for sure,
- // GitHub returns 404 for private repositories) and we
- // cannot ask for authentication credentials (because we
- // are not interactive) then we fallback to GitDriver.
- $this->gitDriver = new GitDriver(
- $this->generateSshUrl(),
- $this->io,
- $this->process,
- $this->remoteFilesystem
- );
- $this->gitDriver->initialize();
- return;
- }
- $this->io->write('Authentication required (<info>'.$this->url.'</info>):');
- $username = $this->io->ask('Username: ');
- $password = $this->io->askAndHideAnswer('Password: ');
- $this->io->setAuthorization($this->url, $username, $password);
- break;
- default:
- throw $e;
- break;
- }
- }
- }
- }
- }
|