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