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\Config\Util\Interpolator; |
10
|
|
|
use Consolidation\SiteProcess\Util\ShellOperatorInterface; |
11
|
|
|
use Consolidation\SiteProcess\Util\Escape; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* A wrapper around Symfony Process that uses site aliases |
15
|
|
|
* (https://github.com/consolidation/site-alias) |
16
|
|
|
* |
17
|
|
|
* - Interpolate arguments using values from the alias |
18
|
|
|
* e.g. `$process = new SiteProcess($alias, ['git', '-C', '{{root}}']);` |
19
|
|
|
* - Make remote calls via ssh as if they were local. |
20
|
|
|
*/ |
21
|
|
|
class SiteProcess extends ProcessBase |
22
|
|
|
{ |
23
|
|
|
/** @var AliasRecord */ |
24
|
|
|
protected $siteAlias; |
25
|
|
|
/** @var string[] */ |
26
|
|
|
protected $args; |
27
|
|
|
/** @var string[] */ |
28
|
|
|
protected $options; |
29
|
|
|
/** @var string[] */ |
30
|
|
|
protected $optionsPassedAsArgs; |
31
|
|
|
/** @var string */ |
32
|
|
|
protected $cd; |
33
|
|
|
/** @var TransportManager */ |
34
|
|
|
protected $transportManager; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Process arguments and options per the site alias and build the |
38
|
|
|
* actual command to run. |
39
|
|
|
*/ |
40
|
|
|
public function __construct(AliasRecord $siteAlias, $args, $options = [], $optionsPassedAsArgs = []) |
41
|
|
|
{ |
42
|
|
|
$this->siteAlias = $siteAlias; |
43
|
|
|
$this->args = $args; |
44
|
|
|
$this->options = $options; |
45
|
|
|
$this->optionsPassedAsArgs = $optionsPassedAsArgs; |
46
|
|
|
|
47
|
|
|
parent::__construct([]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritdoc |
52
|
|
|
*/ |
53
|
|
|
public function setWorkingDirectory($cwd) |
54
|
|
|
{ |
55
|
|
|
$this->cd = $cwd; |
56
|
|
|
return parent::setWorkingDirectory($cwd); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function chdirToSiteRoot($shouldUseSiteRoot = true) |
60
|
|
|
{ |
61
|
|
|
if (!$shouldUseSiteRoot || !$this->siteAlias->hasRoot()) { |
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this->setWorkingDirectory($this->siteAlias->root()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Take all of our individual arguments and process them for use. |
70
|
|
|
*/ |
71
|
|
|
protected function processArgs() |
72
|
|
|
{ |
73
|
|
|
$transport = $this->getTransport($this->siteAlias); |
74
|
|
|
$transport->configure($this); |
75
|
|
|
|
76
|
|
|
$processor = new ArgumentProcessor(); |
77
|
|
|
$selectedArgs = $processor->selectArgs( |
78
|
|
|
$this->siteAlias, |
79
|
|
|
$this->args, |
80
|
|
|
$this->options, |
81
|
|
|
$this->optionsPassedAsArgs |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
// Ask the transport to drop in a 'cd' if needed. |
85
|
|
|
if ($this->cd) { |
86
|
|
|
$selectedArgs = $transport->addChdir($this->cd, $selectedArgs); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// Do any necessary interpolation on the selected arguments. |
90
|
|
|
$processedArgs = $this->interpolate($selectedArgs); |
91
|
|
|
|
92
|
|
|
// Wrap the command with 'ssh' or some other transport if this is |
93
|
|
|
// a remote command; otherwise, leave it as-is. |
94
|
|
|
return $transport->wrap($processedArgs); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function setTransportManager($transportManager) |
98
|
|
|
{ |
99
|
|
|
$this->transportManager = $transportManager; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected function getTransportManager() |
103
|
|
|
{ |
104
|
|
|
if (!$this->transportManager) { |
105
|
|
|
$this->transportManager = TransportManager::createDefault(); |
106
|
|
|
} |
107
|
|
|
return $this->transportManager; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Ask the transport manager for the correct transport for the |
112
|
|
|
* provided alias. |
113
|
|
|
*/ |
114
|
|
|
protected function getTransport(AliasRecord $siteAlias) |
115
|
|
|
{ |
116
|
|
|
return $this->getTransportManager()->getTransport($siteAlias); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @inheritDoc |
121
|
|
|
*/ |
122
|
|
|
public function getCommandLine() |
123
|
|
|
{ |
124
|
|
|
$commandLine = parent::getCommandLine(); |
125
|
|
|
if (empty($commandLine)) { |
126
|
|
|
$processedArgs = $this->processArgs(); |
127
|
|
|
$commandLine = Escape::argsForSite($this->siteAlias, $processedArgs); |
128
|
|
|
$commandLine = implode(' ', $commandLine); |
129
|
|
|
$this->setCommandLine($commandLine); |
130
|
|
|
} |
131
|
|
|
return $commandLine; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @inheritDoc |
136
|
|
|
*/ |
137
|
|
|
public function start(callable $callback = null) |
138
|
|
|
{ |
139
|
|
|
$cmd = $this->getCommandLine(); |
|
|
|
|
140
|
|
|
parent::start($callback); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @inheritDoc |
145
|
|
|
*/ |
146
|
|
|
public function wait(callable $callback = null) |
147
|
|
|
{ |
148
|
|
|
$return = parent::wait($callback); |
149
|
|
|
return $return; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* interpolate examines each of the arguments in the provided argument list |
154
|
|
|
* and replaces any token found therein with the value for that key as |
155
|
|
|
* pulled from the given site alias. |
156
|
|
|
* |
157
|
|
|
* Example: "git -C {{root}} status" |
158
|
|
|
* |
159
|
|
|
* The token "{{root}}" will be converted to a value via $siteAlias->get('root'). |
160
|
|
|
* The result will replace the token. |
161
|
|
|
* |
162
|
|
|
* It is possible to use dot notation in the keys to access nested elements |
163
|
|
|
* within the site alias record. |
164
|
|
|
* |
165
|
|
|
* @param AliasRecord $siteAlias |
|
|
|
|
166
|
|
|
* @param type $args |
167
|
|
|
* @return type |
168
|
|
|
*/ |
169
|
|
|
protected function interpolate($args) |
170
|
|
|
{ |
171
|
|
|
$interpolator = new Interpolator(); |
172
|
|
|
return array_map( |
173
|
|
|
function ($arg) use ($interpolator) { |
174
|
|
|
if ($arg instanceof ShellOperatorInterface) { |
175
|
|
|
return $arg; |
176
|
|
|
} |
177
|
|
|
return $interpolator->interpolate($this->siteAlias, $arg, false); |
178
|
|
|
}, |
179
|
|
|
$args |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.