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