123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- <?php
- namespace Composer\Repository\Vcs;
- use Composer\Json\JsonFile;
- use Composer\Util\ProcessExecutor;
- use Composer\IO\IOInterface;
- class SvnDriver extends VcsDriver implements VcsDriverInterface
- {
- protected $baseUrl;
- protected $tags;
- protected $branches;
- protected $infoCache = array();
-
- protected $useAuth = false;
-
- protected $svnUsername = '';
-
- protected $svnPassword = '';
- public function __construct($url, IOInterface $io, ProcessExecutor $process = null)
- {
- parent::__construct($this->baseUrl = rtrim($url, '/'), $io, $process);
- if (false !== ($pos = strrpos($url, '/trunk'))) {
- $this->baseUrl = substr($url, 0, $pos);
- }
- $this->detectSvnAuth();
- }
-
- public function initialize()
- {
- $this->getBranches();
- $this->getTags();
- }
-
- public function getRootIdentifier()
- {
- return 'trunk';
- }
-
- public function getUrl()
- {
- return $this->url;
- }
-
- public function getSource($identifier)
- {
- return array('type' => 'svn', 'url' => $this->baseUrl, 'reference' => $identifier);
- }
-
- public function getDist($identifier)
- {
- return null;
- }
-
- public function getComposerInformation($identifier)
- {
- $identifier = '/' . trim($identifier, '/') . '/';
- if (!isset($this->infoCache[$identifier])) {
- preg_match('{^(.+?)(@\d+)?/$}', $identifier, $match);
- if (!empty($match[2])) {
- $identifier = $match[1];
- $rev = $match[2];
- } else {
- $rev = '';
- }
- $this->process->execute(
- sprintf(
- 'svn cat --non-interactive %s %s',
- $this->getSvnCredentialString(),
- escapeshellarg($this->baseUrl.$identifier.'composer.json'.$rev)
- ),
- $composer
- );
- if (!trim($composer)) {
- throw new \UnexpectedValueException('Failed to retrieve composer information for identifier '.$identifier.' in '.$this->getUrl());
- }
- $composer = JsonFile::parseJson($composer);
- if (!isset($composer['time'])) {
- $this->process->execute(
- sprintf(
- 'svn info %s %s',
- $this->getSvnCredentialString(),
- escapeshellarg($this->baseUrl.$identifier.$rev)
- ),
- $output
- );
- foreach ($this->process->splitLines($output) as $line) {
- if ($line && preg_match('{^Last Changed Date: ([^(]+)}', $line, $match)) {
- $date = new \DateTime($match[1]);
- $composer['time'] = $date->format('Y-m-d H:i:s');
- break;
- }
- }
- }
- $this->infoCache[$identifier] = $composer;
- }
- return $this->infoCache[$identifier];
- }
-
- public function getTags()
- {
- if (null === $this->tags) {
- $this->process->execute(
- sprintf(
- 'svn ls --non-interactive %s %s',
- $this->getSvnCredentialString(),
- escapeshellarg($this->baseUrl.'/tags')
- ),
- $output
- );
- $this->tags = array();
- foreach ($this->process->splitLines($output) as $tag) {
- if ($tag) {
- $this->tags[rtrim($tag, '/')] = '/tags/'.$tag;
- }
- }
- }
- return $this->tags;
- }
-
- public function getBranches()
- {
- if (null === $this->branches) {
- $this->process->execute(
- sprintf(
- 'svn ls --verbose --non-interactive %s %s',
- $this->getSvnCredentialString(),
- escapeshellarg($this->baseUrl.'/')
- ),
- $output
- );
- $this->branches = array();
- foreach ($this->process->splitLines($output) as $line) {
- preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match);
- if ($match[2] === 'trunk/') {
- $this->branches['trunk'] = '/trunk/@'.$match[1];
- break;
- }
- }
- unset($output);
- $this->process->execute(
- sprintf(
- 'svn ls --verbose --non-interactive %s',
- $this->getSvnCredentialString(),
- escapeshellarg($this->baseUrl.'/branches')
- ),
- $output
- );
- foreach ($this->process->splitLines(trim($output)) as $line) {
- preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match);
- if ($match[2] === './') {
- continue;
- }
- $this->branches[rtrim($match[2], '/')] = '/branches/'.$match[2].'@'.$match[1];
- }
- }
- return $this->branches;
- }
-
- public function getSvnCredentialString()
- {
- if ($this->useAuth !== true) {
- return '';
- }
- $str = ' --no-auth-cache --username %s --password %s ';
- return sprintf(
- $str,
- escapeshellarg($this->svnUsername),
- escapeshellarg($this->svnPassword)
- );
- }
-
- public function hasComposerFile($identifier)
- {
- try {
- $this->getComposerInformation($identifier);
- return true;
- } catch (\Exception $e) {
- }
- return false;
- }
-
- public static function supports($url, $deep = false)
- {
- if (preg_match('#(^svn://|//svn\.)#i', $url)) {
- return true;
- }
- if (!$deep) {
- return false;
- }
- $processExecutor = new ProcessExecutor();
- $exit = $processExecutor->execute(
- sprintf(
- 'svn info --non-interactive %s %s 2>/dev/null',
- $this->getSvnCredentialString(),
- escapeshellarg($url)
- ),
- $ignored
- );
- return $exit === 0;
- }
-
- protected function detectSvnAuth()
- {
- $uri = parse_url($this->baseUrl);
- if (empty($uri['user'])) {
- return;
- }
- $this->svnUsername = $uri['user'];
- if (!empty($uri['pass'])) {
- $this->svnPassword = $uri['pass'];
- }
- $this->useAuth = true;
- }
- }
|