-
Notifications
You must be signed in to change notification settings - Fork 21
/
Connection.php
262 lines (229 loc) · 5.32 KB
/
Connection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php namespace Illuminate\Remote;
use Closure;
use Illuminate\Filesystem\Filesystem;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
class Connection implements ConnectionInterface {
/**
* The SSH gateway implementation.
*
* @var \Illuminate\Remote\GatewayInterface
*/
protected $gateway;
/**
* The name of the connection.
*
* @var string
*/
protected $name;
/**
* The host name of the server.
*
* @var string
*/
protected $host;
/**
* The username for the connection.
*
* @var string
*/
protected $username;
/**
* All of the defined tasks.
*
* @var array
*/
protected $tasks = array();
/**
* The output implementation for the connection.
*
* @var \Symfony\Component\Console\Output\OutputInterface
*/
protected $output;
/**
* Create a new SSH connection instance.
*
* @param string $name
* @param string $host
* @param string $username
* @param array $auth
* @param \Illuminate\Remote\GatewayInterface
* @param
*/
public function __construct($name, $host, $username, array $auth, GatewayInterface $gateway = null)
{
$this->name = $name;
$this->host = $host;
$this->username = $username;
$this->gateway = $gateway ?: new SecLibGateway($host, $auth, new Filesystem);
}
/**
* Define a set of commands as a task.
*
* @param string $task
* @param string|array $commands
* @return void
*/
public function define($task, $commands)
{
$this->tasks[$task] = $commands;
}
/**
* Run a task against the connection.
*
* @param string $task
* @param \Closure $callback
* @return void
*/
public function task($task, Closure $callback = null)
{
if (isset($this->tasks[$task]))
{
return $this->run($this->tasks[$task], $callback);
}
}
/**
* Run a set of commands against the connection.
*
* @param string|array $commands
* @param \Closure $callback
* @return void
*/
public function run($commands, Closure $callback = null)
{
// First, we will initialize the SSH gateway, and then format the commands so
// they can be run. Once we have the commands formatted and the server is
// ready to go we will just fire off these commands against the server.
$gateway = $this->getGateway();
$callback = $this->getCallback($callback);
$gateway->run($this->formatCommands($commands));
// After running the commands against the server, we will continue to ask for
// the next line of output that is available, and write it them out using
// our callback. Once we hit the end of output, we'll bail out of here.
while (true)
{
if (is_null($line = $gateway->nextLine())) break;
call_user_func($callback, $line, $this);
}
}
/**
* Download the contents of a remote file.
*
* @param string $remote
* @param string $local
* @return void
*/
public function get($remote, $local)
{
$this->getGateway()->get($remote, $local);
}
/**
* Get the contents of a remote file.
*
* @param string $remote
* @return string
*/
public function getString($remote)
{
return $this->getGateway()->getString($remote);
}
/**
* Upload a local file to the server.
*
* @param string $local
* @param string $remote
* @return void
*/
public function put($local, $remote)
{
$this->getGateway()->put($local, $remote);
}
/**
* Upload a string to to the given file on the server.
*
* @param string $remote
* @param string $contents
* @return void
*/
public function putString($remote, $contents)
{
$this->getGateway()->putString($remote, $contents);
}
/**
* Display the given line using the default output.
*
* @param string $line
* @return void
*/
public function display($line)
{
$server = $this->username.'@'.$this->host;
$lead = '<comment>['.$server.']</comment> <info>('.$this->name.')</info>';
$this->getOutput()->writeln($lead.' '.$line);
}
/**
* Format the given command set.
*
* @param string|array $commands
* @return string
*/
protected function formatCommands($commands)
{
return is_array($commands) ? implode(' && ', $commands) : $commands;
}
/**
* Get the display callback for the connection.
*
* @param \Closure|null $callback
* @return \Closure
*/
protected function getCallback($callback)
{
if ( ! is_null($callback)) return $callback;
return function($line) { $this->display($line); };
}
/**
* Get the exit status of the last command.
*
* @return int|bool
*/
public function status()
{
return $this->gateway->status();
}
/**
* Get the gateway implementation.
*
* @return \Illuminate\Remote\GatewayInterface
*
* @throws \RuntimeException
*/
public function getGateway()
{
if ( ! $this->gateway->connected() && ! $this->gateway->connect($this->username))
{
throw new \RuntimeException("Unable to connect to remote server.");
}
return $this->gateway;
}
/**
* Get the output implementation for the connection.
*
* @return \Symfony\Component\Console\Output\OutputInterface
*/
public function getOutput()
{
if (is_null($this->output)) $this->output = new NullOutput;
return $this->output;
}
/**
* Set the output implementation.
*
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return void
*/
public function setOutput(OutputInterface $output)
{
$this->output = $output;
}
}