|
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
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
12
|
|
|
|
|
13
|
|
|
class ConfigForCommand implements EventSubscriberInterface |
|
14
|
|
|
{ |
|
15
|
|
|
protected $config; |
|
16
|
|
|
protected $application; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(ConfigInterface $config) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->config = $config; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function setApplication(Application $application) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->application = $application; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function getSubscribedEvents() |
|
32
|
|
|
{ |
|
33
|
|
|
return [ConsoleEvents::COMMAND => 'injectConfiguration']; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Before a Console command runs, inject configuration settings |
|
38
|
|
|
* for this command into the default value of the options of |
|
39
|
|
|
* this command. |
|
40
|
|
|
* |
|
41
|
|
|
* @param \Symfony\Component\Console\Event\ConsoleCommandEvent $event |
|
42
|
|
|
*/ |
|
43
|
|
|
public function injectConfiguration(ConsoleCommandEvent $event) |
|
44
|
|
|
{ |
|
45
|
|
|
$command = $event->getCommand(); |
|
46
|
|
|
$this->injectConfigurationForGlobalOptions($event->getInput()); |
|
47
|
|
|
$this->injectConfigurationForCommand($command, $event->getInput()); |
|
48
|
|
|
|
|
49
|
|
|
$targetOfHelpCommand = $this->getHelpCommandTarget($command, $event->getInput()); |
|
50
|
|
|
if ($targetOfHelpCommand) { |
|
51
|
|
|
$this->injectConfigurationForCommand($targetOfHelpCommand, $event->getInput()); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
protected function injectConfigurationForGlobalOptions($input) |
|
56
|
|
|
{ |
|
57
|
|
|
if (!$this->application) { |
|
58
|
|
|
return; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$configGroup = new ConfigFallback($this->config, 'options'); |
|
62
|
|
|
|
|
63
|
|
|
$definition = $this->application->getDefinition(); |
|
64
|
|
|
$options = $definition->getOptions(); |
|
65
|
|
|
|
|
66
|
|
|
return $this->injectConfigGroupIntoOptions($configGroup, $options, $input); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
protected function injectConfigurationForCommand($command, $input) |
|
70
|
|
|
{ |
|
71
|
|
|
$commandName = $command->getName(); |
|
72
|
|
|
$commandName = str_replace(':', '.', $commandName); |
|
73
|
|
|
$configGroup = new ConfigFallback($this->config, $commandName, 'command.', '.options.'); |
|
74
|
|
|
|
|
75
|
|
|
$definition = $command->getDefinition(); |
|
76
|
|
|
$options = $definition->getOptions(); |
|
77
|
|
|
|
|
78
|
|
|
return $this->injectConfigGroupIntoOptions($configGroup, $options, $input); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
protected function injectConfigGroupIntoOptions($configGroup, $options, $input) |
|
82
|
|
|
{ |
|
83
|
|
|
foreach ($options as $option => $inputOption) { |
|
84
|
|
|
$key = str_replace('.', '-', $option); |
|
85
|
|
|
$value = $configGroup->get($key); |
|
86
|
|
|
if ($value !== null) { |
|
87
|
|
|
// There is no way to test for `InputOption::VALUE_NONE` |
|
88
|
|
|
// We'll check to see if $value == true instead. Users |
|
89
|
|
|
// who put non-boolean values in their boolean configuration |
|
90
|
|
|
// items will cause 'setDefault' to throw an exception. |
|
91
|
|
|
if (is_bool($value) && ($value == true)) { |
|
|
|
|
|
|
92
|
|
|
$input->setOption($key, $value); |
|
93
|
|
|
} else { |
|
94
|
|
|
$inputOption->setDefault($value); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
protected function getHelpCommandTarget($command, $input) |
|
101
|
|
|
{ |
|
102
|
|
|
if (($command->getName() != 'help') || (!isset($this->application))) { |
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$this->fixInputForSymfony2($command, $input); |
|
107
|
|
|
|
|
108
|
|
|
// Symfony Console helpfully swaps 'command_name' and 'command' |
|
109
|
|
|
// depending on whether the user entered `help foo` or `--help foo`. |
|
110
|
|
|
// One of these is always `help`, and the other is the command we |
|
111
|
|
|
// are actually interested in. |
|
112
|
|
|
$nameOfCommandToDescribe = $input->getArgument('command_name'); |
|
113
|
|
|
if ($nameOfCommandToDescribe == 'help') { |
|
114
|
|
|
$nameOfCommandToDescribe = $input->getArgument('command'); |
|
115
|
|
|
} |
|
116
|
|
|
return $this->application->find($nameOfCommandToDescribe); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
protected function fixInputForSymfony2($command, $input) |
|
120
|
|
|
{ |
|
121
|
|
|
// Symfony 3.x prepares $input for us; Symfony 2.x, on the other |
|
122
|
|
|
// hand, passes it in prior to binding with the command definition, |
|
123
|
|
|
// so we have to go to a little extra work. It may be inadvisable |
|
124
|
|
|
// to do these steps for commands other than 'help'. |
|
125
|
|
|
if (!$input->hasArgument('command_name')) { |
|
126
|
|
|
$command->ignoreValidationErrors(); |
|
127
|
|
|
$command->mergeApplicationDefinition(); |
|
128
|
|
|
$input->bind($command->getDefinition()); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.