Chris@0
|
1 <?php
|
Chris@0
|
2 namespace Consolidation\AnnotatedCommand\Options;
|
Chris@0
|
3
|
Chris@0
|
4 use Consolidation\AnnotatedCommand\AnnotatedCommand;
|
Chris@0
|
5 use Symfony\Component\Console\Application;
|
Chris@0
|
6 use Symfony\Component\Console\Command\Command;
|
Chris@0
|
7 use Symfony\Component\Console\ConsoleEvents;
|
Chris@0
|
8 use Symfony\Component\Console\Event\ConsoleCommandEvent;
|
Chris@0
|
9 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * AlterOptionsCommandEvent is a subscriber to the Command Event
|
Chris@0
|
13 * that looks up any additional options (e.g. from an OPTION_HOOK)
|
Chris@0
|
14 * that should be added to the command. Options need to be added
|
Chris@0
|
15 * in two circumstances:
|
Chris@0
|
16 *
|
Chris@0
|
17 * 1. When 'help' for the command is called, so that the additional
|
Chris@0
|
18 * command options may be listed in the command description.
|
Chris@0
|
19 *
|
Chris@0
|
20 * 2. When the command itself is called, so that option validation
|
Chris@0
|
21 * may be done.
|
Chris@0
|
22 *
|
Chris@0
|
23 * We defer the addition of options until these times so that we
|
Chris@0
|
24 * do not invoke the option hooks for every command on every run
|
Chris@0
|
25 * of the program, and so that we do not need to defer the addition
|
Chris@0
|
26 * of all of the application hooks until after all of the application
|
Chris@0
|
27 * commands have been added. (Hooks may appear in the same command files
|
Chris@0
|
28 * as command implementations; applications may support command file
|
Chris@0
|
29 * plug-ins, and hooks may add options to commands defined in other
|
Chris@0
|
30 * commandfiles.)
|
Chris@0
|
31 */
|
Chris@0
|
32 class AlterOptionsCommandEvent implements EventSubscriberInterface
|
Chris@0
|
33 {
|
Chris@0
|
34 /** var Application */
|
Chris@0
|
35 protected $application;
|
Chris@0
|
36
|
Chris@0
|
37 public function __construct(Application $application)
|
Chris@0
|
38 {
|
Chris@0
|
39 $this->application = $application;
|
Chris@0
|
40 }
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * @param ConsoleCommandEvent $event
|
Chris@0
|
44 */
|
Chris@0
|
45 public function alterCommandOptions(ConsoleCommandEvent $event)
|
Chris@0
|
46 {
|
Chris@0
|
47 /* @var Command $command */
|
Chris@0
|
48 $command = $event->getCommand();
|
Chris@0
|
49 $input = $event->getInput();
|
Chris@0
|
50 if ($command->getName() == 'help') {
|
Chris@0
|
51 // Symfony 3.x prepares $input for us; Symfony 2.x, on the other
|
Chris@0
|
52 // hand, passes it in prior to binding with the command definition,
|
Chris@0
|
53 // so we have to go to a little extra work. It may be inadvisable
|
Chris@0
|
54 // to do these steps for commands other than 'help'.
|
Chris@0
|
55 if (!$input->hasArgument('command_name')) {
|
Chris@0
|
56 $command->ignoreValidationErrors();
|
Chris@0
|
57 $command->mergeApplicationDefinition();
|
Chris@0
|
58 $input->bind($command->getDefinition());
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 // Symfony Console helpfully swaps 'command_name' and 'command'
|
Chris@0
|
62 // depending on whether the user entered `help foo` or `--help foo`.
|
Chris@0
|
63 // One of these is always `help`, and the other is the command we
|
Chris@0
|
64 // are actually interested in.
|
Chris@0
|
65 $nameOfCommandToDescribe = $event->getInput()->getArgument('command_name');
|
Chris@0
|
66 if ($nameOfCommandToDescribe == 'help') {
|
Chris@0
|
67 $nameOfCommandToDescribe = $event->getInput()->getArgument('command');
|
Chris@0
|
68 }
|
Chris@0
|
69 $commandToDescribe = $this->application->find($nameOfCommandToDescribe);
|
Chris@0
|
70 $this->findAndAddHookOptions($commandToDescribe);
|
Chris@0
|
71 } else {
|
Chris@0
|
72 $this->findAndAddHookOptions($command);
|
Chris@0
|
73 }
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 public function findAndAddHookOptions($command)
|
Chris@0
|
77 {
|
Chris@0
|
78 if (!$command instanceof AnnotatedCommand) {
|
Chris@0
|
79 return;
|
Chris@0
|
80 }
|
Chris@0
|
81 $command->optionsHook();
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84
|
Chris@0
|
85 /**
|
Chris@0
|
86 * @{@inheritdoc}
|
Chris@0
|
87 */
|
Chris@0
|
88 public static function getSubscribedEvents()
|
Chris@0
|
89 {
|
Chris@0
|
90 return [ConsoleEvents::COMMAND => 'alterCommandOptions'];
|
Chris@0
|
91 }
|
Chris@0
|
92 }
|