Completed
Pull Request — develop (#147)
by
unknown
12:57
created

Caller::getOutputLines()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 4
cts 4
cp 1
rs 9.7666
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4
1
<?php
2
/**
3
 * GitElephant - An abstraction layer for git written in PHP
4
 * Copyright (C) 2013  Matteo Giachino
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see [http://www.gnu.org/licenses/].
18
 */
19
20
namespace GitElephant\Command\Caller;
21
22
use GitElephant\Exception\InvalidRepositoryPathException;
23
use \GitElephant\GitBinary;
24
use \Symfony\Component\Process\Process;
25
26
/**
27
 * Caller
28
 *
29
 * @author Matteo Giachino <[email protected]>
30
 */
31
class Caller implements CallerInterface
32
{
33
    /**
34
     * GitBinary instance
35
     *
36
     * @var \GitElephant\GitBinary
37
     */
38
    private $binary;
39
40
    /**
41
     * the repository path
42
     *
43
     * @var string
44
     */
45
    private $repositoryPath;
46
47
    /**
48
     * the output lines of the command
49
     *
50
     * @var array
51
     */
52
    private $outputLines = array();
53
54
    /**
55
     * raw output
56
     *
57
     * @var string
58
     */
59
    private $rawOutput;
60
61
    /**
62
     * the output lines of the command
63
     *
64
     * @var array
65
     */
66
    private $errorLines = array();
67
68
    /**
69
     * raw output
70
     *
71
     * @var string
72
     */
73
    private $rawErrorOutput;
74
75
    /**
76
     * Class constructor
77
     *
78
     * @param \GitElephant\GitBinary $binary         the binary
79
     * @param string                 $repositoryPath the physical base path for the repository
80
     */
81 105
    public function __construct(GitBinary $binary, $repositoryPath)
82
    {
83 105
        $this->binary         = $binary;
84
85 105
        if (!is_dir($repositoryPath)) {
86 1
            throw new InvalidRepositoryPathException($repositoryPath);
87
        }
88
89 105
        $this->repositoryPath = $repositoryPath;
90 105
    }
91
92
    /**
93
     * Get the binary path
94
     *
95
     * @return mixed
96
     */
97 1
    public function getBinaryPath()
98
    {
99 1
        return $this->binary->getPath();
100
    }
101
102
    /**
103
     * Get the binary version
104
     *
105
     * @return mixed
106
     */
107
    public function getBinaryVersion()
108
    {
109
        return $this->binary->getVersion();
110
    }
111
112
    /**
113
     * Executes a command
114
     *
115
     * @param string $cmd               the command to execute
116
     * @param bool   $git               if the command is git or a generic command
117 99
     * @param null   $cwd               the directory where the command must be executed
118
     * @param array  $acceptedExitCodes exit codes accepted to consider the command execution successful
119 99
     *
120 99
     * @throws \RuntimeException
121
     * @throws \Symfony\Component\Process\Exception\InvalidArgumentException
122
     * @throws \Symfony\Component\Process\Exception\ProcessTimedOutException
123 99
     * @throws \Symfony\Component\Process\Exception\RuntimeException
124
     * @throws \Symfony\Component\Process\Exception\LogicException
125 99
     * @return Caller
126
     */
127
    public function execute($cmd, $git = true, $cwd = null, $acceptedExitCodes = array(0))
128 99
    {
129 99
        if ($git) {
130 99
            $cmd = $this->binary->getPath() . ' ' . $cmd;
131 99
        }
132 1
133 1
        if (stripos(PHP_OS, 'WIN') !== 0) {
134 1
            // We rely on the C locale in all output we parse.
135 1
            $cmd = 'LC_ALL=C ' . $cmd;
136 1
        }
137
138 98
        $process = new Process($cmd, is_null($cwd) ? $this->repositoryPath : $cwd);
139
        $process->setTimeout(15000);
140
        $process->run();
141 98
        if (!in_array($process->getExitCode(), $acceptedExitCodes)) {
142 98
            $text = 'Exit code: ' . $process->getExitCode();
143
            $text .= ' while executing: "' . $cmd;
144 98
            $text .= '" with reason: ' . $process->getErrorOutput();
145
            $text .= "\n" . $process->getOutput();
146
            throw new \RuntimeException($text);
147
        }
148
        $this->rawOutput = $process->getOutput();
149
150
        // rtrim values
151
        $values = array_map('rtrim', explode(PHP_EOL, $process->getOutput()));
152 2
        $this->outputLines = $values;
153
154 2
        return $this;
155
    }
156
157
    /**
158
     * returns the output of the last executed command
159
     *
160
     * @return string
161
     */
162
    public function getOutput()
163
    {
164 91
        return implode("\n", $this->outputLines);
165
    }
166 91
167 81
    /**
168 81
     * returns the output of the last executed command as an array of lines
169 81
     *
170 81
     * @param bool $stripBlankLines remove the blank lines
171
     *
172
     * @return array
173
     */
174 81
    public function getOutputLines($stripBlankLines = false)
175
    {
176
        if ($stripBlankLines) {
177 45
            $output = array();
178
            foreach ($this->outputLines as $line) {
179
                if ('' !== $line) {
180
                    $output[] = $line;
181
                }
182
            }
183
184
            return $output;
185 1
        }
186
187 1
        return $this->outputLines;
188
    }
189
190
    /**
191
     * Get RawOutput
192
     *
193
     * @return string
194
     */
195
    public function getRawOutput()
196
    {
197
        return $this->rawOutput;
198
    }
199
200
    /**
201
     * returns the error output of the last executed command
202
     *
203
     * @return string
204
     */
205
    public function getErrorOutput()
206
    {
207
        return implode("\n", $this->errorLines);
208
    }
209
210
    /**
211
     * returns the error output of the last executed command as an array of lines
212
     *
213
     * @param bool $stripBlankLines remove the blank lines
214
     *
215
     * @return array
216
     */
217
    public function getErrorLines($stripBlankLines = false)
218
    {
219
        if ($stripBlankLines) {
220
            $output = array();
221
            foreach ($this->errorLines as $line) {
222
                if ('' !== $line) {
223
                    $output[] = $line;
224
                }
225
            }
226
227
            return $output;
228
        }
229
230
        return $this->errorLines;
231
    }
232
233
    /**
234
     * Get RawErrorOutput
235
     *
236
     * @return string
237
     */
238
    public function getRawErrorOutput()
239
    {
240
        return $this->rawErrorOutput;
241
    }
242
}
243