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
|
|
|
return $this->cd_remote; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Set a starting directory for the remote process. |
63
|
|
|
* |
64
|
|
|
* @param string $cd_remote |
65
|
|
|
* |
66
|
|
|
* @return \Consolidation\SiteProcess\SiteProcess |
67
|
|
|
*/ |
68
|
|
|
public function setWorkingDirectory($cd_remote) { |
69
|
|
|
$this->cd_remote = $cd_remote; |
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set a starting directory for the initial/local process. |
75
|
|
|
* |
76
|
|
|
* @param string $cd |
77
|
|
|
* |
78
|
|
|
* @return \Consolidation\SiteProcess\SiteProcess |
79
|
|
|
*/ |
80
|
|
|
public function setWorkingDirectoryLocal($cd) { |
81
|
|
|
return parent::setWorkingDirectory($cd); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the starting directory for the initial/local process. |
86
|
|
|
* |
87
|
|
|
* @return string|null; |
|
|
|
|
88
|
|
|
*/ |
89
|
|
|
public function getWorkingDirectoryLocal() { |
90
|
|
|
return parent::getWorkingDirectory(); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* |
95
|
|
|
* @param bool $shouldUseSiteRoot |
96
|
|
|
* @return $this|\Symfony\Component\Process\Process |
97
|
|
|
* @throws \Exception |
98
|
|
|
*/ |
99
|
|
|
public function chdirToSiteRoot($shouldUseSiteRoot = true) |
100
|
|
|
{ |
101
|
|
|
if (!$shouldUseSiteRoot || !$this->siteAlias->hasRoot()) { |
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $this->setWorkingDirectory($this->siteAlias->root()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Take all of our individual arguments and process them for use. |
110
|
|
|
*/ |
111
|
|
|
protected function processArgs() |
112
|
|
|
{ |
113
|
|
|
$transport = $this->getTransport($this->siteAlias); |
114
|
|
|
$transport->configure($this); |
115
|
|
|
|
116
|
|
|
$processor = new ArgumentProcessor(); |
117
|
|
|
$selectedArgs = $processor->selectArgs( |
118
|
|
|
$this->siteAlias, |
119
|
|
|
$this->args, |
120
|
|
|
$this->options, |
121
|
|
|
$this->optionsPassedAsArgs |
122
|
|
|
); |
123
|
|
|
|
124
|
|
|
// Ask the transport to drop in a 'cd' if needed. |
125
|
|
|
if ($this->getWorkingDirectory()) { |
|
|
|
|
126
|
|
|
$selectedArgs = $transport->addChdir($this->getWorkingDirectory(), $selectedArgs); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// Do any necessary interpolation on the selected arguments. |
130
|
|
|
$processedArgs = $this->interpolate($selectedArgs); |
131
|
|
|
|
132
|
|
|
// Wrap the command with 'ssh' or some other transport if this is |
133
|
|
|
// a remote command; otherwise, leave it as-is. |
134
|
|
|
return $transport->wrap($processedArgs); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function setTransport($transport) |
138
|
|
|
{ |
139
|
|
|
$this->transport = $transport; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Ask the transport manager for the correct transport for the |
144
|
|
|
* provided alias. |
145
|
|
|
*/ |
146
|
|
|
protected function getTransport(AliasRecord $siteAlias) |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
return $this->transport; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @inheritDoc |
153
|
|
|
*/ |
154
|
|
|
public function getCommandLine() |
155
|
|
|
{ |
156
|
|
|
$commandLine = parent::getCommandLine(); |
157
|
|
|
if (empty($commandLine)) { |
158
|
|
|
$processedArgs = $this->processArgs(); |
159
|
|
|
$commandLine = Escape::argsForSite($this->siteAlias, $processedArgs); |
160
|
|
|
$commandLine = implode(' ', $commandLine); |
161
|
|
|
$this->setCommandLine($commandLine); |
162
|
|
|
} |
163
|
|
|
return $commandLine; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @inheritDoc |
168
|
|
|
*/ |
169
|
|
|
public function start(callable $callback = null, $env = array()) |
170
|
|
|
{ |
171
|
|
|
$cmd = $this->getCommandLine(); |
|
|
|
|
172
|
|
|
parent::start($callback, $env); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @inheritDoc |
177
|
|
|
*/ |
178
|
|
|
public function wait(callable $callback = null) |
179
|
|
|
{ |
180
|
|
|
$return = parent::wait($callback); |
181
|
|
|
return $return; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* interpolate examines each of the arguments in the provided argument list |
186
|
|
|
* and replaces any token found therein with the value for that key as |
187
|
|
|
* pulled from the given site alias. |
188
|
|
|
* |
189
|
|
|
* Example: "git -C {{root}} status" |
190
|
|
|
* |
191
|
|
|
* The token "{{root}}" will be converted to a value via $siteAlias->get('root'). |
192
|
|
|
* The result will replace the token. |
193
|
|
|
* |
194
|
|
|
* It is possible to use dot notation in the keys to access nested elements |
195
|
|
|
* within the site alias record. |
196
|
|
|
* |
197
|
|
|
* @param AliasRecord $siteAlias |
|
|
|
|
198
|
|
|
* @param type $args |
199
|
|
|
* @return type |
200
|
|
|
*/ |
201
|
|
|
protected function interpolate($args) |
202
|
|
|
{ |
203
|
|
|
$interpolator = new Interpolator(); |
204
|
|
|
return array_map( |
205
|
|
|
function ($arg) use ($interpolator) { |
206
|
|
|
if ($arg instanceof ShellOperatorInterface) { |
207
|
|
|
return $arg; |
208
|
|
|
} |
209
|
|
|
return $interpolator->interpolate($this->siteAlias, $arg, false); |
210
|
|
|
}, |
211
|
|
|
$args |
212
|
|
|
); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
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 theSon
calls the wrong method in the parent class.