Completed
Push — master ( ef8ae5...2c2ae1 )
by Greg
02:18
created

AlterOptionsCommandEvent::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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