aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/symfony/intl/Symfony/Component/Intl/Util/SvnRepository.php
blob: fb44e3c6c6f4abaf4eaa20c7cd656a99d811a3a8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Intl\Util;

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Intl\Exception\RuntimeException;

/**
 * A SVN repository containing ICU data.
 *
 * @author Bernhard Schussek <bschussek@gmail.com>
 */
class SvnRepository
{
    /**
     * @var string The path to the repository.
     */
    private $path;

    /**
     * @var \SimpleXMLElement
     */
    private $svnInfo;

    /**
     * @var SvnCommit
     */
    private $lastCommit;

    /**
     * Downloads the ICU data for the given version.
     *
     * @param string $url       The URL to download from.
     * @param string $targetDir The directory in which to store the repository.
     *
     * @return SvnRepository The directory where the data is stored.
     *
     * @throws RuntimeException If an error occurs during the download.
     */
    public static function download($url, $targetDir)
    {
        exec('which svn', $output, $result);

        if ($result !== 0) {
            throw new RuntimeException('The command "svn" is not installed.');
        }

        $filesystem = new Filesystem();

        if (!$filesystem->exists($targetDir . '/.svn')) {
            $filesystem->remove($targetDir);
            $filesystem->mkdir($targetDir);

            exec('svn checkout ' . $url . ' ' . $targetDir, $output, $result);

            if ($result !== 0) {
                throw new RuntimeException('The SVN checkout of ' . $url . 'failed.');
            }
        }

        return new static(realpath($targetDir));
    }

    /**
     * Reads the SVN repository at the given path.
     *
     * @param string $path The path to the repository.
     */
    public function __construct($path)
    {
        $this->path = $path;
    }

    /**
     * Returns the path to the repository.
     *
     * @return string The path to the repository.
     */
    public function getPath()
    {
        return $this->path;
    }

    /**
     * Returns the URL of the repository.
     *
     * @return string The URL of the repository.
     */
    public function getUrl()
    {
        return (string) $this->getSvnInfo()->entry->url;
    }

    /**
     * Returns the last commit of the repository.
     *
     * @return SvnCommit The last commit.
     */
    public function getLastCommit()
    {
        if (null === $this->lastCommit) {
            $this->lastCommit = new SvnCommit($this->getSvnInfo()->entry->commit);
        }

        return $this->lastCommit;
    }

    /**
     * Returns information about the SVN repository.
     *
     * @return \SimpleXMLElement The XML result from the "svn info" command.
     *
     * @throws RuntimeException If the "svn info" command failed.
     */
    private function getSvnInfo()
    {
        if (null === $this->svnInfo) {
            exec('svn info --xml '.$this->path, $output, $result);

            $svnInfo = simplexml_load_string(implode("\n", $output));

            if ($result !== 0) {
                throw new RuntimeException('svn info failed');
            }

            $this->svnInfo = $svnInfo;
        }

        return $this->svnInfo;
    }

}