You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SvnDriver.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /*
  3. * This file is part of the Fxp Composer Asset Plugin package.
  4. *
  5. * (c) François Pluchino <francois.pluchino@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Fxp\Composer\AssetPlugin\Repository\Vcs;
  11. use Composer\Config;
  12. use Composer\Downloader\TransportException;
  13. use Composer\IO\IOInterface;
  14. use Composer\Json\JsonFile;
  15. use Composer\Repository\Vcs\SvnDriver as BaseSvnDriver;
  16. /**
  17. * SVN vcs driver.
  18. *
  19. * @author François Pluchino <francois.pluchino@gmail.com>
  20. */
  21. class SvnDriver extends BaseSvnDriver
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function getComposerInformation($identifier)
  27. {
  28. $identifier = '/'.trim($identifier, '/').'/';
  29. $this->infoCache[$identifier] = Util::readCache($this->infoCache, $this->cache, $this->repoConfig['asset-type'], trim($identifier, '/'), true);
  30. if (!isset($this->infoCache[$identifier])) {
  31. list($path, $rev) = $this->getPathRev($identifier);
  32. $resource = $path.$this->repoConfig['filename'];
  33. $output = $this->getComposerContent($resource, $rev);
  34. $composer = $this->parseComposerContent($output, $resource, $path, $rev);
  35. Util::writeCache($this->cache, $this->repoConfig['asset-type'], trim($identifier, '/'), $composer, true);
  36. $this->infoCache[$identifier] = $composer;
  37. }
  38. return $this->infoCache[$identifier];
  39. }
  40. /**
  41. * Get path and rev.
  42. *
  43. * @param string $identifier The identifier
  44. *
  45. * @return string[]
  46. */
  47. protected function getPathRev($identifier)
  48. {
  49. $path = $identifier;
  50. $rev = '';
  51. preg_match('{^(.+?)(@\d+)?/$}', $identifier, $match);
  52. if (!empty($match[2])) {
  53. $path = $match[1];
  54. $rev = $match[2];
  55. }
  56. return array($path, $rev);
  57. }
  58. /**
  59. * Get the composer content.
  60. *
  61. * @param string $resource The resource
  62. * @param string $rev The rev
  63. *
  64. * @return null|string The composer content
  65. *
  66. * @throws TransportException
  67. */
  68. protected function getComposerContent($resource, $rev)
  69. {
  70. $output = null;
  71. try {
  72. $output = $this->execute('svn cat', $this->baseUrl.$resource.$rev);
  73. } catch (\RuntimeException $e) {
  74. throw new TransportException($e->getMessage());
  75. }
  76. return $output;
  77. }
  78. /**
  79. * Parse the content of composer.
  80. *
  81. * @param string|null $output The output of process executor
  82. * @param string $resource The resouce
  83. * @param string $path The path
  84. * @param string $rev The rev
  85. *
  86. * @return array The composer
  87. */
  88. protected function parseComposerContent($output, $resource, $path, $rev)
  89. {
  90. if (!trim($output)) {
  91. return array('_nonexistent_package' => true);
  92. }
  93. $composer = (array) JsonFile::parseJson($output, $this->baseUrl.$resource.$rev);
  94. return $this->addComposerTime($composer, $path, $rev);
  95. }
  96. /**
  97. * Add time in composer.
  98. *
  99. * @param array $composer The composer
  100. * @param string $path The path
  101. * @param string $rev The rev
  102. *
  103. * @return array The composer
  104. */
  105. protected function addComposerTime(array $composer, $path, $rev)
  106. {
  107. if (!isset($composer['time'])) {
  108. $output = $this->execute('svn info', $this->baseUrl.$path.$rev);
  109. foreach ($this->process->splitLines($output) as $line) {
  110. if ($line && preg_match('{^Last Changed Date: ([^(]+)}', $line, $match)) {
  111. $date = new \DateTime($match[1], new \DateTimeZone('UTC'));
  112. $composer['time'] = $date->format('Y-m-d H:i:s');
  113. break;
  114. }
  115. }
  116. }
  117. return $composer;
  118. }
  119. /**
  120. * {@inheritdoc}
  121. */
  122. public static function supports(IOInterface $io, Config $config, $url, $deep = false)
  123. {
  124. if (0 === strpos($url, 'http') && preg_match('/\/svn|svn\//i', $url)) {
  125. $url = 'svn'.substr($url, strpos($url, '://'));
  126. }
  127. return parent::supports($io, $config, $url, $deep);
  128. }
  129. }