|
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
|
|
|
/** |
|
24
|
|
|
* Caller via ssh2 PECL extension |
|
25
|
|
|
* |
|
26
|
|
|
* @author Matteo Giachino <[email protected]> |
|
27
|
|
|
* @author Tim Bernhard <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
class CallerSSH2 extends AbstractCaller |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var resource |
|
33
|
|
|
*/ |
|
34
|
|
|
private $resource; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param resource $resource |
|
38
|
|
|
* @param string $gitPath path of the git executable on the remote host |
|
39
|
|
|
* |
|
40
|
|
|
* @internal param string $host remote host |
|
41
|
|
|
* @internal param int $port remote port |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct($resource, $gitPath = '/usr/bin/git') |
|
44
|
|
|
{ |
|
45
|
|
|
$this->resource = $resource; |
|
46
|
|
|
$this->binaryPath = $gitPath; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* execute a command |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $cmd the command |
|
53
|
|
|
* @param bool $git prepend git to the command |
|
54
|
|
|
* @param null|string $cwd directory where the command should be executed |
|
55
|
|
|
* |
|
56
|
|
|
* @return CallerInterface |
|
57
|
|
|
*/ |
|
58
|
|
|
public function execute( |
|
59
|
|
|
$cmd, |
|
60
|
|
|
$git = true, |
|
61
|
|
|
$cwd = null |
|
62
|
|
|
): \GitElephant\Command\Caller\CallerInterface { |
|
63
|
|
|
if ($git) { |
|
64
|
|
|
$cmd = $this->getBinaryPath() . ' ' . $cmd; |
|
65
|
|
|
} |
|
66
|
|
|
$stream = ssh2_exec($this->resource, $cmd); |
|
67
|
|
|
stream_set_blocking($stream, true); |
|
68
|
|
|
$data = stream_get_contents($stream); |
|
69
|
|
|
fclose($stream); |
|
70
|
|
|
|
|
71
|
|
|
$this->rawOutput = $data === false ? '' : $data; |
|
72
|
|
|
// rtrim values |
|
73
|
|
|
$values = array_map('rtrim', explode(PHP_EOL, $this->rawOutput)); |
|
74
|
|
|
$this->outputLines = $values; |
|
75
|
|
|
|
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|