Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Console\Command;
|
Chris@0
|
13
|
Chris@17
|
14 use Symfony\Component\Console\Application;
|
Chris@0
|
15 use Symfony\Component\Console\Exception\ExceptionInterface;
|
Chris@0
|
16 use Symfony\Component\Console\Exception\InvalidArgumentException;
|
Chris@0
|
17 use Symfony\Component\Console\Exception\LogicException;
|
Chris@17
|
18 use Symfony\Component\Console\Helper\HelperSet;
|
Chris@17
|
19 use Symfony\Component\Console\Input\InputArgument;
|
Chris@17
|
20 use Symfony\Component\Console\Input\InputDefinition;
|
Chris@17
|
21 use Symfony\Component\Console\Input\InputInterface;
|
Chris@17
|
22 use Symfony\Component\Console\Input\InputOption;
|
Chris@17
|
23 use Symfony\Component\Console\Output\OutputInterface;
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * Base class for all commands.
|
Chris@0
|
27 *
|
Chris@0
|
28 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
29 */
|
Chris@0
|
30 class Command
|
Chris@0
|
31 {
|
Chris@14
|
32 /**
|
Chris@14
|
33 * @var string|null The default command name
|
Chris@14
|
34 */
|
Chris@14
|
35 protected static $defaultName;
|
Chris@14
|
36
|
Chris@0
|
37 private $application;
|
Chris@0
|
38 private $name;
|
Chris@0
|
39 private $processTitle;
|
Chris@17
|
40 private $aliases = [];
|
Chris@0
|
41 private $definition;
|
Chris@0
|
42 private $hidden = false;
|
Chris@0
|
43 private $help;
|
Chris@0
|
44 private $description;
|
Chris@0
|
45 private $ignoreValidationErrors = false;
|
Chris@0
|
46 private $applicationDefinitionMerged = false;
|
Chris@0
|
47 private $applicationDefinitionMergedWithArgs = false;
|
Chris@0
|
48 private $code;
|
Chris@17
|
49 private $synopsis = [];
|
Chris@17
|
50 private $usages = [];
|
Chris@0
|
51 private $helperSet;
|
Chris@0
|
52
|
Chris@0
|
53 /**
|
Chris@14
|
54 * @return string|null The default command name or null when no default name is set
|
Chris@14
|
55 */
|
Chris@14
|
56 public static function getDefaultName()
|
Chris@14
|
57 {
|
Chris@17
|
58 $class = \get_called_class();
|
Chris@14
|
59 $r = new \ReflectionProperty($class, 'defaultName');
|
Chris@14
|
60
|
Chris@14
|
61 return $class === $r->class ? static::$defaultName : null;
|
Chris@14
|
62 }
|
Chris@14
|
63
|
Chris@14
|
64 /**
|
Chris@0
|
65 * @param string|null $name The name of the command; passing null means it must be set in configure()
|
Chris@0
|
66 *
|
Chris@0
|
67 * @throws LogicException When the command name is empty
|
Chris@0
|
68 */
|
Chris@0
|
69 public function __construct($name = null)
|
Chris@0
|
70 {
|
Chris@0
|
71 $this->definition = new InputDefinition();
|
Chris@0
|
72
|
Chris@14
|
73 if (null !== $name || null !== $name = static::getDefaultName()) {
|
Chris@0
|
74 $this->setName($name);
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@0
|
77 $this->configure();
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 /**
|
Chris@0
|
81 * Ignores validation errors.
|
Chris@0
|
82 *
|
Chris@0
|
83 * This is mainly useful for the help command.
|
Chris@0
|
84 */
|
Chris@0
|
85 public function ignoreValidationErrors()
|
Chris@0
|
86 {
|
Chris@0
|
87 $this->ignoreValidationErrors = true;
|
Chris@0
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 public function setApplication(Application $application = null)
|
Chris@0
|
91 {
|
Chris@0
|
92 $this->application = $application;
|
Chris@0
|
93 if ($application) {
|
Chris@0
|
94 $this->setHelperSet($application->getHelperSet());
|
Chris@0
|
95 } else {
|
Chris@0
|
96 $this->helperSet = null;
|
Chris@0
|
97 }
|
Chris@0
|
98 }
|
Chris@0
|
99
|
Chris@0
|
100 public function setHelperSet(HelperSet $helperSet)
|
Chris@0
|
101 {
|
Chris@0
|
102 $this->helperSet = $helperSet;
|
Chris@0
|
103 }
|
Chris@0
|
104
|
Chris@0
|
105 /**
|
Chris@0
|
106 * Gets the helper set.
|
Chris@0
|
107 *
|
Chris@0
|
108 * @return HelperSet A HelperSet instance
|
Chris@0
|
109 */
|
Chris@0
|
110 public function getHelperSet()
|
Chris@0
|
111 {
|
Chris@0
|
112 return $this->helperSet;
|
Chris@0
|
113 }
|
Chris@0
|
114
|
Chris@0
|
115 /**
|
Chris@0
|
116 * Gets the application instance for this command.
|
Chris@0
|
117 *
|
Chris@0
|
118 * @return Application An Application instance
|
Chris@0
|
119 */
|
Chris@0
|
120 public function getApplication()
|
Chris@0
|
121 {
|
Chris@0
|
122 return $this->application;
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 /**
|
Chris@0
|
126 * Checks whether the command is enabled or not in the current environment.
|
Chris@0
|
127 *
|
Chris@0
|
128 * Override this to check for x or y and return false if the command can not
|
Chris@0
|
129 * run properly under the current conditions.
|
Chris@0
|
130 *
|
Chris@0
|
131 * @return bool
|
Chris@0
|
132 */
|
Chris@0
|
133 public function isEnabled()
|
Chris@0
|
134 {
|
Chris@0
|
135 return true;
|
Chris@0
|
136 }
|
Chris@0
|
137
|
Chris@0
|
138 /**
|
Chris@0
|
139 * Configures the current command.
|
Chris@0
|
140 */
|
Chris@0
|
141 protected function configure()
|
Chris@0
|
142 {
|
Chris@0
|
143 }
|
Chris@0
|
144
|
Chris@0
|
145 /**
|
Chris@0
|
146 * Executes the current command.
|
Chris@0
|
147 *
|
Chris@0
|
148 * This method is not abstract because you can use this class
|
Chris@0
|
149 * as a concrete class. In this case, instead of defining the
|
Chris@0
|
150 * execute() method, you set the code to execute by passing
|
Chris@0
|
151 * a Closure to the setCode() method.
|
Chris@0
|
152 *
|
Chris@17
|
153 * @return int|null null or 0 if everything went fine, or an error code
|
Chris@0
|
154 *
|
Chris@0
|
155 * @throws LogicException When this abstract method is not implemented
|
Chris@0
|
156 *
|
Chris@0
|
157 * @see setCode()
|
Chris@0
|
158 */
|
Chris@0
|
159 protected function execute(InputInterface $input, OutputInterface $output)
|
Chris@0
|
160 {
|
Chris@0
|
161 throw new LogicException('You must override the execute() method in the concrete command class.');
|
Chris@0
|
162 }
|
Chris@0
|
163
|
Chris@0
|
164 /**
|
Chris@0
|
165 * Interacts with the user.
|
Chris@0
|
166 *
|
Chris@0
|
167 * This method is executed before the InputDefinition is validated.
|
Chris@0
|
168 * This means that this is the only place where the command can
|
Chris@0
|
169 * interactively ask for values of missing required arguments.
|
Chris@0
|
170 */
|
Chris@0
|
171 protected function interact(InputInterface $input, OutputInterface $output)
|
Chris@0
|
172 {
|
Chris@0
|
173 }
|
Chris@0
|
174
|
Chris@0
|
175 /**
|
Chris@17
|
176 * Initializes the command after the input has been bound and before the input
|
Chris@17
|
177 * is validated.
|
Chris@0
|
178 *
|
Chris@0
|
179 * This is mainly useful when a lot of commands extends one main command
|
Chris@0
|
180 * where some things need to be initialized based on the input arguments and options.
|
Chris@17
|
181 *
|
Chris@17
|
182 * @see InputInterface::bind()
|
Chris@17
|
183 * @see InputInterface::validate()
|
Chris@0
|
184 */
|
Chris@0
|
185 protected function initialize(InputInterface $input, OutputInterface $output)
|
Chris@0
|
186 {
|
Chris@0
|
187 }
|
Chris@0
|
188
|
Chris@0
|
189 /**
|
Chris@0
|
190 * Runs the command.
|
Chris@0
|
191 *
|
Chris@0
|
192 * The code to execute is either defined directly with the
|
Chris@0
|
193 * setCode() method or by overriding the execute() method
|
Chris@0
|
194 * in a sub-class.
|
Chris@0
|
195 *
|
Chris@0
|
196 * @return int The command exit code
|
Chris@0
|
197 *
|
Chris@0
|
198 * @throws \Exception When binding input fails. Bypass this by calling {@link ignoreValidationErrors()}.
|
Chris@0
|
199 *
|
Chris@0
|
200 * @see setCode()
|
Chris@0
|
201 * @see execute()
|
Chris@0
|
202 */
|
Chris@0
|
203 public function run(InputInterface $input, OutputInterface $output)
|
Chris@0
|
204 {
|
Chris@0
|
205 // force the creation of the synopsis before the merge with the app definition
|
Chris@0
|
206 $this->getSynopsis(true);
|
Chris@0
|
207 $this->getSynopsis(false);
|
Chris@0
|
208
|
Chris@0
|
209 // add the application arguments and options
|
Chris@0
|
210 $this->mergeApplicationDefinition();
|
Chris@0
|
211
|
Chris@0
|
212 // bind the input against the command specific arguments/options
|
Chris@0
|
213 try {
|
Chris@0
|
214 $input->bind($this->definition);
|
Chris@0
|
215 } catch (ExceptionInterface $e) {
|
Chris@0
|
216 if (!$this->ignoreValidationErrors) {
|
Chris@0
|
217 throw $e;
|
Chris@0
|
218 }
|
Chris@0
|
219 }
|
Chris@0
|
220
|
Chris@0
|
221 $this->initialize($input, $output);
|
Chris@0
|
222
|
Chris@0
|
223 if (null !== $this->processTitle) {
|
Chris@17
|
224 if (\function_exists('cli_set_process_title')) {
|
Chris@16
|
225 if (!@cli_set_process_title($this->processTitle)) {
|
Chris@0
|
226 if ('Darwin' === PHP_OS) {
|
Chris@17
|
227 $output->writeln('<comment>Running "cli_set_process_title" as an unprivileged user is not supported on MacOS.</comment>', OutputInterface::VERBOSITY_VERY_VERBOSE);
|
Chris@0
|
228 } else {
|
Chris@16
|
229 cli_set_process_title($this->processTitle);
|
Chris@0
|
230 }
|
Chris@0
|
231 }
|
Chris@17
|
232 } elseif (\function_exists('setproctitle')) {
|
Chris@0
|
233 setproctitle($this->processTitle);
|
Chris@0
|
234 } elseif (OutputInterface::VERBOSITY_VERY_VERBOSE === $output->getVerbosity()) {
|
Chris@0
|
235 $output->writeln('<comment>Install the proctitle PECL to be able to change the process title.</comment>');
|
Chris@0
|
236 }
|
Chris@0
|
237 }
|
Chris@0
|
238
|
Chris@0
|
239 if ($input->isInteractive()) {
|
Chris@0
|
240 $this->interact($input, $output);
|
Chris@0
|
241 }
|
Chris@0
|
242
|
Chris@0
|
243 // The command name argument is often omitted when a command is executed directly with its run() method.
|
Chris@0
|
244 // It would fail the validation if we didn't make sure the command argument is present,
|
Chris@0
|
245 // since it's required by the application.
|
Chris@0
|
246 if ($input->hasArgument('command') && null === $input->getArgument('command')) {
|
Chris@0
|
247 $input->setArgument('command', $this->getName());
|
Chris@0
|
248 }
|
Chris@0
|
249
|
Chris@0
|
250 $input->validate();
|
Chris@0
|
251
|
Chris@0
|
252 if ($this->code) {
|
Chris@17
|
253 $statusCode = \call_user_func($this->code, $input, $output);
|
Chris@0
|
254 } else {
|
Chris@0
|
255 $statusCode = $this->execute($input, $output);
|
Chris@0
|
256 }
|
Chris@0
|
257
|
Chris@0
|
258 return is_numeric($statusCode) ? (int) $statusCode : 0;
|
Chris@0
|
259 }
|
Chris@0
|
260
|
Chris@0
|
261 /**
|
Chris@0
|
262 * Sets the code to execute when running this command.
|
Chris@0
|
263 *
|
Chris@0
|
264 * If this method is used, it overrides the code defined
|
Chris@0
|
265 * in the execute() method.
|
Chris@0
|
266 *
|
Chris@0
|
267 * @param callable $code A callable(InputInterface $input, OutputInterface $output)
|
Chris@0
|
268 *
|
Chris@0
|
269 * @return $this
|
Chris@0
|
270 *
|
Chris@0
|
271 * @throws InvalidArgumentException
|
Chris@0
|
272 *
|
Chris@0
|
273 * @see execute()
|
Chris@0
|
274 */
|
Chris@0
|
275 public function setCode(callable $code)
|
Chris@0
|
276 {
|
Chris@0
|
277 if ($code instanceof \Closure) {
|
Chris@0
|
278 $r = new \ReflectionFunction($code);
|
Chris@0
|
279 if (null === $r->getClosureThis()) {
|
Chris@12
|
280 if (\PHP_VERSION_ID < 70000) {
|
Chris@0
|
281 // Bug in PHP5: https://bugs.php.net/bug.php?id=64761
|
Chris@0
|
282 // This means that we cannot bind static closures and therefore we must
|
Chris@0
|
283 // ignore any errors here. There is no way to test if the closure is
|
Chris@0
|
284 // bindable.
|
Chris@0
|
285 $code = @\Closure::bind($code, $this);
|
Chris@0
|
286 } else {
|
Chris@0
|
287 $code = \Closure::bind($code, $this);
|
Chris@0
|
288 }
|
Chris@0
|
289 }
|
Chris@0
|
290 }
|
Chris@0
|
291
|
Chris@0
|
292 $this->code = $code;
|
Chris@0
|
293
|
Chris@0
|
294 return $this;
|
Chris@0
|
295 }
|
Chris@0
|
296
|
Chris@0
|
297 /**
|
Chris@0
|
298 * Merges the application definition with the command definition.
|
Chris@0
|
299 *
|
Chris@0
|
300 * This method is not part of public API and should not be used directly.
|
Chris@0
|
301 *
|
Chris@0
|
302 * @param bool $mergeArgs Whether to merge or not the Application definition arguments to Command definition arguments
|
Chris@0
|
303 */
|
Chris@0
|
304 public function mergeApplicationDefinition($mergeArgs = true)
|
Chris@0
|
305 {
|
Chris@0
|
306 if (null === $this->application || (true === $this->applicationDefinitionMerged && ($this->applicationDefinitionMergedWithArgs || !$mergeArgs))) {
|
Chris@0
|
307 return;
|
Chris@0
|
308 }
|
Chris@0
|
309
|
Chris@0
|
310 $this->definition->addOptions($this->application->getDefinition()->getOptions());
|
Chris@0
|
311
|
Chris@17
|
312 $this->applicationDefinitionMerged = true;
|
Chris@17
|
313
|
Chris@0
|
314 if ($mergeArgs) {
|
Chris@0
|
315 $currentArguments = $this->definition->getArguments();
|
Chris@0
|
316 $this->definition->setArguments($this->application->getDefinition()->getArguments());
|
Chris@0
|
317 $this->definition->addArguments($currentArguments);
|
Chris@0
|
318
|
Chris@0
|
319 $this->applicationDefinitionMergedWithArgs = true;
|
Chris@0
|
320 }
|
Chris@0
|
321 }
|
Chris@0
|
322
|
Chris@0
|
323 /**
|
Chris@0
|
324 * Sets an array of argument and option instances.
|
Chris@0
|
325 *
|
Chris@0
|
326 * @param array|InputDefinition $definition An array of argument and option instances or a definition instance
|
Chris@0
|
327 *
|
Chris@0
|
328 * @return $this
|
Chris@0
|
329 */
|
Chris@0
|
330 public function setDefinition($definition)
|
Chris@0
|
331 {
|
Chris@0
|
332 if ($definition instanceof InputDefinition) {
|
Chris@0
|
333 $this->definition = $definition;
|
Chris@0
|
334 } else {
|
Chris@0
|
335 $this->definition->setDefinition($definition);
|
Chris@0
|
336 }
|
Chris@0
|
337
|
Chris@0
|
338 $this->applicationDefinitionMerged = false;
|
Chris@0
|
339
|
Chris@0
|
340 return $this;
|
Chris@0
|
341 }
|
Chris@0
|
342
|
Chris@0
|
343 /**
|
Chris@0
|
344 * Gets the InputDefinition attached to this Command.
|
Chris@0
|
345 *
|
Chris@0
|
346 * @return InputDefinition An InputDefinition instance
|
Chris@0
|
347 */
|
Chris@0
|
348 public function getDefinition()
|
Chris@0
|
349 {
|
Chris@0
|
350 return $this->definition;
|
Chris@0
|
351 }
|
Chris@0
|
352
|
Chris@0
|
353 /**
|
Chris@0
|
354 * Gets the InputDefinition to be used to create representations of this Command.
|
Chris@0
|
355 *
|
Chris@0
|
356 * Can be overridden to provide the original command representation when it would otherwise
|
Chris@0
|
357 * be changed by merging with the application InputDefinition.
|
Chris@0
|
358 *
|
Chris@0
|
359 * This method is not part of public API and should not be used directly.
|
Chris@0
|
360 *
|
Chris@0
|
361 * @return InputDefinition An InputDefinition instance
|
Chris@0
|
362 */
|
Chris@0
|
363 public function getNativeDefinition()
|
Chris@0
|
364 {
|
Chris@0
|
365 return $this->getDefinition();
|
Chris@0
|
366 }
|
Chris@0
|
367
|
Chris@0
|
368 /**
|
Chris@0
|
369 * Adds an argument.
|
Chris@0
|
370 *
|
Chris@17
|
371 * @param string $name The argument name
|
Chris@17
|
372 * @param int|null $mode The argument mode: InputArgument::REQUIRED or InputArgument::OPTIONAL
|
Chris@17
|
373 * @param string $description A description text
|
Chris@17
|
374 * @param string|string[]|null $default The default value (for InputArgument::OPTIONAL mode only)
|
Chris@17
|
375 *
|
Chris@17
|
376 * @throws InvalidArgumentException When argument mode is not valid
|
Chris@0
|
377 *
|
Chris@0
|
378 * @return $this
|
Chris@0
|
379 */
|
Chris@0
|
380 public function addArgument($name, $mode = null, $description = '', $default = null)
|
Chris@0
|
381 {
|
Chris@0
|
382 $this->definition->addArgument(new InputArgument($name, $mode, $description, $default));
|
Chris@0
|
383
|
Chris@0
|
384 return $this;
|
Chris@0
|
385 }
|
Chris@0
|
386
|
Chris@0
|
387 /**
|
Chris@0
|
388 * Adds an option.
|
Chris@0
|
389 *
|
Chris@17
|
390 * @param string $name The option name
|
Chris@17
|
391 * @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
|
Chris@17
|
392 * @param int|null $mode The option mode: One of the InputOption::VALUE_* constants
|
Chris@17
|
393 * @param string $description A description text
|
Chris@17
|
394 * @param string|string[]|int|bool|null $default The default value (must be null for InputOption::VALUE_NONE)
|
Chris@17
|
395 *
|
Chris@17
|
396 * @throws InvalidArgumentException If option mode is invalid or incompatible
|
Chris@0
|
397 *
|
Chris@0
|
398 * @return $this
|
Chris@0
|
399 */
|
Chris@0
|
400 public function addOption($name, $shortcut = null, $mode = null, $description = '', $default = null)
|
Chris@0
|
401 {
|
Chris@0
|
402 $this->definition->addOption(new InputOption($name, $shortcut, $mode, $description, $default));
|
Chris@0
|
403
|
Chris@0
|
404 return $this;
|
Chris@0
|
405 }
|
Chris@0
|
406
|
Chris@0
|
407 /**
|
Chris@0
|
408 * Sets the name of the command.
|
Chris@0
|
409 *
|
Chris@0
|
410 * This method can set both the namespace and the name if
|
Chris@0
|
411 * you separate them by a colon (:)
|
Chris@0
|
412 *
|
Chris@0
|
413 * $command->setName('foo:bar');
|
Chris@0
|
414 *
|
Chris@0
|
415 * @param string $name The command name
|
Chris@0
|
416 *
|
Chris@0
|
417 * @return $this
|
Chris@0
|
418 *
|
Chris@0
|
419 * @throws InvalidArgumentException When the name is invalid
|
Chris@0
|
420 */
|
Chris@0
|
421 public function setName($name)
|
Chris@0
|
422 {
|
Chris@0
|
423 $this->validateName($name);
|
Chris@0
|
424
|
Chris@0
|
425 $this->name = $name;
|
Chris@0
|
426
|
Chris@0
|
427 return $this;
|
Chris@0
|
428 }
|
Chris@0
|
429
|
Chris@0
|
430 /**
|
Chris@0
|
431 * Sets the process title of the command.
|
Chris@0
|
432 *
|
Chris@0
|
433 * This feature should be used only when creating a long process command,
|
Chris@0
|
434 * like a daemon.
|
Chris@0
|
435 *
|
Chris@0
|
436 * PHP 5.5+ or the proctitle PECL library is required
|
Chris@0
|
437 *
|
Chris@0
|
438 * @param string $title The process title
|
Chris@0
|
439 *
|
Chris@0
|
440 * @return $this
|
Chris@0
|
441 */
|
Chris@0
|
442 public function setProcessTitle($title)
|
Chris@0
|
443 {
|
Chris@0
|
444 $this->processTitle = $title;
|
Chris@0
|
445
|
Chris@0
|
446 return $this;
|
Chris@0
|
447 }
|
Chris@0
|
448
|
Chris@0
|
449 /**
|
Chris@0
|
450 * Returns the command name.
|
Chris@0
|
451 *
|
Chris@0
|
452 * @return string The command name
|
Chris@0
|
453 */
|
Chris@0
|
454 public function getName()
|
Chris@0
|
455 {
|
Chris@0
|
456 return $this->name;
|
Chris@0
|
457 }
|
Chris@0
|
458
|
Chris@0
|
459 /**
|
Chris@0
|
460 * @param bool $hidden Whether or not the command should be hidden from the list of commands
|
Chris@0
|
461 *
|
Chris@0
|
462 * @return Command The current instance
|
Chris@0
|
463 */
|
Chris@0
|
464 public function setHidden($hidden)
|
Chris@0
|
465 {
|
Chris@0
|
466 $this->hidden = (bool) $hidden;
|
Chris@0
|
467
|
Chris@0
|
468 return $this;
|
Chris@0
|
469 }
|
Chris@0
|
470
|
Chris@0
|
471 /**
|
Chris@14
|
472 * @return bool whether the command should be publicly shown or not
|
Chris@0
|
473 */
|
Chris@0
|
474 public function isHidden()
|
Chris@0
|
475 {
|
Chris@0
|
476 return $this->hidden;
|
Chris@0
|
477 }
|
Chris@0
|
478
|
Chris@0
|
479 /**
|
Chris@0
|
480 * Sets the description for the command.
|
Chris@0
|
481 *
|
Chris@0
|
482 * @param string $description The description for the command
|
Chris@0
|
483 *
|
Chris@0
|
484 * @return $this
|
Chris@0
|
485 */
|
Chris@0
|
486 public function setDescription($description)
|
Chris@0
|
487 {
|
Chris@0
|
488 $this->description = $description;
|
Chris@0
|
489
|
Chris@0
|
490 return $this;
|
Chris@0
|
491 }
|
Chris@0
|
492
|
Chris@0
|
493 /**
|
Chris@0
|
494 * Returns the description for the command.
|
Chris@0
|
495 *
|
Chris@0
|
496 * @return string The description for the command
|
Chris@0
|
497 */
|
Chris@0
|
498 public function getDescription()
|
Chris@0
|
499 {
|
Chris@0
|
500 return $this->description;
|
Chris@0
|
501 }
|
Chris@0
|
502
|
Chris@0
|
503 /**
|
Chris@0
|
504 * Sets the help for the command.
|
Chris@0
|
505 *
|
Chris@0
|
506 * @param string $help The help for the command
|
Chris@0
|
507 *
|
Chris@0
|
508 * @return $this
|
Chris@0
|
509 */
|
Chris@0
|
510 public function setHelp($help)
|
Chris@0
|
511 {
|
Chris@0
|
512 $this->help = $help;
|
Chris@0
|
513
|
Chris@0
|
514 return $this;
|
Chris@0
|
515 }
|
Chris@0
|
516
|
Chris@0
|
517 /**
|
Chris@0
|
518 * Returns the help for the command.
|
Chris@0
|
519 *
|
Chris@0
|
520 * @return string The help for the command
|
Chris@0
|
521 */
|
Chris@0
|
522 public function getHelp()
|
Chris@0
|
523 {
|
Chris@0
|
524 return $this->help;
|
Chris@0
|
525 }
|
Chris@0
|
526
|
Chris@0
|
527 /**
|
Chris@0
|
528 * Returns the processed help for the command replacing the %command.name% and
|
Chris@0
|
529 * %command.full_name% patterns with the real values dynamically.
|
Chris@0
|
530 *
|
Chris@0
|
531 * @return string The processed help for the command
|
Chris@0
|
532 */
|
Chris@0
|
533 public function getProcessedHelp()
|
Chris@0
|
534 {
|
Chris@0
|
535 $name = $this->name;
|
Chris@17
|
536 $isSingleCommand = $this->application && $this->application->isSingleCommand();
|
Chris@0
|
537
|
Chris@17
|
538 $placeholders = [
|
Chris@0
|
539 '%command.name%',
|
Chris@0
|
540 '%command.full_name%',
|
Chris@17
|
541 ];
|
Chris@17
|
542 $replacements = [
|
Chris@0
|
543 $name,
|
Chris@17
|
544 $isSingleCommand ? $_SERVER['PHP_SELF'] : $_SERVER['PHP_SELF'].' '.$name,
|
Chris@17
|
545 ];
|
Chris@0
|
546
|
Chris@0
|
547 return str_replace($placeholders, $replacements, $this->getHelp() ?: $this->getDescription());
|
Chris@0
|
548 }
|
Chris@0
|
549
|
Chris@0
|
550 /**
|
Chris@0
|
551 * Sets the aliases for the command.
|
Chris@0
|
552 *
|
Chris@0
|
553 * @param string[] $aliases An array of aliases for the command
|
Chris@0
|
554 *
|
Chris@0
|
555 * @return $this
|
Chris@0
|
556 *
|
Chris@0
|
557 * @throws InvalidArgumentException When an alias is invalid
|
Chris@0
|
558 */
|
Chris@0
|
559 public function setAliases($aliases)
|
Chris@0
|
560 {
|
Chris@17
|
561 if (!\is_array($aliases) && !$aliases instanceof \Traversable) {
|
Chris@0
|
562 throw new InvalidArgumentException('$aliases must be an array or an instance of \Traversable');
|
Chris@0
|
563 }
|
Chris@0
|
564
|
Chris@0
|
565 foreach ($aliases as $alias) {
|
Chris@0
|
566 $this->validateName($alias);
|
Chris@0
|
567 }
|
Chris@0
|
568
|
Chris@0
|
569 $this->aliases = $aliases;
|
Chris@0
|
570
|
Chris@0
|
571 return $this;
|
Chris@0
|
572 }
|
Chris@0
|
573
|
Chris@0
|
574 /**
|
Chris@0
|
575 * Returns the aliases for the command.
|
Chris@0
|
576 *
|
Chris@0
|
577 * @return array An array of aliases for the command
|
Chris@0
|
578 */
|
Chris@0
|
579 public function getAliases()
|
Chris@0
|
580 {
|
Chris@0
|
581 return $this->aliases;
|
Chris@0
|
582 }
|
Chris@0
|
583
|
Chris@0
|
584 /**
|
Chris@0
|
585 * Returns the synopsis for the command.
|
Chris@0
|
586 *
|
Chris@0
|
587 * @param bool $short Whether to show the short version of the synopsis (with options folded) or not
|
Chris@0
|
588 *
|
Chris@0
|
589 * @return string The synopsis
|
Chris@0
|
590 */
|
Chris@0
|
591 public function getSynopsis($short = false)
|
Chris@0
|
592 {
|
Chris@0
|
593 $key = $short ? 'short' : 'long';
|
Chris@0
|
594
|
Chris@0
|
595 if (!isset($this->synopsis[$key])) {
|
Chris@0
|
596 $this->synopsis[$key] = trim(sprintf('%s %s', $this->name, $this->definition->getSynopsis($short)));
|
Chris@0
|
597 }
|
Chris@0
|
598
|
Chris@0
|
599 return $this->synopsis[$key];
|
Chris@0
|
600 }
|
Chris@0
|
601
|
Chris@0
|
602 /**
|
Chris@0
|
603 * Add a command usage example.
|
Chris@0
|
604 *
|
Chris@0
|
605 * @param string $usage The usage, it'll be prefixed with the command name
|
Chris@0
|
606 *
|
Chris@0
|
607 * @return $this
|
Chris@0
|
608 */
|
Chris@0
|
609 public function addUsage($usage)
|
Chris@0
|
610 {
|
Chris@0
|
611 if (0 !== strpos($usage, $this->name)) {
|
Chris@0
|
612 $usage = sprintf('%s %s', $this->name, $usage);
|
Chris@0
|
613 }
|
Chris@0
|
614
|
Chris@0
|
615 $this->usages[] = $usage;
|
Chris@0
|
616
|
Chris@0
|
617 return $this;
|
Chris@0
|
618 }
|
Chris@0
|
619
|
Chris@0
|
620 /**
|
Chris@0
|
621 * Returns alternative usages of the command.
|
Chris@0
|
622 *
|
Chris@0
|
623 * @return array
|
Chris@0
|
624 */
|
Chris@0
|
625 public function getUsages()
|
Chris@0
|
626 {
|
Chris@0
|
627 return $this->usages;
|
Chris@0
|
628 }
|
Chris@0
|
629
|
Chris@0
|
630 /**
|
Chris@0
|
631 * Gets a helper instance by name.
|
Chris@0
|
632 *
|
Chris@0
|
633 * @param string $name The helper name
|
Chris@0
|
634 *
|
Chris@0
|
635 * @return mixed The helper value
|
Chris@0
|
636 *
|
Chris@0
|
637 * @throws LogicException if no HelperSet is defined
|
Chris@0
|
638 * @throws InvalidArgumentException if the helper is not defined
|
Chris@0
|
639 */
|
Chris@0
|
640 public function getHelper($name)
|
Chris@0
|
641 {
|
Chris@0
|
642 if (null === $this->helperSet) {
|
Chris@0
|
643 throw new LogicException(sprintf('Cannot retrieve helper "%s" because there is no HelperSet defined. Did you forget to add your command to the application or to set the application on the command using the setApplication() method? You can also set the HelperSet directly using the setHelperSet() method.', $name));
|
Chris@0
|
644 }
|
Chris@0
|
645
|
Chris@0
|
646 return $this->helperSet->get($name);
|
Chris@0
|
647 }
|
Chris@0
|
648
|
Chris@0
|
649 /**
|
Chris@0
|
650 * Validates a command name.
|
Chris@0
|
651 *
|
Chris@0
|
652 * It must be non-empty and parts can optionally be separated by ":".
|
Chris@0
|
653 *
|
Chris@0
|
654 * @param string $name
|
Chris@0
|
655 *
|
Chris@0
|
656 * @throws InvalidArgumentException When the name is invalid
|
Chris@0
|
657 */
|
Chris@0
|
658 private function validateName($name)
|
Chris@0
|
659 {
|
Chris@0
|
660 if (!preg_match('/^[^\:]++(\:[^\:]++)*$/', $name)) {
|
Chris@0
|
661 throw new InvalidArgumentException(sprintf('Command name "%s" is invalid.', $name));
|
Chris@0
|
662 }
|
Chris@0
|
663 }
|
Chris@0
|
664 }
|