SvnRepository.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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 Symfony\Component\Intl\Util;
  11. use Symfony\Component\Filesystem\Filesystem;
  12. use Symfony\Component\Intl\Exception\RuntimeException;
  13. /**
  14. * A SVN repository containing ICU data.
  15. *
  16. * @author Bernhard Schussek <bschussek@gmail.com>
  17. */
  18. class SvnRepository
  19. {
  20. /**
  21. * @var string The path to the repository
  22. */
  23. private $path;
  24. /**
  25. * @var \SimpleXMLElement
  26. */
  27. private $svnInfo;
  28. /**
  29. * @var SvnCommit
  30. */
  31. private $lastCommit;
  32. /**
  33. * Downloads the ICU data for the given version.
  34. *
  35. * @param string $url The URL to download from
  36. * @param string $targetDir The directory in which to store the repository
  37. *
  38. * @return static
  39. *
  40. * @throws RuntimeException If an error occurs during the download.
  41. */
  42. public static function download($url, $targetDir)
  43. {
  44. exec('which svn', $output, $result);
  45. if ($result !== 0) {
  46. throw new RuntimeException('The command "svn" is not installed.');
  47. }
  48. $filesystem = new Filesystem();
  49. if (!$filesystem->exists($targetDir.'/.svn')) {
  50. $filesystem->remove($targetDir);
  51. $filesystem->mkdir($targetDir);
  52. exec('svn checkout '.$url.' '.$targetDir, $output, $result);
  53. if ($result !== 0) {
  54. throw new RuntimeException('The SVN checkout of '.$url.'failed.');
  55. }
  56. }
  57. return new static(realpath($targetDir));
  58. }
  59. /**
  60. * Reads the SVN repository at the given path.
  61. *
  62. * @param string $path The path to the repository
  63. */
  64. public function __construct($path)
  65. {
  66. $this->path = $path;
  67. }
  68. /**
  69. * Returns the path to the repository.
  70. *
  71. * @return string The path to the repository
  72. */
  73. public function getPath()
  74. {
  75. return $this->path;
  76. }
  77. /**
  78. * Returns the URL of the repository.
  79. *
  80. * @return string The URL of the repository
  81. */
  82. public function getUrl()
  83. {
  84. return (string) $this->getSvnInfo()->entry->url;
  85. }
  86. /**
  87. * Returns the last commit of the repository.
  88. *
  89. * @return SvnCommit The last commit
  90. */
  91. public function getLastCommit()
  92. {
  93. if (null === $this->lastCommit) {
  94. $this->lastCommit = new SvnCommit($this->getSvnInfo()->entry->commit);
  95. }
  96. return $this->lastCommit;
  97. }
  98. /**
  99. * Returns information about the SVN repository.
  100. *
  101. * @return \SimpleXMLElement The XML result from the "svn info" command
  102. *
  103. * @throws RuntimeException If the "svn info" command failed.
  104. */
  105. private function getSvnInfo()
  106. {
  107. if (null === $this->svnInfo) {
  108. exec('svn info --xml '.$this->path, $output, $result);
  109. $svnInfo = simplexml_load_string(implode("\n", $output));
  110. if ($result !== 0) {
  111. throw new RuntimeException('svn info failed');
  112. }
  113. $this->svnInfo = $svnInfo;
  114. }
  115. return $this->svnInfo;
  116. }
  117. }