diff --git a/src/Composer/Repository/Vcs/SvnDriver.php b/src/Composer/Repository/Vcs/SvnDriver.php index 3380b9d41..5b49b7e77 100644 --- a/src/Composer/Repository/Vcs/SvnDriver.php +++ b/src/Composer/Repository/Vcs/SvnDriver.php @@ -4,6 +4,7 @@ namespace Composer\Repository\Vcs; use Composer\Json\JsonFile; use Composer\Util\ProcessExecutor; +use Composer\Util\Svn as SvnUtil; use Composer\IO\IOInterface; /** @@ -38,6 +39,11 @@ class SvnDriver extends VcsDriver */ protected $svnPassword = ''; + /** + * @var Composer\Util\Svn $util + */ + protected $util; + /** * __construct * @@ -56,8 +62,8 @@ class SvnDriver extends VcsDriver if (false !== ($pos = strrpos($url, '/trunk'))) { $this->baseUrl = substr($url, 0, $pos); } - - $this->detectSvnAuth(); + $this->util = new SvnUtil($this->baseUrl, $io); + $this->useAuth = $this->util->hasAuth(); } /** @@ -68,13 +74,13 @@ class SvnDriver extends VcsDriver * @param string $url The SVN URL. * * @return string - * @uses self::getSvnCommand() + * @uses Composer\Util\Svn::getCommand() * @uses parent::$process * @see ProcessExecutor::execute() */ public function execute($command, $url) { - $svnCommand = $this->getSvnCommand($command, $url); + $svnCommand = $this->util->getCommand($command, $url); $status = $this->process->execute( $svnCommand, @@ -239,77 +245,6 @@ class SvnDriver extends VcsDriver return $this->branches; } - /** - * Return the no-auth-cache switch. - * - * @return string - */ - public function getSvnAuthCache() - { - if (!$this->useCache) { - return '--no-auth-cache '; - } - return ''; - } - - /** - * A method to create the svn commands run. - * - * @string $cmd Usually 'svn ls' or something like that. - * @string $url Repo URL. - * @string $pipe Optional pipe for the output. - * - * @return string - */ - public function getSvnCommand($cmd, $url, $pipe = null) - { - $cmd = sprintf('%s %s%s %s', - $cmd, - $this->getSvnInteractiveSetting(), - $this->getSvnCredentialString(), - escapeshellarg($url) - ); - if ($pipe !== null) { - $cmd .= ' ' . $pipe; - } - return $cmd; - } - - /** - * Return the credential string for the svn command. - * - * Adds --no-auth-cache when credentials are present. - * - * @return string - * @uses self::$useAuth - */ - public function getSvnCredentialString() - { - if ($this->useAuth !== true) { - return ''; - } - $str = ' %s--username %s --password %s '; - return sprintf( - $str, - $this->getSvnAuthCache(), - escapeshellarg($this->svnUsername), - escapeshellarg($this->svnPassword) - ); - } - - /** - * Always run commands 'non-interactive': - * - * It's easier to spot potential issues (e.g. auth-failure) because - * non-interactive svn fails fast and does not wait for user input. - * - * @return string - */ - public function getSvnInteractiveSetting() - { - return '--non-interactive '; - } - /** * {@inheritDoc} */ @@ -330,6 +265,7 @@ class SvnDriver extends VcsDriver "svn info --non-interactive {$url}", $ignoredOutput ); + return $exit === 0; } @@ -347,28 +283,4 @@ class SvnDriver extends VcsDriver } return $url; } - - /** - * This is quick and dirty - thoughts? - * - * @return void - * @uses parent::$baseUrl - * @uses self::$useAuth, self::$svnUsername, self::$svnPassword - * @see self::__construct() - */ - 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; - } } diff --git a/src/Composer/Util/Svn.php b/src/Composer/Util/Svn.php new file mode 100644 index 000000000..b7b45790b --- /dev/null +++ b/src/Composer/Util/Svn.php @@ -0,0 +1,181 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Util; + +use Composer\IO\IOInterface; + +/** + * @author Till Klampaeckel + */ +class Svn +{ + /** + * @var mixed $credentials + * @see self::hasAuth() + */ + protected $credentials; + + /** + * @var boolean $hasAuth + */ + protected $hasAuth; + + /** + * @var \Composer\IO\IOInterface $io + */ + protected $io; + + /** + * @var string $url + */ + protected $url; + + /** + * __construct + * + * @param string $url + * @param \Composer\IO\IOInterface $io + * + * @return \Composer\Util\Svn + */ + public function __construct($url, IOInterface $io) + { + $this->url = $url; + $this->io = $io; + } + + /** + * Return the no-auth-cache switch. + * + * @return string + */ + public function getAuthCache() + { + if (!$this->hasCache) { + return '--no-auth-cache '; + } + return ''; + } + + /** + * A method to create the svn commands run. + * + * @param string $cmd Usually 'svn ls' or something like that. + * @param string $url Repo URL. + * @param string $path The path to run this against (e.g. a 'co' into) + * @param mixed $pipe Optional pipe for the output. + * + * @return string + */ + public function getCommand($cmd, $url, $path = '', $pipe = null) + { + $cmd = sprintf('%s %s%s %s', + $cmd, + '--non-interactive ', + $this->getCredentialString(), + escapeshellarg($url) + ); + if (!empty($path)) { + $cmd .= ' ' . escapeshellarg($path); + } + if ($pipe !== null) { + $cmd .= ' ' . $pipe; + } + return $cmd; + } + + /** + * Return the credential string for the svn command. + * + * Adds --no-auth-cache when credentials are present. + * + * @return string + * @uses self::$useAuth + */ + public function getCredentialString() + { + if ($this->hasAuth === null) { + $this->hasAuth(); + } + if (!$this->hasAuth) { + return ''; + } + return sprintf( + ' %s--username %s --password %s ', + $this->getAuthCache(), + escapeshellarg($this->getUsername()), + escapeshellarg($this->getPassword()) + ); + } + + /** + * Get the password for the svn command. Can be empty. + * + * @return string + * @throws \LogicException + */ + public function getPassword() + { + if ($this->credentials === null) { + throw new \LogicException("No auth detected."); + } + if (isset($this->credentials->password)) { + return $this->credentials->password; + } + return ''; // could be empty + } + + /** + * Get the username for the svn command. + * + * @return string + * @throws \LogicException + */ + public function getUsername() + { + if ($this->credentials === null) { + throw new \LogicException("No auth detected."); + } + return $this->credentials->username; + } + + /** + * Detect Svn Auth. + * + * @param string $url + * + * @return \stdClass + */ + public function hasAuth() + { + if ($this->hasAuth !== null) { + return $this->hasAuth; + } + + $uri = parse_url($this->url); + if (empty($uri['user'])) { + $this->hasAuth = false; + return $this->hasAuth; + } + + $this->hasAuth = true; + $this->credentials = new \stdClass(); + + $this->credentials->username = $uri['user']; + + if (!empty($uri['pass'])) { + $this->credentials->password = $uri['pass']; + } + + return $this->hasAuth; + } +} \ No newline at end of file