1 | <?php |
||
27 | class CallerSSH2 implements CallerInterface |
||
28 | { |
||
29 | /** |
||
30 | * @var resource |
||
31 | */ |
||
32 | private $resource; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private $gitPath; |
||
38 | |||
39 | /** |
||
40 | * the output lines of the command |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | private $outputLines = array(); |
||
45 | |||
46 | /** |
||
47 | * @param resource $resource |
||
48 | * @param string $gitPath path of the git executable on the remote host |
||
49 | * |
||
50 | * @internal param string $host remote host |
||
51 | * @internal param int $port remote port |
||
52 | */ |
||
53 | public function __construct($resource, $gitPath = '/usr/bin/git') |
||
54 | { |
||
55 | $this->resource = $resource; |
||
56 | $this->gitPath = $gitPath; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * execute a command |
||
61 | * |
||
62 | * @param string $cmd the command |
||
63 | * @param bool $git prepend git to the command |
||
64 | * @param null|string $cwd directory where the command should be executed |
||
65 | * |
||
66 | * @return CallerInterface |
||
67 | */ |
||
68 | public function execute($cmd, $git = true, $cwd = null) |
||
69 | { |
||
70 | if ($git) { |
||
71 | $cmd = $this->gitPath . ' ' . $cmd; |
||
72 | } |
||
73 | $stream = ssh2_exec($this->resource, $cmd); |
||
74 | stream_set_blocking($stream, 1); |
||
75 | $data = stream_get_contents($stream); |
||
76 | fclose($stream); |
||
77 | // rtrim values |
||
78 | $values = array_map('rtrim', explode(PHP_EOL, $data)); |
||
79 | $this->outputLines = $values; |
||
80 | |||
81 | return $this; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * after calling execute this method should return the output |
||
86 | * |
||
87 | * @param bool $stripBlankLines strips the blank lines |
||
88 | * |
||
89 | * @return array |
||
90 | */ |
||
91 | public function getOutputLines($stripBlankLines = false) |
||
106 | } |
||
107 |