|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the webmozart/console package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Webmozart\Console\Adapter; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Console\Application; |
|
15
|
|
|
use Symfony\Component\Console\Command\Command; |
|
16
|
|
|
use Symfony\Component\Console\Helper\HelperSet; |
|
17
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
|
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
20
|
|
|
use Webmozart\Assert\Assert; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Adapts a `Command` instance of this package to Symfony's {@link Command} API. |
|
24
|
|
|
* |
|
25
|
|
|
* @since 1.0 |
|
26
|
|
|
* |
|
27
|
|
|
* @author Bernhard Schussek <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
abstract class AbstractCommandAdapter extends Command |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var \Webmozart\Console\Api\Command\Command |
|
33
|
|
|
*/ |
|
34
|
|
|
private $adaptedCommand; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Creates the adapter. |
|
38
|
|
|
* |
|
39
|
|
|
* @param \Webmozart\Console\Api\Command\Command $adaptedCommand The adapted command. |
|
40
|
|
|
* @param Application $application The application. |
|
41
|
|
|
*/ |
|
42
|
15 |
|
public function __construct(\Webmozart\Console\Api\Command\Command $adaptedCommand, Application $application) |
|
43
|
|
|
{ |
|
44
|
15 |
|
parent::setName($adaptedCommand->getName()); |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
15 |
|
parent::__construct(); |
|
47
|
|
|
|
|
48
|
15 |
|
$this->adaptedCommand = $adaptedCommand; |
|
49
|
|
|
|
|
50
|
15 |
|
$config = $adaptedCommand->getConfig(); |
|
51
|
|
|
|
|
52
|
15 |
|
parent::setDefinition(new ArgsFormatInputDefinition($this->adaptedCommand->getArgsFormat())); |
|
|
|
|
|
|
53
|
15 |
|
parent::setApplication($application); |
|
|
|
|
|
|
54
|
15 |
|
parent::setDescription($config->getDescription()); |
|
|
|
|
|
|
55
|
15 |
|
parent::setHelp($config->getHelp()); |
|
|
|
|
|
|
56
|
15 |
|
parent::setAliases($adaptedCommand->getAliases()); |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
15 |
|
if ($helperSet = $config->getHelperSet()) { |
|
59
|
15 |
|
parent::setHelperSet($helperSet); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
15 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Returns the adapted command. |
|
65
|
|
|
* |
|
66
|
|
|
* @return Command The adapted command. |
|
|
|
|
|
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function getAdaptedCommand() |
|
69
|
|
|
{ |
|
70
|
1 |
|
return $this->adaptedCommand; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Does nothing. |
|
75
|
|
|
* |
|
76
|
|
|
* @param Application $application The application. |
|
|
|
|
|
|
77
|
|
|
* |
|
78
|
|
|
* @return static The current instance. |
|
79
|
|
|
*/ |
|
80
|
13 |
|
public function setApplication(Application $application = null) |
|
81
|
|
|
{ |
|
82
|
13 |
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Does nothing. |
|
87
|
|
|
* |
|
88
|
|
|
* @param HelperSet $helperSet The helper set. |
|
89
|
|
|
* |
|
90
|
|
|
* @return static The current instance. |
|
91
|
|
|
*/ |
|
92
|
15 |
|
public function setHelperSet(HelperSet $helperSet) |
|
93
|
|
|
{ |
|
94
|
15 |
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Does nothing. |
|
99
|
|
|
* |
|
100
|
|
|
* @param array|InputDefinition $definition The definition |
|
101
|
|
|
* |
|
102
|
|
|
* @return static The current instance. |
|
103
|
|
|
*/ |
|
104
|
|
|
public function setDefinition($definition) |
|
105
|
|
|
{ |
|
106
|
|
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Does nothing. |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $name The name. |
|
113
|
|
|
* |
|
114
|
|
|
* @return static The current instance. |
|
115
|
|
|
*/ |
|
116
|
|
|
public function setName($name) |
|
117
|
|
|
{ |
|
118
|
|
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Does nothing. |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $title The process title. |
|
125
|
|
|
* |
|
126
|
|
|
* @return static The current instance. |
|
127
|
|
|
*/ |
|
128
|
|
|
public function setProcessTitle($title) |
|
129
|
|
|
{ |
|
130
|
|
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Does nothing. |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $description The description. |
|
137
|
|
|
* |
|
138
|
|
|
* @return static The current instance. |
|
139
|
|
|
*/ |
|
140
|
|
|
public function setDescription($description) |
|
141
|
|
|
{ |
|
142
|
|
|
return $this; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Does nothing. |
|
147
|
|
|
* |
|
148
|
|
|
* @param string $help The help. |
|
149
|
|
|
* |
|
150
|
|
|
* @return static The current instance. |
|
151
|
|
|
*/ |
|
152
|
|
|
public function setHelp($help) |
|
153
|
|
|
{ |
|
154
|
|
|
return $this; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Does nothing. |
|
159
|
|
|
* |
|
160
|
|
|
* @param string[] $aliases The aliases. |
|
161
|
|
|
* |
|
162
|
|
|
* @return static The current instance. |
|
163
|
|
|
*/ |
|
164
|
|
|
public function setAliases($aliases) |
|
165
|
|
|
{ |
|
166
|
|
|
return $this; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Does nothing. |
|
171
|
|
|
* |
|
172
|
|
|
* @param bool $mergeArgs |
|
173
|
|
|
* |
|
174
|
|
|
* @return static The current instance. |
|
175
|
|
|
*/ |
|
176
|
12 |
|
public function mergeApplicationDefinition($mergeArgs = true) |
|
177
|
|
|
{ |
|
178
|
12 |
|
return $this; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Does nothing. |
|
183
|
|
|
* |
|
184
|
|
|
* @param string $name |
|
185
|
|
|
* @param null $mode |
|
186
|
|
|
* @param string $description |
|
187
|
|
|
* @param null $default |
|
188
|
|
|
* |
|
189
|
|
|
* @return static The current instance. |
|
190
|
|
|
*/ |
|
191
|
|
|
public function addArgument($name, $mode = null, $description = '', $default = null) |
|
192
|
|
|
{ |
|
193
|
|
|
return $this; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Does nothing. |
|
198
|
|
|
* |
|
199
|
|
|
* @param string $name |
|
200
|
|
|
* @param null $shortcut |
|
201
|
|
|
* @param null $mode |
|
202
|
|
|
* @param string $description |
|
203
|
|
|
* @param null $default |
|
204
|
|
|
* |
|
205
|
|
|
* @return static The current instance. |
|
206
|
|
|
*/ |
|
207
|
|
|
public function addOption($name, $shortcut = null, $mode = null, $description = '', $default = null) |
|
208
|
|
|
{ |
|
209
|
|
|
return $this; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* {@inheritdoc} |
|
214
|
|
|
*/ |
|
215
|
15 |
|
public function isEnabled() |
|
216
|
|
|
{ |
|
217
|
15 |
|
return $this->adaptedCommand->getConfig()->isEnabled(); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Executes the command. |
|
222
|
|
|
* |
|
223
|
|
|
* @param InputInterface $input The console input. |
|
224
|
|
|
* @param OutputInterface $output The console output. |
|
225
|
|
|
* |
|
226
|
|
|
* @return int The exit status. |
|
227
|
|
|
*/ |
|
228
|
|
|
public function run(InputInterface $input, OutputInterface $output) |
|
229
|
|
|
{ |
|
230
|
|
|
/* @var ArgsInput $input */ |
|
231
|
|
|
/* @var IOOutput $output */ |
|
232
|
|
|
Assert::isInstanceOf($input, 'Webmozart\Console\Adapter\ArgsInput'); |
|
233
|
|
|
Assert::isInstanceOf($output, 'Webmozart\Console\Adapter\IOOutput'); |
|
234
|
|
|
|
|
235
|
|
|
return $this->adaptedCommand->handle($input->getArgs(), $output->getIO()); |
|
|
|
|
|
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
1 |
|
if (method_exists('Symfony\Component\Console\Command\Command', 'asText')) { |
|
240
|
|
|
// Symfony 2.0 compatible definition |
|
241
|
|
|
class CommandAdapter extends AbstractCommandAdapter |
|
|
|
|
|
|
242
|
|
|
{ |
|
243
|
|
|
/** |
|
244
|
|
|
* Does nothing. |
|
245
|
|
|
* |
|
246
|
|
|
* @param callable $code The code. |
|
247
|
|
|
* |
|
248
|
|
|
* @return static The current instance. |
|
249
|
|
|
*/ |
|
250
|
|
|
public function setCode($code) |
|
251
|
|
|
{ |
|
252
|
|
|
return $this; |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
} else { |
|
256
|
|
|
// Symfony 3.0 compatible definition |
|
257
|
|
|
class CommandAdapter extends AbstractCommandAdapter |
|
|
|
|
|
|
258
|
|
|
{ |
|
259
|
|
|
/** |
|
260
|
|
|
* Does nothing. |
|
261
|
|
|
* |
|
262
|
|
|
* @param callable $code The code. |
|
263
|
|
|
* |
|
264
|
|
|
* @return static The current instance. |
|
265
|
|
|
*/ |
|
266
|
|
|
public function setCode(callable $code) |
|
267
|
|
|
{ |
|
268
|
|
|
return $this; |
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
} |
|
272
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.