1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
namespace Modulate\Artisan\Interceptor; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Application; |
6
|
|
|
use Symfony\Component\Console\Command\Command; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
|
10
|
|
|
class InterceptedCommand |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The command that was called |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected string $command; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The artisan console instance |
22
|
|
|
* |
23
|
|
|
* @var Application |
24
|
|
|
*/ |
25
|
|
|
protected Application $app; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The input given to the command |
29
|
|
|
* |
30
|
|
|
* @var InputInterface |
31
|
|
|
*/ |
32
|
|
|
protected InputInterface $input; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The instance where the output of the command will be sent |
36
|
|
|
* |
37
|
|
|
* @var OutputInterface |
38
|
|
|
*/ |
39
|
|
|
protected OutputInterface $output; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The exit code of the command which will be populated when the CommandFinished event is fired |
43
|
|
|
* |
44
|
|
|
* @var integer|null |
45
|
|
|
*/ |
46
|
|
|
protected int|null $exitCode = 0; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $command |
50
|
|
|
* @param InputInterface $input |
51
|
|
|
* @param OutputInterface $output |
52
|
|
|
* @param integer|null $exitCode |
53
|
|
|
*/ |
54
|
6 |
|
public function __construct(string $command, InputInterface $input, OutputInterface $output, int $exitCode = null) |
55
|
|
|
{ |
56
|
6 |
|
$this->command = $command; |
57
|
6 |
|
$this->input = $input; |
58
|
6 |
|
$this->output = $output; |
59
|
6 |
|
$this->exitCode = $exitCode; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Return the command that was intercepted |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
1 |
|
public function getCommand(): string |
68
|
|
|
{ |
69
|
1 |
|
return $this->command; |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
public function getCommandInstance(): Command |
73
|
|
|
{ |
74
|
1 |
|
return $this->getArtisan()->get($this->command); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Inject the artisan application into the intercepted command |
79
|
|
|
* |
80
|
|
|
* @param Application $app |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
6 |
|
public function setArtisan(Application $app) |
84
|
|
|
{ |
85
|
6 |
|
$this->app = $app; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the artisan console application |
90
|
|
|
* |
91
|
|
|
* @return Application |
92
|
|
|
*/ |
93
|
2 |
|
public function getArtisan(): Application |
94
|
|
|
{ |
95
|
2 |
|
return $this->app; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get the input for the command |
100
|
|
|
* |
101
|
|
|
* @return InputInterface |
102
|
|
|
*/ |
103
|
3 |
|
public function getInput(): InputInterface |
104
|
|
|
{ |
105
|
3 |
|
return $this->input; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get an option from the command input |
110
|
|
|
* |
111
|
|
|
* @param string $name |
112
|
|
|
* @return mixed |
113
|
|
|
*/ |
114
|
1 |
|
public function getOption(string $name): mixed |
115
|
|
|
{ |
116
|
1 |
|
return $this->getInput()->getOption($name); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Check if an input option exists |
121
|
|
|
* |
122
|
|
|
* @param string $name |
123
|
|
|
* @return boolean |
124
|
|
|
*/ |
125
|
1 |
|
public function hasOption(string $name): bool |
126
|
|
|
{ |
127
|
1 |
|
return $this->getInput()->hasOption($name); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Set an input option |
132
|
|
|
* |
133
|
|
|
* @param string $name |
134
|
|
|
* @param mixed $value |
135
|
|
|
* @return InterceptedCommand |
136
|
|
|
*/ |
137
|
1 |
|
public function setOption(string $name, mixed $value): InterceptedCommand |
138
|
|
|
{ |
139
|
1 |
|
$this->getInput()->setOption($name, $value); |
140
|
|
|
|
141
|
1 |
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get an argument from the command input |
146
|
|
|
* |
147
|
|
|
* @param string $name |
148
|
|
|
* @return mixed |
149
|
|
|
*/ |
150
|
1 |
|
public function getArgument(string $name): mixed |
151
|
|
|
{ |
152
|
1 |
|
return $this->getInput()->getArgument($name); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Check if an input argument exists |
157
|
|
|
* |
158
|
|
|
* @param string $name |
159
|
|
|
* @return boolean |
160
|
|
|
*/ |
161
|
1 |
|
public function hasArgument(string $name): bool |
162
|
|
|
{ |
163
|
1 |
|
return $this->getInput()->hasArgument($name); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Set an input argument |
168
|
|
|
* |
169
|
|
|
* @param string $name |
170
|
|
|
* @param mixed $value |
171
|
|
|
* @return InterceptedCommand |
172
|
|
|
*/ |
173
|
1 |
|
public function setArgument(string $name, mixed $value): InterceptedCommand |
174
|
|
|
{ |
175
|
1 |
|
$this->getInput()->setArgument($name, $value); |
176
|
|
|
|
177
|
1 |
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Get the output for the command |
182
|
|
|
* |
183
|
|
|
* @return OutputInterface |
184
|
|
|
*/ |
185
|
4 |
|
public function getOutput(): OutputInterface |
186
|
|
|
{ |
187
|
4 |
|
return $this->output; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Get the exit code of the finished command |
192
|
|
|
* |
193
|
|
|
* @return integer |
194
|
|
|
*/ |
195
|
1 |
|
public function getExitCode(): int|null |
196
|
|
|
{ |
197
|
1 |
|
return $this->exitCode; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Determines if the command is still running by the lack of an exit code |
202
|
|
|
* |
203
|
|
|
* @return boolean |
204
|
|
|
*/ |
205
|
1 |
|
public function hasFinished(): bool |
206
|
|
|
{ |
207
|
1 |
|
return $this->exitCode !== null; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|