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