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 \Symfony\Component\Process\Process; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Caller |
27
|
|
|
* |
28
|
|
|
* @author Matteo Giachino <[email protected]> |
29
|
|
|
* @author Tim Bernhard <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class Caller extends AbstractCaller |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* the repository path |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $repositoryPath; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* raw output |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $rawOutput; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Class constructor |
49
|
|
|
* |
50
|
|
|
* @param string|null $gitPath the physical path to the git binary |
51
|
|
|
* @param string $repositoryPath the physical base path for the repository |
52
|
|
|
*/ |
53
|
107 |
|
public function __construct($gitPath, $repositoryPath) |
54
|
|
|
{ |
55
|
107 |
|
if (is_null($gitPath)) { |
56
|
|
|
// unix only! |
57
|
107 |
|
$gitPath = exec('which git'); |
58
|
|
|
} |
59
|
107 |
|
$this->setBinaryPath($gitPath); |
60
|
|
|
|
61
|
107 |
|
if (!is_dir($repositoryPath)) { |
62
|
1 |
|
throw new InvalidRepositoryPathException($repositoryPath); |
63
|
|
|
} |
64
|
|
|
|
65
|
107 |
|
$this->repositoryPath = $repositoryPath; |
66
|
107 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Executes a command |
70
|
|
|
* |
71
|
|
|
* @param string $cmd the command to execute |
72
|
|
|
* @param bool $git if the command is git or a generic command |
73
|
|
|
* @param null $cwd the directory where the command must be executed |
74
|
|
|
* @param array $acceptedExitCodes exit codes accepted to consider the command execution successful |
75
|
|
|
* |
76
|
|
|
* @throws \RuntimeException |
77
|
|
|
* @throws \Symfony\Component\Process\Exception\InvalidArgumentException |
78
|
|
|
* @throws \Symfony\Component\Process\Exception\ProcessTimedOutException |
79
|
|
|
* @throws \Symfony\Component\Process\Exception\RuntimeException |
80
|
|
|
* @throws \Symfony\Component\Process\Exception\LogicException |
81
|
|
|
* @return Caller |
82
|
|
|
*/ |
83
|
102 |
|
public function execute($cmd, $git = true, $cwd = null, $acceptedExitCodes = array(0)) |
84
|
|
|
{ |
85
|
102 |
|
if ($git) { |
86
|
102 |
|
$cmd = $this->getBinaryPath() . ' ' . $cmd; |
87
|
|
|
} |
88
|
|
|
|
89
|
102 |
|
if (stripos(PHP_OS, 'WIN') !== 0) { |
90
|
|
|
// We rely on the C locale in all output we parse. |
91
|
102 |
|
$cmd = 'LC_ALL=C ' . $cmd; |
92
|
|
|
} |
93
|
|
|
|
94
|
102 |
|
$process = new Process($cmd, is_null($cwd) ? $this->repositoryPath : $cwd); |
95
|
102 |
|
$process->setTimeout(15000); |
96
|
102 |
|
$process->run(); |
97
|
102 |
|
if (!in_array($process->getExitCode(), $acceptedExitCodes)) { |
98
|
1 |
|
$text = 'Exit code: ' . $process->getExitCode(); |
99
|
1 |
|
$text .= ' while executing: "' . $cmd; |
100
|
1 |
|
$text .= '" with reason: ' . $process->getErrorOutput(); |
101
|
1 |
|
$text .= "\n" . $process->getOutput(); |
102
|
1 |
|
throw new \RuntimeException($text); |
103
|
|
|
} |
104
|
101 |
|
$this->rawOutput = $process->getOutput(); |
105
|
|
|
|
106
|
|
|
// rtrim values |
107
|
101 |
|
$values = array_map('rtrim', explode(PHP_EOL, $process->getOutput())); |
108
|
101 |
|
$this->outputLines = $values; |
109
|
|
|
|
110
|
101 |
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* returns the output of the last executed command |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
7 |
|
public function getOutput() |
119
|
|
|
{ |
120
|
7 |
|
return implode("\n", $this->outputLines); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* returns the output of the last executed command as an array of lines |
125
|
|
|
* |
126
|
|
|
* @param bool $stripBlankLines remove the blank lines |
127
|
|
|
* |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
91 |
|
public function getOutputLines($stripBlankLines = false) |
131
|
|
|
{ |
132
|
91 |
|
if ($stripBlankLines) { |
133
|
81 |
|
$output = array(); |
134
|
81 |
|
foreach ($this->outputLines as $line) { |
135
|
81 |
|
if ('' !== $line) { |
136
|
81 |
|
$output[] = $line; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
81 |
|
return $output; |
141
|
|
|
} |
142
|
|
|
|
143
|
45 |
|
return $this->outputLines; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get RawOutput |
148
|
|
|
* |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
1 |
|
public function getRawOutput() |
152
|
|
|
{ |
153
|
1 |
|
return $this->rawOutput; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|