Completed
Push — develop ( b29d0d...9c2be0 )
by
unknown
16s queued 11s
created

Caller::getRawOutput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * GitElephant - An abstraction layer for git written in PHP
5
 * Copyright (C) 2013  Matteo Giachino
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see [http://www.gnu.org/licenses/].
19
 */
20
21
namespace GitElephant\Command\Caller;
22
23
use GitElephant\Exception\InvalidRepositoryPathException;
24
use \Symfony\Component\Process\Process;
25
26
/**
27
 * Caller
28
 *
29
 * @author Matteo Giachino <[email protected]>
30
 * @author Tim Bernhard <[email protected]>
31
 */
32
class Caller extends AbstractCaller
33
{
34
    /**
35
     * the repository path
36
     *
37
     * @var string
38
     */
39
    private $repositoryPath;
40
41
    /**
42
     * raw output
43
     *
44
     * @var string
45
     */
46
    private $rawOutput;
47
48
    /**
49
     * Class constructor
50
     *
51
     * @param string|null   $gitPath the physical path to the git binary
52
     * @param string        $repositoryPath the physical base path for the repository
53
     */
54 107
    public function __construct($gitPath, $repositoryPath)
55
    {
56 107
        if (is_null($gitPath)) {
57
            // unix only!
58 107
            $gitPath = exec('which git');
59
        }
60 107
        $this->setBinaryPath($gitPath);
61
62 107
        if (!is_dir($repositoryPath)) {
63 1
            throw new InvalidRepositoryPathException($repositoryPath);
64
        }
65
66 107
        $this->repositoryPath = $repositoryPath;
67 107
    }
68
69
    /**
70
     * Executes a command
71
     *
72
     * @param string $cmd               the command to execute
73
     * @param bool   $git               if the command is git or a generic command
74
     * @param null   $cwd               the directory where the command must be executed
75
     * @param array  $acceptedExitCodes exit codes accepted to consider the command execution successful
76
     *
77
     * @throws \RuntimeException
78
     * @throws \Symfony\Component\Process\Exception\InvalidArgumentException
79
     * @throws \Symfony\Component\Process\Exception\ProcessTimedOutException
80
     * @throws \Symfony\Component\Process\Exception\RuntimeException
81
     * @throws \Symfony\Component\Process\Exception\LogicException
82
     * @return Caller
83
     */
84 102
    public function execute($cmd, $git = true, $cwd = null, $acceptedExitCodes = array(0)): \GitElephant\Command\Caller\CallerInterface
85
    {
86 102
        if ($git) {
87 102
            $cmd = $this->getBinaryPath() . ' ' . $cmd;
88
        }
89
90 102
        if (stripos(PHP_OS, 'WIN') !== 0) {
91
            // We rely on the C locale in all output we parse.
92 102
            $cmd = 'LC_ALL=C ' . $cmd;
93
        }
94
95 102
        if (is_null($cwd) || !is_dir($cwd)) {
96 102
            $cwd = $this->repositoryPath;
97
        }
98 102
        $process = Process::fromShellCommandline($cmd, $cwd);
99 102
        $process->setTimeout(15000);
100 102
        $process->run();
101 102
        if (!in_array($process->getExitCode(), $acceptedExitCodes)) {
102 1
            $text = 'Exit code: ' . $process->getExitCode();
103 1
            $text .= ' while executing: "' . $cmd;
104 1
            $text .= '" with reason: ' . $process->getErrorOutput();
105 1
            $text .= "\n" . $process->getOutput();
106 1
            throw new \RuntimeException($text);
107
        }
108 101
        $this->rawOutput = $process->getOutput();
109
110
        // rtrim values
111 101
        $values = array_map('rtrim', explode(PHP_EOL, $process->getOutput()));
112 101
        $this->outputLines = $values;
113
114 101
        return $this;
115
    }
116
117
    /**
118
     * returns the output of the last executed command
119
     *
120
     * @return string
121
     */
122 7
    public function getOutput(): string
123
    {
124 7
        return implode("\n", $this->outputLines);
125
    }
126
127
    /**
128
     * returns the output of the last executed command as an array of lines
129
     *
130
     * @param bool $stripBlankLines remove the blank lines
131
     *
132
     * @return array
133
     */
134 91
    public function getOutputLines($stripBlankLines = false): array
135
    {
136 91
        if ($stripBlankLines) {
137 81
            $output = array();
138 81
            foreach ($this->outputLines as $line) {
139 81
                if ('' !== $line) {
140 81
                    $output[] = $line;
141
                }
142
            }
143
144 81
            return $output;
145
        }
146
147 45
        return $this->outputLines;
148
    }
149
150
    /**
151
     * Get RawOutput
152
     *
153
     * @return string
154
     */
155 1
    public function getRawOutput(): string
156
    {
157 1
        return $this->rawOutput;
158
    }
159
}
160