Completed
Push — master ( 320654...e3c731 )
by Greg
10s
created

ConfigForCommand::fixInputForSymfony2()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
namespace Consolidation\Config\Inject;
3
4
use Consolidation\Config\ConfigInterface;
5
use Consolidation\Config\Util\ConfigFallback;
6
7
use Symfony\Component\Console\ConsoleEvents;
8
use Symfony\Component\Console\Event\ConsoleCommandEvent;
9
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10
use Symfony\Component\Console\Application;
11
12
class ConfigForCommand implements EventSubscriberInterface
13
{
14
    protected $config;
15
    protected $application;
16
17
    public function __construct(ConfigInterface $config)
18
    {
19
        $this->config = $config;
20
    }
21
22
    public function setApplication(Application $application)
23
    {
24
        $this->application = $application;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public static function getSubscribedEvents()
31
    {
32
        return [ConsoleEvents::COMMAND => 'injectConfiguration'];
33
    }
34
35
    /**
36
     * Before a Console command runs, inject configuration settings
37
     * for this command into the default value of the options of
38
     * this command.
39
     *
40
     * @param \Symfony\Component\Console\Event\ConsoleCommandEvent $event
41
     */
42
    public function injectConfiguration(ConsoleCommandEvent $event)
43
    {
44
        $command = $event->getCommand();
45
        $this->injectConfigurationForCommand($command);
46
47
        $targetOfHelpCommand = $this->getHelpCommandTarget($command, $event->getInput());
48
        if ($targetOfHelpCommand) {
49
            $this->injectConfigurationForCommand($targetOfHelpCommand);
50
        }
51
    }
52
53
    protected function injectConfigurationForCommand($command)
54
    {
55
        $commandName = $command->getName();
56
        $commandName = str_replace(':', '.', $commandName);
57
        $definition = $command->getDefinition();
58
        $options = $definition->getOptions();
59
        $configGroup = new ConfigFallback($this->config, $commandName, 'command.', '.options.');
60
        foreach ($options as $option => $inputOption) {
61
            $key = str_replace('.', '-', $option);
62
            $value = $configGroup->get($key);
63
            if ($value !== null) {
64
                $inputOption->setDefault($value);
65
            }
66
        }
67
    }
68
69
    protected function getHelpCommandTarget($command, $input)
70
    {
71
        if (($command->getName() != 'help') || (!isset($this->application))) {
72
            return false;
73
        }
74
75
        $this->fixInputForSymfony2($command, $input);
76
77
        // Symfony Console helpfully swaps 'command_name' and 'command'
78
        // depending on whether the user entered `help foo` or `--help foo`.
79
        // One of these is always `help`, and the other is the command we
80
        // are actually interested in.
81
        $nameOfCommandToDescribe = $input->getArgument('command_name');
82
        if ($nameOfCommandToDescribe == 'help') {
83
            $nameOfCommandToDescribe = $input->getArgument('command');
84
        }
85
        return $this->application->find($nameOfCommandToDescribe);
86
    }
87
88
    protected function fixInputForSymfony2($command, $input)
89
    {
90
        // Symfony 3.x prepares $input for us; Symfony 2.x, on the other
91
        // hand, passes it in prior to binding with the command definition,
92
        // so we have to go to a little extra work.  It may be inadvisable
93
        // to do these steps for commands other than 'help'.
94
        if (!$input->hasArgument('command_name')) {
95
            $command->ignoreValidationErrors();
96
            $command->mergeApplicationDefinition();
97
            $input->bind($command->getDefinition());
98
        }
99
    }
100
}
101