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