1
|
|
|
<?php |
2
|
|
|
namespace Consolidation\AnnotatedCommand\Options; |
3
|
|
|
|
4
|
|
|
use Consolidation\AnnotatedCommand\AnnotatedCommand; |
5
|
|
|
use Symfony\Component\Console\Application; |
6
|
|
|
use Symfony\Component\Console\Command\Command; |
7
|
|
|
use Symfony\Component\Console\ConsoleEvents; |
8
|
|
|
use Symfony\Component\Console\Event\ConsoleCommandEvent; |
9
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* AlterOptionsCommandEvent is a subscriber to the Command Event |
13
|
|
|
* that looks up any additional options (e.g. from an OPTION_HOOK) |
14
|
|
|
* that should be added to the command. Options need to be added |
15
|
|
|
* in two circumstances: |
16
|
|
|
* |
17
|
|
|
* 1. When 'help' for the command is called, so that the additional |
18
|
|
|
* command options may be listed in the command description. |
19
|
|
|
* |
20
|
|
|
* 2. When the command itself is called, so that option validation |
21
|
|
|
* may be done. |
22
|
|
|
* |
23
|
|
|
* We defer the addition of options until these times so that we |
24
|
|
|
* do not invoke the option hooks for every command on every run |
25
|
|
|
* of the program, and so that we do not need to defer the addition |
26
|
|
|
* of all of the application hooks until after all of the application |
27
|
|
|
* commands have been added. (Hooks may appear in the same command files |
28
|
|
|
* as command implementations; applications may support command file |
29
|
|
|
* plug-ins, and hooks may add options to commands defined in other |
30
|
|
|
* commandfiles.) |
31
|
|
|
*/ |
32
|
|
|
class AlterOptionsCommandEvent implements EventSubscriberInterface |
33
|
|
|
{ |
34
|
|
|
/** var Application */ |
35
|
|
|
protected $application; |
36
|
|
|
|
37
|
|
|
public function __construct(Application $application) |
38
|
|
|
{ |
39
|
|
|
$this->application = $application; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param ConsoleCommandEvent $event |
44
|
|
|
*/ |
45
|
|
|
public function alterCommandOptions(ConsoleCommandEvent $event) |
46
|
|
|
{ |
47
|
|
|
/* @var Command $command */ |
48
|
|
|
$command = $event->getCommand(); |
49
|
|
|
if ($command->getName() == 'help') { |
50
|
|
|
$nameOfCommandToDescribe = $event->getInput()->getArgument('command_name'); |
51
|
|
|
$commandToDescribe = $this->application->find($nameOfCommandToDescribe); |
52
|
|
|
$this->findAndAddHookOptions($commandToDescribe); |
53
|
|
|
} else { |
54
|
|
|
$this->findAndAddHookOptions($command); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function findAndAddHookOptions($command) |
59
|
|
|
{ |
60
|
|
|
if (!$command instanceof AnnotatedCommand) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
$command->optionsHook(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @{@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public static function getSubscribedEvents() |
71
|
|
|
{ |
72
|
|
|
return [ConsoleEvents::COMMAND => 'alterCommandOptions']; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|