1
0
Fork 0
mirror of https://github.com/composer/composer synced 2025-05-10 17:12:51 +00:00

add getFileContent function

This function is very similar to a part from getComposerInformation - so we can use this function in getComposerInformation too. And because it is almost everywhere the same we can put it to abstract class.

By implementing getComposerInformation in abstract class we need to add the getChangeDate to interface too. Only Problem: perforce seems not to support a ChangeDate. For this we use 'now' to have at least something.
This commit is contained in:
Thomas Flori 2016-11-11 09:06:03 +01:00
parent 8ad6385ffb
commit 597f834ae9
13 changed files with 547 additions and 296 deletions

View file

@ -22,43 +22,18 @@ use Composer\Util\Bitbucket;
/**
* @author Per Bernhardt <plb@webfactory.de>
*/
class GitBitbucketDriver extends VcsDriver implements VcsDriverInterface
class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface
{
/**
* @var Cache
*/
protected $cache;
protected $owner;
protected $repository;
protected $tags;
protected $branches;
protected $rootIdentifier;
protected $infoCache = array();
private $hasIssues;
/**
* @var GitDriver
*/
private $gitDriver;
/**
* {@inheritDoc}
*/
public function initialize()
{
preg_match('#^https?://bitbucket\.org/([^/]+)/(.+?)\.git$#', $this->url, $match);
$this->owner = $match[1];
$this->repository = $match[2];
$this->originUrl = 'bitbucket.org';
$this->cache = new Cache($this->io, $this->config->get('cache-repo-dir').'/'.$this->originUrl.'/'.$this->owner.'/'.$this->repository);
}
/**
* {@inheritDoc}
*/
public function getRootIdentifier()
{
if ($this->gitDriver) {
return $this->gitDriver->getRootIdentifier();
if ($this->sshDriver) {
return $this->sshDriver->getRootIdentifier();
}
if (null === $this->rootIdentifier) {
@ -76,8 +51,8 @@ class GitBitbucketDriver extends VcsDriver implements VcsDriverInterface
*/
public function getUrl()
{
if ($this->gitDriver) {
return $this->gitDriver->getUrl();
if ($this->sshDriver) {
return $this->sshDriver->getUrl();
}
return 'https://' . $this->originUrl . '/'.$this->owner.'/'.$this->repository.'.git';
@ -88,8 +63,8 @@ class GitBitbucketDriver extends VcsDriver implements VcsDriverInterface
*/
public function getSource($identifier)
{
if ($this->gitDriver) {
return $this->gitDriver->getSource($identifier);
if ($this->sshDriver) {
return $this->sshDriver->getSource($identifier);
}
return array('type' => 'git', 'url' => $this->getUrl(), 'reference' => $identifier);
@ -105,76 +80,14 @@ class GitBitbucketDriver extends VcsDriver implements VcsDriverInterface
return array('type' => 'zip', 'url' => $url, 'reference' => $identifier, 'shasum' => '');
}
/**
* {@inheritDoc}
*/
public function getComposerInformation($identifier)
{
if ($this->gitDriver) {
return $this->gitDriver->getComposerInformation($identifier);
}
if (preg_match('{[a-f0-9]{40}}i', $identifier) && $res = $this->cache->read($identifier)) {
$this->infoCache[$identifier] = JsonFile::parseJson($res);
}
if (!isset($this->infoCache[$identifier])) {
$resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/src/'.$identifier.'/composer.json';
$file = JsonFile::parseJson($this->getContentsWithOAuthCredentials($resource), $resource);
if (!is_array($file) || ! array_key_exists('data', $file)) {
return array();
}
$composer = JsonFile::parseJson($file['data'], $resource);
if (empty($composer['time'])) {
$resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/changesets/'.$identifier;
$changeset = JsonFile::parseJson($this->getContentsWithOAuthCredentials($resource), $resource);
$composer['time'] = $changeset['timestamp'];
}
if (!isset($composer['support']['source'])) {
$label = array_search($identifier, $this->getTags()) ?: array_search($identifier, $this->getBranches()) ?: $identifier;
if (array_key_exists($label, $tags = $this->getTags())) {
$hash = $tags[$label];
} elseif (array_key_exists($label, $branches = $this->getBranches())) {
$hash = $branches[$label];
}
if (! isset($hash)) {
$composer['support']['source'] = sprintf('https://%s/%s/%s/src', $this->originUrl, $this->owner, $this->repository);
} else {
$composer['support']['source'] = sprintf(
'https://%s/%s/%s/src/%s/?at=%s',
$this->originUrl,
$this->owner,
$this->repository,
$hash,
$label
);
}
}
if (!isset($composer['support']['issues']) && $this->hasIssues) {
$composer['support']['issues'] = sprintf('https://%s/%s/%s/issues', $this->originUrl, $this->owner, $this->repository);
}
if (preg_match('{[a-f0-9]{40}}i', $identifier)) {
$this->cache->write($identifier, json_encode($composer));
}
$this->infoCache[$identifier] = $composer;
}
return $this->infoCache[$identifier];
}
/**
* {@inheritDoc}
*/
public function getTags()
{
if ($this->gitDriver) {
return $this->gitDriver->getTags();
if ($this->sshDriver) {
return $this->sshDriver->getTags();
}
if (null === $this->tags) {
@ -194,8 +107,8 @@ class GitBitbucketDriver extends VcsDriver implements VcsDriverInterface
*/
public function getBranches()
{
if ($this->gitDriver) {
return $this->gitDriver->getBranches();
if ($this->sshDriver) {
return $this->sshDriver->getBranches();
}
if (null === $this->branches) {
@ -228,28 +141,19 @@ class GitBitbucketDriver extends VcsDriver implements VcsDriverInterface
return true;
}
protected function attemptCloneFallback()
{
try {
$this->setupGitDriver($this->generateSshUrl());
return;
} catch (\RuntimeException $e) {
$this->gitDriver = null;
$this->io->writeError('<error>Failed to clone the '.$this->generateSshUrl().' repository, try running in interactive mode so that you can enter your Bitbucket OAuth consumer credentials</error>');
throw $e;
}
}
/**
* Generate an SSH URL
*
* @return string
* @param string $url
*/
private function generateSshUrl()
protected function setupSshDriver($url)
{
return 'git@' . $this->originUrl . ':' . $this->owner.'/'.$this->repository.'.git';
$this->sshDriver = new GitDriver(
array('url' => $url),
$this->io,
$this->config,
$this->process,
$this->remoteFilesystem
);
$this->sshDriver->initialize();
}
/**
@ -284,19 +188,4 @@ class GitBitbucketDriver extends VcsDriver implements VcsDriverInterface
}
}
}
/**
* @param string $url
*/
private function setupGitDriver($url)
{
$this->gitDriver = new GitDriver(
array('url' => $url),
$this->io,
$this->config,
$this->process,
$this->remoteFilesystem
);
$this->gitDriver->initialize();
}
}