forked from consolidation/robo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GlobalOptionsEventListener.php
146 lines (128 loc) · 4.24 KB
/
GlobalOptionsEventListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
namespace Robo;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Robo\Contract\ConfigAwareInterface;
use Robo\Common\ConfigAwareTrait;
use Robo\Config\GlobalOptionDefaultValuesInterface;
class GlobalOptionsEventListener implements EventSubscriberInterface, ConfigAwareInterface
{
use ConfigAwareTrait;
/** @var Application */
protected $application;
/** @var string */
protected $prefix;
/**
* GlobalOptionsEventListener listener
*/
public function __construct()
{
$this->prefix = 'options';
}
/**
* Add a reference to the Symfony Console application object.
*/
public function setApplication($application)
{
$this->application = $application;
return $this;
}
/**
* Stipulate the prefix to use for option injection.
* @param string $prefix
*/
public function setGlobalOptionsPrefix($prefix)
{
$this->prefix = $prefix;
return $this;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [ConsoleEvents::COMMAND => 'handleCommandEvent'];
}
/**
* Run all of our individual operations when a command event is received.
*/
public function handleCommandEvent(ConsoleCommandEvent $event)
{
$this->setGlobalOptions($event);
$this->setConfigurationValues($event);
}
/**
* Before a Console command runs, examine the global
* commandline options from the event Input, and set
* configuration values as appropriate.
*
* @param \Symfony\Component\Console\Event\ConsoleCommandEvent $event
*/
public function setGlobalOptions(ConsoleCommandEvent $event)
{
$config = $this->getConfig();
$input = $event->getInput();
$globalOptions = $config->get($this->prefix, []);
if ($config instanceof \Consolidation\Config\GlobalOptionDefaultValuesInterface) {
$globalOptions += $config->getGlobalOptionDefaultValues();
}
$globalOptions += $this->applicationOptionDefaultValues();
// Set any config value that has a defined global option (e.g. --simulate)
foreach ($globalOptions as $option => $default) {
$value = $input->hasOption($option) ? $input->getOption($option) : null;
// Unfortunately, the `?:` operator does not differentate between `0` and `null`
if (!isset($value)) {
$value = $default;
}
$config->set($this->prefix . '.' . $option, $value);
}
}
/**
* Examine the commandline --define / -D options, and apply the provided
* values to the active configuration.
*
* @param \Symfony\Component\Console\Event\ConsoleCommandEvent $event
*/
public function setConfigurationValues(ConsoleCommandEvent $event)
{
$config = $this->getConfig();
$input = $event->getInput();
// Also set any `-D config.key=value` options from the commandline.
if ($input->hasOption('define')) {
$configDefinitions = $input->getOption('define');
foreach ($configDefinitions as $value) {
list($key, $value) = $this->splitConfigKeyValue($value);
$config->set($key, $value);
}
}
}
/**
* Split up the key=value config setting into its component parts. If
* the input string contains no '=' character, then the value will be 'true'.
*
* @param string $value
* @return array
*/
protected function splitConfigKeyValue($value)
{
$parts = explode('=', $value, 2);
$parts[] = true;
return $parts;
}
/**
* Get default option values from the Symfony Console application, if
* it is available.
*/
protected function applicationOptionDefaultValues()
{
if (!$this->application) {
return [];
}
$result = [];
foreach ($this->application->getDefinition()->getOptions() as $key => $option) {
$result[$key] = $option->acceptValue() ? $option->getDefault() : null;
}
return $result;
}
}