SvnDriver.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. namespace Composer\Repository\Vcs;
  3. use Composer\Json\JsonFile;
  4. use Composer\Util\ProcessExecutor;
  5. use Composer\IO\IOInterface;
  6. /**
  7. * @author Jordi Boggiano <j.boggiano@seld.be>
  8. */
  9. class SvnDriver extends VcsDriver implements VcsDriverInterface
  10. {
  11. protected $baseUrl;
  12. protected $tags;
  13. protected $branches;
  14. protected $infoCache = array();
  15. /**
  16. * @var boolean $useAuth Contains credentials, or not?
  17. */
  18. protected $useAuth = false;
  19. /**
  20. * @var string $svnUsername
  21. */
  22. protected $svnUsername = '';
  23. /**
  24. * @var string $svnPassword
  25. */
  26. protected $svnPassword = '';
  27. public function __construct($url, IOInterface $io, ProcessExecutor $process = null)
  28. {
  29. parent::__construct($this->baseUrl = rtrim($url, '/'), $io, $process);
  30. if (false !== ($pos = strrpos($url, '/trunk'))) {
  31. $this->baseUrl = substr($url, 0, $pos);
  32. }
  33. $this->detectSvnAuth();
  34. }
  35. /**
  36. * {@inheritDoc}
  37. */
  38. public function initialize()
  39. {
  40. $this->getBranches();
  41. $this->getTags();
  42. }
  43. /**
  44. * {@inheritDoc}
  45. */
  46. public function getRootIdentifier()
  47. {
  48. return 'trunk';
  49. }
  50. /**
  51. * {@inheritDoc}
  52. */
  53. public function getUrl()
  54. {
  55. return $this->url;
  56. }
  57. /**
  58. * {@inheritDoc}
  59. */
  60. public function getSource($identifier)
  61. {
  62. return array('type' => 'svn', 'url' => $this->baseUrl, 'reference' => $identifier);
  63. }
  64. /**
  65. * {@inheritDoc}
  66. */
  67. public function getDist($identifier)
  68. {
  69. return null;
  70. }
  71. /**
  72. * {@inheritDoc}
  73. */
  74. public function getComposerInformation($identifier)
  75. {
  76. $identifier = '/' . trim($identifier, '/') . '/';
  77. if (!isset($this->infoCache[$identifier])) {
  78. preg_match('{^(.+?)(@\d+)?/$}', $identifier, $match);
  79. if (!empty($match[2])) {
  80. $identifier = $match[1];
  81. $rev = $match[2];
  82. } else {
  83. $rev = '';
  84. }
  85. $this->process->execute(
  86. sprintf(
  87. 'svn cat --non-interactive %s %s',
  88. $this->getSvnCredentialString(),
  89. escapeshellarg($this->baseUrl.$identifier.'composer.json'.$rev)
  90. ),
  91. $composer
  92. );
  93. if (!trim($composer)) {
  94. throw new \UnexpectedValueException('Failed to retrieve composer information for identifier '.$identifier.' in '.$this->getUrl());
  95. }
  96. $composer = JsonFile::parseJson($composer);
  97. if (!isset($composer['time'])) {
  98. $this->process->execute(
  99. sprintf(
  100. 'svn info %s %s',
  101. $this->getSvnCredentialString(),
  102. escapeshellarg($this->baseUrl.$identifier.$rev)
  103. ),
  104. $output
  105. );
  106. foreach ($this->process->splitLines($output) as $line) {
  107. if ($line && preg_match('{^Last Changed Date: ([^(]+)}', $line, $match)) {
  108. $date = new \DateTime($match[1]);
  109. $composer['time'] = $date->format('Y-m-d H:i:s');
  110. break;
  111. }
  112. }
  113. }
  114. $this->infoCache[$identifier] = $composer;
  115. }
  116. return $this->infoCache[$identifier];
  117. }
  118. /**
  119. * {@inheritDoc}
  120. */
  121. public function getTags()
  122. {
  123. if (null === $this->tags) {
  124. $this->process->execute(
  125. sprintf(
  126. 'svn ls --non-interactive %s %s',
  127. $this->getSvnCredentialString(),
  128. escapeshellarg($this->baseUrl.'/tags')
  129. ),
  130. $output
  131. );
  132. $this->tags = array();
  133. foreach ($this->process->splitLines($output) as $tag) {
  134. if ($tag) {
  135. $this->tags[rtrim($tag, '/')] = '/tags/'.$tag;
  136. }
  137. }
  138. }
  139. return $this->tags;
  140. }
  141. /**
  142. * {@inheritDoc}
  143. */
  144. public function getBranches()
  145. {
  146. if (null === $this->branches) {
  147. $this->process->execute(
  148. sprintf(
  149. 'svn ls --verbose --non-interactive %s %s',
  150. $this->getSvnCredentialString(),
  151. escapeshellarg($this->baseUrl.'/')
  152. ),
  153. $output
  154. );
  155. $this->branches = array();
  156. foreach ($this->process->splitLines($output) as $line) {
  157. preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match);
  158. if ($match[2] === 'trunk/') {
  159. $this->branches['trunk'] = '/trunk/@'.$match[1];
  160. break;
  161. }
  162. }
  163. unset($output);
  164. $this->process->execute(
  165. sprintf(
  166. 'svn ls --verbose --non-interactive %s',
  167. $this->getSvnCredentialString(),
  168. escapeshellarg($this->baseUrl.'/branches')
  169. ),
  170. $output
  171. );
  172. foreach ($this->process->splitLines(trim($output)) as $line) {
  173. preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match);
  174. if ($match[2] === './') {
  175. continue;
  176. }
  177. $this->branches[rtrim($match[2], '/')] = '/branches/'.$match[2].'@'.$match[1];
  178. }
  179. }
  180. return $this->branches;
  181. }
  182. /**
  183. * Return the credential string for the svn command.
  184. *
  185. * --no-auth-cache when credentials are present
  186. *
  187. * @return string
  188. */
  189. public function getSvnCredentialString()
  190. {
  191. if ($this->useAuth !== true) {
  192. return '';
  193. }
  194. $str = ' --no-auth-cache --username %s --password %s ';
  195. return sprintf(
  196. $str,
  197. escapeshellarg($this->svnUsername),
  198. escapeshellarg($this->svnPassword)
  199. );
  200. }
  201. /**
  202. * {@inheritDoc}
  203. */
  204. public function hasComposerFile($identifier)
  205. {
  206. try {
  207. $this->getComposerInformation($identifier);
  208. return true;
  209. } catch (\Exception $e) {
  210. }
  211. return false;
  212. }
  213. /**
  214. * {@inheritDoc}
  215. */
  216. public static function supports($url, $deep = false)
  217. {
  218. if (preg_match('#(^svn://|//svn\.)#i', $url)) {
  219. return true;
  220. }
  221. if (!$deep) {
  222. return false;
  223. }
  224. $processExecutor = new ProcessExecutor();
  225. $exit = $processExecutor->execute(
  226. sprintf(
  227. 'svn info --non-interactive %s %s 2>/dev/null',
  228. $this->getSvnCredentialString(),
  229. escapeshellarg($url)
  230. ),
  231. $ignored
  232. );
  233. return $exit === 0;
  234. }
  235. /**
  236. * This is quick and dirty - thoughts?
  237. *
  238. * @return void
  239. * @uses parent::$baseUrl
  240. * @uses self::$useAuth, self::$svnUsername, self::$svnPassword
  241. * @see self::__construct()
  242. */
  243. protected function detectSvnAuth()
  244. {
  245. $uri = parse_url($this->baseUrl);
  246. if (empty($uri['user'])) {
  247. return;
  248. }
  249. $this->svnUsername = $uri['user'];
  250. if (!empty($uri['pass'])) {
  251. $this->svnPassword = $uri['pass'];
  252. }
  253. $this->useAuth = true;
  254. }
  255. }