|
1
|
|
|
<?php |
|
2
|
|
|
namespace Consolidation\SiteProcess; |
|
3
|
|
|
|
|
4
|
|
|
use Consolidation\SiteAlias\AliasRecord; |
|
5
|
|
|
use Consolidation\SiteProcess\Transport\DockerComposeTransport; |
|
6
|
|
|
use Consolidation\SiteProcess\Util\ArgumentProcessor; |
|
7
|
|
|
use Consolidation\SiteProcess\Transport\LocalTransport; |
|
8
|
|
|
use Consolidation\SiteProcess\Transport\SshTransport; |
|
9
|
|
|
use Consolidation\SiteProcess\Transport\TransportInterface; |
|
10
|
|
|
use Consolidation\Config\Util\Interpolator; |
|
11
|
|
|
use Consolidation\SiteProcess\Util\ShellOperatorInterface; |
|
12
|
|
|
use Consolidation\SiteProcess\Util\Escape; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* A wrapper around Symfony Process that uses site aliases |
|
16
|
|
|
* (https://github.com/consolidation/site-alias) |
|
17
|
|
|
* |
|
18
|
|
|
* - Interpolate arguments using values from the alias |
|
19
|
|
|
* e.g. `$process = new SiteProcess($alias, ['git', '-C', '{{root}}']);` |
|
20
|
|
|
* - Make remote calls via ssh as if they were local. |
|
21
|
|
|
*/ |
|
22
|
|
|
class SiteProcess extends ProcessBase |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var AliasRecord */ |
|
25
|
|
|
protected $siteAlias; |
|
26
|
|
|
/** @var string[] */ |
|
27
|
|
|
protected $args; |
|
28
|
|
|
/** @var string[] */ |
|
29
|
|
|
protected $options; |
|
30
|
|
|
/** @var string[] */ |
|
31
|
|
|
protected $optionsPassedAsArgs; |
|
32
|
|
|
/** @var string */ |
|
33
|
|
|
protected $cd_remote; |
|
34
|
|
|
/** @var TransportInterface */ |
|
35
|
|
|
protected $transport; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Process arguments and options per the site alias and build the |
|
39
|
|
|
* actual command to run. |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(AliasRecord $siteAlias, TransportInterface $transport, $args, $options = [], $optionsPassedAsArgs = []) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->siteAlias = $siteAlias; |
|
44
|
|
|
$this->transport = $transport; |
|
45
|
|
|
$this->args = $args; |
|
46
|
|
|
$this->options = $options; |
|
47
|
|
|
$this->optionsPassedAsArgs = $optionsPassedAsArgs; |
|
48
|
|
|
|
|
49
|
|
|
parent::__construct([]); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Get a starting directory for the remote process. |
|
54
|
|
|
* |
|
55
|
|
|
* @return string|null |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getWorkingDirectory() |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->cd_remote; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Set a starting directory for the remote process. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $cd_remote |
|
66
|
|
|
* |
|
67
|
|
|
* @return \Consolidation\SiteProcess\SiteProcess |
|
68
|
|
|
*/ |
|
69
|
|
|
public function setWorkingDirectory($cd_remote) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->cd_remote = $cd_remote; |
|
72
|
|
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Set a starting directory for the initial/local process. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $cd |
|
79
|
|
|
* |
|
80
|
|
|
* @return \Consolidation\SiteProcess\SiteProcess |
|
81
|
|
|
*/ |
|
82
|
|
|
public function setWorkingDirectoryLocal($cd) |
|
83
|
|
|
{ |
|
84
|
|
|
return parent::setWorkingDirectory($cd); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Get the starting directory for the initial/local process. |
|
89
|
|
|
* |
|
90
|
|
|
* @return string|null; |
|
|
|
|
|
|
91
|
|
|
*/ |
|
92
|
|
|
public function getWorkingDirectoryLocal() |
|
93
|
|
|
{ |
|
94
|
|
|
return parent::getWorkingDirectory(); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* |
|
99
|
|
|
* @param bool $shouldUseSiteRoot |
|
100
|
|
|
* @return $this|\Symfony\Component\Process\Process |
|
101
|
|
|
* @throws \Exception |
|
102
|
|
|
*/ |
|
103
|
|
|
public function chdirToSiteRoot($shouldUseSiteRoot = true) |
|
104
|
|
|
{ |
|
105
|
|
|
if (!$shouldUseSiteRoot || !$this->siteAlias->hasRoot()) { |
|
106
|
|
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $this->setWorkingDirectory($this->siteAlias->root()); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Take all of our individual arguments and process them for use. |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function processArgs() |
|
116
|
|
|
{ |
|
117
|
|
|
$transport = $this->getTransport($this->siteAlias); |
|
118
|
|
|
$transport->configure($this); |
|
119
|
|
|
|
|
120
|
|
|
$processor = new ArgumentProcessor(); |
|
121
|
|
|
$selectedArgs = $processor->selectArgs( |
|
122
|
|
|
$this->siteAlias, |
|
123
|
|
|
$this->args, |
|
124
|
|
|
$this->options, |
|
125
|
|
|
$this->optionsPassedAsArgs |
|
126
|
|
|
); |
|
127
|
|
|
|
|
128
|
|
|
// Ask the transport to drop in a 'cd' if needed. |
|
129
|
|
|
if ($this->getWorkingDirectory()) { |
|
|
|
|
|
|
130
|
|
|
$selectedArgs = $transport->addChdir($this->getWorkingDirectory(), $selectedArgs); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
// Do any necessary interpolation on the selected arguments. |
|
134
|
|
|
$processedArgs = $this->interpolate($selectedArgs); |
|
135
|
|
|
|
|
136
|
|
|
// Wrap the command with 'ssh' or some other transport if this is |
|
137
|
|
|
// a remote command; otherwise, leave it as-is. |
|
138
|
|
|
return $transport->wrap($processedArgs); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function setTransport($transport) |
|
142
|
|
|
{ |
|
143
|
|
|
$this->transport = $transport; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Ask the transport manager for the correct transport for the |
|
148
|
|
|
* provided alias. |
|
149
|
|
|
*/ |
|
150
|
|
|
protected function getTransport(AliasRecord $siteAlias) |
|
|
|
|
|
|
151
|
|
|
{ |
|
152
|
|
|
return $this->transport; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @inheritDoc |
|
157
|
|
|
*/ |
|
158
|
|
|
public function getCommandLine() |
|
159
|
|
|
{ |
|
160
|
|
|
$commandLine = parent::getCommandLine(); |
|
161
|
|
|
if (empty($commandLine)) { |
|
162
|
|
|
$processedArgs = $this->processArgs(); |
|
163
|
|
|
$commandLine = Escape::argsForSite($this->siteAlias, $processedArgs); |
|
164
|
|
|
$commandLine = implode(' ', $commandLine); |
|
165
|
|
|
$this->setCommandLine($commandLine); |
|
166
|
|
|
} |
|
167
|
|
|
return $commandLine; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @inheritDoc |
|
172
|
|
|
*/ |
|
173
|
|
|
public function start(callable $callback = null, $env = array()) |
|
174
|
|
|
{ |
|
175
|
|
|
$cmd = $this->getCommandLine(); |
|
|
|
|
|
|
176
|
|
|
parent::start($callback, $env); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @inheritDoc |
|
181
|
|
|
*/ |
|
182
|
|
|
public function wait(callable $callback = null) |
|
183
|
|
|
{ |
|
184
|
|
|
$return = parent::wait($callback); |
|
185
|
|
|
return $return; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* interpolate examines each of the arguments in the provided argument list |
|
190
|
|
|
* and replaces any token found therein with the value for that key as |
|
191
|
|
|
* pulled from the given site alias. |
|
192
|
|
|
* |
|
193
|
|
|
* Example: "git -C {{root}} status" |
|
194
|
|
|
* |
|
195
|
|
|
* The token "{{root}}" will be converted to a value via $siteAlias->get('root'). |
|
196
|
|
|
* The result will replace the token. |
|
197
|
|
|
* |
|
198
|
|
|
* It is possible to use dot notation in the keys to access nested elements |
|
199
|
|
|
* within the site alias record. |
|
200
|
|
|
* |
|
201
|
|
|
* @param AliasRecord $siteAlias |
|
|
|
|
|
|
202
|
|
|
* @param type $args |
|
203
|
|
|
* @return type |
|
204
|
|
|
*/ |
|
205
|
|
|
protected function interpolate($args) |
|
206
|
|
|
{ |
|
207
|
|
|
$interpolator = new Interpolator(); |
|
208
|
|
|
return array_map( |
|
209
|
|
|
function ($arg) use ($interpolator) { |
|
210
|
|
|
if ($arg instanceof ShellOperatorInterface) { |
|
211
|
|
|
return $arg; |
|
212
|
|
|
} |
|
213
|
|
|
return $interpolator->interpolate($this->siteAlias, $arg, false); |
|
214
|
|
|
}, |
|
215
|
|
|
$args |
|
216
|
|
|
); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.