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