1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TemplesOfCode\CodeSanity\Location; |
4
|
|
|
|
5
|
|
|
use TemplesOfCode\CodeSanity\RemoteConnection; |
6
|
|
|
use TemplesOfCode\CodeSanity\Location; |
7
|
|
|
use TemplesOfCode\CodeSanity\RosterItem; |
8
|
|
|
use TemplesOfCode\Sofa\Command\ShellCommand; |
9
|
|
|
use TemplesOfCode\Sofa\CommandChain; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class RemoteLocation |
13
|
|
|
* @package TemplesOfCode\CodeSanity\Location |
14
|
|
|
*/ |
15
|
|
|
class RemoteLocation extends Location |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var RemoteConnection |
19
|
|
|
*/ |
20
|
|
|
protected $remoteConnection = null; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @return RemoteConnection |
24
|
|
|
*/ |
25
|
14 |
|
public function getRemoteConnection() |
26
|
|
|
{ |
27
|
14 |
|
return $this->remoteConnection; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param RemoteConnection $remoteConnection |
32
|
|
|
* @return $this |
33
|
|
|
*/ |
34
|
14 |
|
public function setRemoteConnection(RemoteConnection $remoteConnection) |
35
|
|
|
{ |
36
|
14 |
|
$this->remoteConnection = $remoteConnection; |
37
|
14 |
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
4 |
|
public function isValid() |
44
|
|
|
{ |
45
|
|
|
/** |
46
|
|
|
* @var RemoteConnection|null $remoteConnection |
47
|
|
|
*/ |
48
|
4 |
|
$remoteConnection = $this->getRemoteConnection(); |
49
|
4 |
|
if (empty($remoteConnection)) { |
50
|
1 |
|
return false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var bool $validRemoteConnection |
55
|
|
|
*/ |
56
|
3 |
|
$validRemoteConnection = $this |
57
|
|
|
->remoteConnection |
58
|
3 |
|
->isValid() |
59
|
3 |
|
; |
60
|
|
|
|
61
|
3 |
|
if (!$validRemoteConnection) { |
62
|
1 |
|
return false; |
63
|
|
|
} |
64
|
|
|
/** |
65
|
|
|
* @var bool $validRemoteLocation |
66
|
|
|
*/ |
67
|
2 |
|
$validRemoteLocation = $this->isValidRemoteDirectory(); |
68
|
2 |
|
if (!$validRemoteLocation) { |
69
|
1 |
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
return true; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
3 |
|
public function isValidRemoteDirectory() |
79
|
|
|
{ |
80
|
|
|
/** |
81
|
|
|
* @var RemoteConnection|null $remoteConnection |
82
|
|
|
*/ |
83
|
3 |
|
$remoteConnection = $this->getRemoteConnection(); |
84
|
3 |
|
if (empty($remoteConnection)) { |
85
|
1 |
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @var CommandChain $commandChain |
90
|
|
|
*/ |
91
|
2 |
|
$commandChain = $this->buildTestCommandChain($remoteConnection); |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @var int $exitStatus |
95
|
|
|
*/ |
96
|
|
|
list( |
97
|
|
|
$exitStatus |
98
|
2 |
|
) = $commandChain->execute(false); |
99
|
|
|
|
100
|
2 |
|
if ($exitStatus) { |
101
|
1 |
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
return true; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param RemoteConnection $remoteConnection |
109
|
|
|
* @return CommandChain |
110
|
|
|
*/ |
111
|
3 |
|
protected function buildTestCommandChain(RemoteConnection $remoteConnection) |
112
|
|
|
{ |
113
|
|
|
/** |
114
|
|
|
* @var ShellCommand $sshCommand |
115
|
|
|
*/ |
116
|
3 |
|
$sshCommand = $remoteConnection->getCommand(true); |
117
|
|
|
|
118
|
3 |
|
$sshCommand->addParameter(sprintf( |
119
|
3 |
|
'"test -e %s"', |
120
|
3 |
|
$this->directory |
121
|
3 |
|
)); |
122
|
|
|
|
123
|
3 |
|
$sequencedCommandChain = new CommandChain(';'); |
124
|
|
|
$sequencedCommandChain |
125
|
3 |
|
->addCommand($sshCommand) |
126
|
|
|
; |
127
|
|
|
|
128
|
3 |
|
return $sequencedCommandChain; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return CommandChain |
133
|
|
|
*/ |
134
|
3 |
|
public function getRosterListCommand() |
135
|
|
|
{ |
136
|
3 |
|
if (!$this->isValid()) { |
137
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
138
|
1 |
|
"Remote location validation failed for Location with directory '%s'", |
139
|
1 |
|
$this->directory |
140
|
1 |
|
)); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @var RemoteConnection $remoteConnection |
145
|
|
|
*/ |
146
|
2 |
|
$remoteConnection = $this->getRemoteConnection(); |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @var CommandChain $sshCommandChain |
150
|
|
|
*/ |
151
|
2 |
|
$sshCommandChain = $this->buildSshCommandChain($remoteConnection); |
152
|
|
|
|
153
|
2 |
|
return $sshCommandChain; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Auxiliary function. |
158
|
|
|
* |
159
|
|
|
* @param RemoteConnection $remoteConnection |
160
|
|
|
* @return ShellCommand |
161
|
|
|
*/ |
162
|
3 |
|
protected function buildSshCommandChain(RemoteConnection $remoteConnection) |
163
|
|
|
{ |
164
|
|
|
/** |
165
|
|
|
* @var CommandChain $commandChain |
166
|
|
|
*/ |
167
|
3 |
|
$commandChain = $this->buildRemoteCommandChain(); |
168
|
|
|
|
169
|
3 |
|
$remoteCommand = $commandChain->getCommand(); |
170
|
3 |
|
$remoteCommand = str_replace('\\', '\\\\', $remoteCommand); |
171
|
|
|
|
172
|
3 |
|
$sshCommand = $remoteConnection->getCommand(true); |
173
|
3 |
|
$sshCommand->addParameter(sprintf( |
174
|
3 |
|
'"%s"', |
175
|
|
|
$remoteCommand |
176
|
3 |
|
)); |
177
|
|
|
|
178
|
3 |
|
return $sshCommand; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Auxiliary function. |
183
|
|
|
* @return CommandChain |
184
|
|
|
*/ |
185
|
3 |
|
protected function buildRemoteCommandChain() |
186
|
|
|
{ |
187
|
|
|
/** |
188
|
|
|
* @var CommandChain $pipeChainedCommands |
189
|
|
|
*/ |
190
|
3 |
|
$pipeChainedCommands = $this->buildPipeChainedCommands(); |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @var CommandChain $sequenceChainedCommands |
194
|
|
|
*/ |
195
|
3 |
|
$sequenceChainedCommands = $this->buildSequenceChainedCommands(); |
196
|
3 |
|
$sequenceChainedCommands->addCommand($pipeChainedCommands); |
197
|
|
|
|
198
|
3 |
|
return $sequenceChainedCommands; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* {@inheritdoc} |
203
|
|
|
*/ |
204
|
1 |
|
public function getName() |
205
|
|
|
{ |
206
|
1 |
|
if (empty($this->name)) { |
207
|
|
|
/** |
208
|
|
|
* @var RemoteConnection |
209
|
|
|
*/ |
210
|
1 |
|
$remoteConnection = $this->getRemoteConnection(); |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @var string |
214
|
|
|
*/ |
215
|
1 |
|
$this->name = sprintf( |
216
|
1 |
|
'%s@%s:%s', |
217
|
1 |
|
$remoteConnection->getUsername(), |
218
|
1 |
|
$remoteConnection->getHost(), |
219
|
1 |
|
$this->getDirectory() |
220
|
1 |
|
); |
221
|
1 |
|
} |
222
|
1 |
|
return $this->name; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param RosterItem $rosterItem |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
1 |
|
public function getFullPath(RosterItem $rosterItem) |
231
|
|
|
{ |
232
|
|
|
/** |
233
|
|
|
* @var RemoteConnection $remoteConnection |
234
|
|
|
*/ |
235
|
1 |
|
$remoteConnection = $this->getRemoteConnection(); |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @var string $targetFileName |
239
|
|
|
*/ |
240
|
1 |
|
$targetFileName = $rosterItem->getRelativeFileName(); |
241
|
|
|
|
242
|
1 |
|
$fullPath = sprintf( |
243
|
1 |
|
'%s@%s:%s', |
244
|
1 |
|
$remoteConnection->getUsername(), |
245
|
1 |
|
$remoteConnection->getHost(), |
246
|
1 |
|
$this->getDirectory() . '/' . $targetFileName |
247
|
1 |
|
); |
248
|
|
|
|
249
|
1 |
|
return $fullPath; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|