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; |
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
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function getCd() |
56
|
|
|
{ |
57
|
|
|
return $this->cd; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $cwd |
|
|
|
|
62
|
|
|
*/ |
63
|
|
|
public function setCd($cd) |
64
|
|
|
{ |
65
|
|
|
$this->cd = $cd; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* |
70
|
|
|
* @param bool $shouldUseSiteRoot |
71
|
|
|
* @return $this|\Symfony\Component\Process\Process |
72
|
|
|
* @throws \Exception |
73
|
|
|
* @deprecated. |
74
|
|
|
*/ |
75
|
|
|
public function chdirToSiteRoot($shouldUseSiteRoot = true) |
76
|
|
|
{ |
77
|
|
|
if (!$shouldUseSiteRoot || !$this->siteAlias->hasRoot()) { |
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this->setWorkingDirectory($this->siteAlias->root()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Take all of our individual arguments and process them for use. |
86
|
|
|
*/ |
87
|
|
|
protected function processArgs() |
88
|
|
|
{ |
89
|
|
|
$transport = $this->getTransport($this->siteAlias); |
90
|
|
|
$transport->configure($this); |
91
|
|
|
|
92
|
|
|
$processor = new ArgumentProcessor(); |
93
|
|
|
$selectedArgs = $processor->selectArgs( |
94
|
|
|
$this->siteAlias, |
95
|
|
|
$this->args, |
96
|
|
|
$this->options, |
97
|
|
|
$this->optionsPassedAsArgs |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
// Ask the transport to drop in a 'cd' if needed. |
101
|
|
|
if ($this->cd) { |
102
|
|
|
$selectedArgs = $transport->addChdir($this->cd, $selectedArgs, $this); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// Do any necessary interpolation on the selected arguments. |
106
|
|
|
$processedArgs = $this->interpolate($selectedArgs); |
107
|
|
|
|
108
|
|
|
// Wrap the command with 'ssh' or some other transport if this is |
109
|
|
|
// a remote command; otherwise, leave it as-is. |
110
|
|
|
return $transport->wrap($processedArgs); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function setTransport($transport) |
114
|
|
|
{ |
115
|
|
|
$this->transport = $transport; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Ask the transport manager for the correct transport for the |
120
|
|
|
* provided alias. |
121
|
|
|
*/ |
122
|
|
|
protected function getTransport(AliasRecord $siteAlias) |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
return $this->transport; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @inheritDoc |
129
|
|
|
*/ |
130
|
|
|
public function getCommandLine() |
131
|
|
|
{ |
132
|
|
|
$commandLine = parent::getCommandLine(); |
133
|
|
|
if (empty($commandLine)) { |
134
|
|
|
$processedArgs = $this->processArgs(); |
135
|
|
|
$commandLine = Escape::argsForSite($this->siteAlias, $processedArgs); |
136
|
|
|
$commandLine = implode(' ', $commandLine); |
137
|
|
|
$this->setCommandLine($commandLine); |
138
|
|
|
} |
139
|
|
|
return $commandLine; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @inheritDoc |
144
|
|
|
*/ |
145
|
|
|
public function start(callable $callback = null, $env = array()) |
146
|
|
|
{ |
147
|
|
|
$cmd = $this->getCommandLine(); |
|
|
|
|
148
|
|
|
parent::start($callback, $env); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @inheritDoc |
153
|
|
|
*/ |
154
|
|
|
public function wait(callable $callback = null) |
155
|
|
|
{ |
156
|
|
|
$return = parent::wait($callback); |
157
|
|
|
return $return; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* interpolate examines each of the arguments in the provided argument list |
162
|
|
|
* and replaces any token found therein with the value for that key as |
163
|
|
|
* pulled from the given site alias. |
164
|
|
|
* |
165
|
|
|
* Example: "git -C {{root}} status" |
166
|
|
|
* |
167
|
|
|
* The token "{{root}}" will be converted to a value via $siteAlias->get('root'). |
168
|
|
|
* The result will replace the token. |
169
|
|
|
* |
170
|
|
|
* It is possible to use dot notation in the keys to access nested elements |
171
|
|
|
* within the site alias record. |
172
|
|
|
* |
173
|
|
|
* @param AliasRecord $siteAlias |
|
|
|
|
174
|
|
|
* @param type $args |
175
|
|
|
* @return type |
176
|
|
|
*/ |
177
|
|
|
protected function interpolate($args) |
178
|
|
|
{ |
179
|
|
|
$interpolator = new Interpolator(); |
180
|
|
|
return array_map( |
181
|
|
|
function ($arg) use ($interpolator) { |
182
|
|
|
if ($arg instanceof ShellOperatorInterface) { |
183
|
|
|
return $arg; |
184
|
|
|
} |
185
|
|
|
return $interpolator->interpolate($this->siteAlias, $arg, false); |
186
|
|
|
}, |
187
|
|
|
$args |
188
|
|
|
); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.