1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Consolidation\SiteProcess; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
6
|
|
|
use Symfony\Component\Console\Style\OutputStyle; |
7
|
|
|
use Symfony\Component\Process\Process; |
8
|
|
|
use Consolidation\SiteProcess\Util\RealtimeOutputHandler; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Symfony\Component\Console\Output\ConsoleOutputInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* A wrapper around Symfony Process. |
14
|
|
|
* |
15
|
|
|
* - Supports simulated mode. Typically enabled via a --simulate option. |
16
|
|
|
* - Supports verbose mode - logs all runs. |
17
|
|
|
* - Can convert output json data into php array (convenience method) |
18
|
|
|
* - Provides a "realtime output" helper |
19
|
|
|
*/ |
20
|
|
|
class ProcessBase extends Process |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var OutputStyle |
24
|
|
|
*/ |
25
|
|
|
protected $output; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var OutputInterface |
29
|
|
|
*/ |
30
|
|
|
protected $stderr; |
31
|
|
|
|
32
|
|
|
private $simulated = false; |
33
|
|
|
|
34
|
|
|
private $verbose = false; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var LoggerInterface |
38
|
|
|
*/ |
39
|
|
|
private $logger; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Symfony 4 style constructor for creating Process instances from strings. |
43
|
|
|
* @param string $command The commandline string to run |
44
|
|
|
* @param string|null $cwd The working directory or null to use the working dir of the current PHP process |
45
|
|
|
* @param array|null $env The environment variables or null to use the same environment as the current PHP process |
46
|
|
|
* @param mixed|null $input The input as stream resource, scalar or \Traversable, or null for no input |
47
|
|
|
* @param int|float|null $timeout The timeout in seconds or null to disable |
48
|
|
|
* @return Process |
49
|
|
|
*/ |
50
|
|
|
public static function fromShellCommandline($command, $cwd = null, array $env = null, $input = null, $timeout = 60) |
51
|
|
|
{ |
52
|
|
|
if (method_exists('\Symfony\Component\Process\Process', 'fromShellCommandline')) { |
53
|
|
|
return Process::fromShellCommandline($command, $cwd, $env, $input, $timeout); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
return new Process($command, $cwd, $env, $input, $timeout); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* realtimeStdout returns the output stream that realtime output |
60
|
|
|
* should be sent to (if applicable) |
61
|
|
|
* |
62
|
|
|
* @return OutputStyle $output |
63
|
|
|
*/ |
64
|
|
|
public function realtimeStdout() |
65
|
|
|
{ |
66
|
|
|
return $this->output; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function realtimeStderr() |
70
|
|
|
{ |
71
|
|
|
if ($this->stderr) { |
72
|
|
|
return $this->stderr; |
73
|
|
|
} |
74
|
|
|
if (method_exists($this->output, 'getErrorStyle')) { |
75
|
|
|
return $this->output->getErrorStyle(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this->realtimeStdout(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* setRealtimeOutput allows the caller to inject an OutputStyle object |
83
|
|
|
* that will be used to stream realtime output if applicable. |
84
|
|
|
* |
85
|
|
|
* @param OutputStyle $output |
86
|
|
|
*/ |
87
|
|
|
public function setRealtimeOutput(OutputInterface $output, $stderr = null) |
88
|
|
|
{ |
89
|
|
|
$this->output = $output; |
|
|
|
|
90
|
|
|
$this->stderr = $stderr instanceof ConsoleOutputInterface ? $stderr->getErrorOutput() : $stderr; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
public function isVerbose() |
97
|
|
|
{ |
98
|
|
|
return $this->verbose; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param bool $verbose |
103
|
|
|
*/ |
104
|
|
|
public function setVerbose($verbose) |
105
|
|
|
{ |
106
|
|
|
$this->verbose = $verbose; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
public function isSimulated() |
113
|
|
|
{ |
114
|
|
|
return $this->simulated; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param bool $simulated |
119
|
|
|
*/ |
120
|
|
|
public function setSimulated($simulated) |
121
|
|
|
{ |
122
|
|
|
$this->simulated = $simulated; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return LoggerInterface |
127
|
|
|
*/ |
128
|
|
|
public function getLogger() |
129
|
|
|
{ |
130
|
|
|
return $this->logger; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param LoggerInterface $logger |
135
|
|
|
*/ |
136
|
|
|
public function setLogger($logger) |
137
|
|
|
{ |
138
|
|
|
$this->logger = $logger; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @inheritDoc |
143
|
|
|
*/ |
144
|
|
|
public function start(callable $callback = null, $env = array()) |
145
|
|
|
{ |
146
|
|
|
$cmd = $this->getCommandLine(); |
147
|
|
|
if ($this->isSimulated()) { |
148
|
|
|
$this->getLogger()->notice('Simulating: ' . $cmd); |
149
|
|
|
// Run a command that always succeeds. |
150
|
|
|
$this->setCommandLine('exit 0'); |
151
|
|
|
} elseif ($this->isVerbose()) { |
152
|
|
|
$this->getLogger()->info('Executing: ' . $cmd); |
153
|
|
|
} |
154
|
|
|
parent::start($callback, $env); |
155
|
|
|
// Set command back to original value in case anyone asks. |
156
|
|
|
if ($this->isSimulated()) { |
157
|
|
|
$this->setCommandLine($cmd); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get Process output and decode its JSON. |
163
|
|
|
* |
164
|
|
|
* @return array |
165
|
|
|
* An associative array. |
166
|
|
|
*/ |
167
|
|
|
public function getOutputAsJson() |
168
|
|
|
{ |
169
|
|
|
$output = trim($this->getOutput()); |
170
|
|
|
if (empty($output)) { |
171
|
|
|
throw new \InvalidArgumentException('Output is empty.'); |
172
|
|
|
} |
173
|
|
|
$output = preg_replace('#^[^{]*#', '', $output); |
174
|
|
|
$output = preg_replace('#[^}]*$#', '', $output); |
175
|
|
|
if (!$json = json_decode($output, true)) { |
176
|
|
|
throw new \InvalidArgumentException('Unable to decode output into JSON.'); |
177
|
|
|
} |
178
|
|
|
return $json; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Return a realTime output object. |
183
|
|
|
* |
184
|
|
|
* @return callable |
185
|
|
|
*/ |
186
|
|
|
public function showRealtime() |
187
|
|
|
{ |
188
|
|
|
$realTimeOutput = new RealtimeOutputHandler($this->realtimeStdout(), $this->realtimeStderr()); |
189
|
|
|
$realTimeOutput->configure($this); |
|
|
|
|
190
|
|
|
return $realTimeOutput; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: