-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Command.php
277 lines (246 loc) · 7.89 KB
/
Command.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
namespace yii1tech\async\cmd;
use CComponent;
/**
* Command represents particular shell command to be executed.
*
* Do not instantiate this class on your own, use {@see \yii1tech\async\cmd\CommandDispatcher::create()} instead.
*
* @property string|null $commandClass class name of the Yii console command to be executed.
* @property string|null $commandAction name of the Yii console command action to be executed.
* @property string|null $binPath path to the console binary, which should be executed.
* @property array<int|string, mixed> $params list of shell arguments, which should be passed to the command.
* @property string|null $outputLog file or stream, to which the command output should be redirected.
* @property \yii1tech\async\cmd\CommandDispatcher $dispatcher the related dispatcher.
* @property bool $autoDispatch whether the command should be automatically dispatched, once it is out of program scope (e.g. on destruct).
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 1.0
*/
class Command extends CComponent
{
/**
* @var bool whether this command has been already dispatched.
*/
public $isDispatched = false;
/**
* @var string|null class name of the Yii console command to be executed.
*/
private $_commandClass;
/**
* @var string|null name of the Yii console command action to be executed.
*/
private $_commandAction;
/**
* @var string|null path to the console binary, which should be executed.
*/
private $_binPath;
/**
* @var array<int|string, mixed> list of shell arguments, which should be passed to the command.
*/
private $_params = [];
/**
* @var string|null file or stream, to which the command output should be redirected.
*/
private $_outputLog;
/**
* @var \yii1tech\async\cmd\CommandDispatcher the related dispatcher.
*/
private $_dispatcher;
/**
* @var bool whether the command should be automatically dispatched, once it is out of program scope (e.g. on destruct).
*/
private $_autoDispatch = true;
/**
* @return string|null class name of the Yii console command to be executed.
*/
public function getCommandClass(): ?string
{
return $this->_commandClass;
}
/**
* @param string $commandClass class name of the Yii console command to be executed.
* @return static self reference.
*/
public function setCommandClass(string $commandClass): self
{
$this->_commandClass = $commandClass;
return $this;
}
/**
* @return string|null name of the Yii console command action to be executed.
*/
public function getCommandAction(): ?string
{
return $this->_commandAction;
}
/**
* @param string $commandAction name of the Yii console command action to be executed.
* @return static self reference.
*/
public function setCommandAction(string $commandAction): self
{
$this->_commandAction = $commandAction;
return $this;
}
/**
* @return string|null path to the console binary, which should be executed.
*/
public function getBinPath(): ?string
{
return $this->_binPath;
}
/**
* @param string $binPath path to the console binary, which should be executed.
* @return static self reference.
*/
public function setBinPath(string $binPath): self
{
$this->_binPath = $binPath;
return $this;
}
/**
* @return array<int|string, mixed> list of arguments, which should be applied to the console command.
*/
public function getParams(): array
{
return $this->_params;
}
/**
* Sets parameters for the console command execution.
*
* Param values will be automatically sanitized using {@see escapeshellarg()}.
*
* For the external command parameter name should be specified in full, including possible prefixing '-' symbols.
* For example:
*
* ```
* [
* '-X' => 'POST',
* '-d' => 'param1=value1¶m2=value2',
* 'http://example.com/api/notify',
* ]
* ```
*
* For the Yii console there is no need to add '-' symbols - it will be performed automatically.
* For example:
*
* ```
* [
* 'interactive' => '0',
* 'foo' => 'bar',
* 'unnamed-arg-value',
* ]
* ```
*
* @param array<int|string, mixed> $params list of parameters, which should be applied to the console command.
* @return static self reference.
*/
public function setParams(array $params): self
{
$this->_params = $params;
return $this;
}
/**
* @return string|null file or stream, to which the command output should be redirected.
*/
public function getOutputLog(): ?string
{
return $this->_outputLog;
}
/**
* @param string $outputLog file or stream, to which the command output should be redirected.
* @return static self reference.
*/
public function setOutputLog(string $outputLog): self
{
$this->_outputLog = $outputLog;
return $this;
}
/**
* @return \yii1tech\async\cmd\CommandDispatcher related dispatcher.
*/
public function getDispatcher(): CommandDispatcher
{
return $this->_dispatcher;
}
/**
* @param \yii1tech\async\cmd\CommandDispatcher $dispatcher dispatcher, which should be used to execute this command.
* @return static self reference.
*/
public function setDispatcher(CommandDispatcher $dispatcher): self
{
$this->_dispatcher = $dispatcher;
return $this;
}
/**
* @return bool whether the command should be automatically dispatched, once it is out of program scope (e.g. on destruct).
*/
public function getAutoDispatch(): bool
{
return $this->_autoDispatch;
}
/**
* @param bool $autoDispatch whether the command should be automatically dispatched, once it is out of program scope (e.g. on destruct).
* @return static self reference.
*/
public function setAutoDispatch(bool $autoDispatch): self
{
$this->_autoDispatch = $autoDispatch;
return $this;
}
/**
* Configures command to run Yii console application command.
*
* @see setParams()
*
* @param string $class console command class name.
* @param string|null $action console command action
* @param array $params list of parameters, which should be applied to the console command.
* @return static self reference.
*/
public function yiic(string $class, ?string $action = null, array $params = []): self
{
$this->setCommandClass($class);
if ($action !== null) {
$this->setCommandAction($action);
}
$this->setParams($params);
return $this;
}
/**
* Configures command to run external utility in the system.
*
* @see setParams()
*
* @param string $binPath path to the console binary, which should be executed.
* @param array $params list of parameters, which should be applied to the console command.
* @return static self reference.
*/
public function external(string $binPath, array $params = []): self
{
return $this->setBinPath($binPath)
->setParams($params);
}
/**
* Dispatches this command for an asynchronous execution, without waiting for its result.
*
* @return static self reference.
*/
public function dispatch(): self
{
$this->getDispatcher()->dispatch($this);
$this->isDispatched = true;
return $this;
}
/**
* Destructor.
* Automatically dispatches pending command, if {@see $autoDispatch} is enabled.
*/
public function __destruct()
{
if ($this->getAutoDispatch() && !$this->isDispatched) {
$this->dispatch();
}
}
}