1
|
|
|
<?php |
2
|
|
|
namespace Consolidation\OutputFormatters; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
5
|
|
|
use Consolidation\OutputFormatters\Transformations\PropertyParser; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* FormetterOptions holds information that affects the way a formatter |
9
|
|
|
* renders its output. |
10
|
|
|
* |
11
|
|
|
* There are three places where a formatter might get options from: |
12
|
|
|
* |
13
|
|
|
* 1. Configuration associated with the command that produced the output. |
14
|
|
|
* This is passed in to FormatterManager::write() along with the data |
15
|
|
|
* to format. It might originally come from annotations on the command, |
16
|
|
|
* or it might come from another source. Examples include the field labels |
17
|
|
|
* for a table, or the default list of fields to display. |
18
|
|
|
* |
19
|
|
|
* 2. Options specified by the user, e.g. by commandline options. |
20
|
|
|
* |
21
|
|
|
* 3. Default values associated with the formatter itself. |
22
|
|
|
* |
23
|
|
|
* This class caches configuration from sources (1) and (2), and expects |
24
|
|
|
* to be provided the defaults, (3), whenever a value is requested. |
25
|
|
|
*/ |
26
|
|
|
class FormatterOptions |
27
|
|
|
{ |
28
|
|
|
/** var array */ |
29
|
|
|
protected $configurationData = []; |
30
|
|
|
/** var array */ |
31
|
|
|
protected $options = []; |
32
|
|
|
/** var InputInterface */ |
33
|
|
|
protected $input; |
34
|
|
|
|
35
|
|
|
public function __construct($configurationData = [], $options = []) |
36
|
|
|
{ |
37
|
|
|
$this->configurationData = $configurationData; |
38
|
|
|
$this->options = $options; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function override($configurationData) |
42
|
|
|
{ |
43
|
|
|
return new self($configurationData + $this->getConfigurationData(), $this->getOptions()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function get($key, $defaults = [], $default = false) |
47
|
|
|
{ |
48
|
|
|
$value = $this->fetch($key, $defaults, $default); |
49
|
|
|
return $this->parse($key, $value); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function fetch($key, $defaults = [], $default = false) |
53
|
|
|
{ |
54
|
|
|
$values = array_merge( |
55
|
|
|
$defaults, |
56
|
|
|
$this->getConfigurationData(), |
57
|
|
|
$this->getOptions(), |
58
|
|
|
$this->getInputOptions($defaults) |
59
|
|
|
); |
60
|
|
|
if (array_key_exists($key, $values)) { |
61
|
|
|
return $values[$key]; |
62
|
|
|
} |
63
|
|
|
return $default; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function parse($key, $value) |
67
|
|
|
{ |
68
|
|
|
$optionFormat = $this->getOptionFormat($key); |
69
|
|
|
if ($optionFormat) { |
70
|
|
|
return $this->$optionFormat($value); |
71
|
|
|
} |
72
|
|
|
return $value; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function parsePropertyList($value) |
76
|
|
|
{ |
77
|
|
|
return PropertyParser::parse($value); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function getOptionFormat($key) |
81
|
|
|
{ |
82
|
|
|
$propertyFormats = [ |
83
|
|
|
'row-labels' => 'PropertyList', |
84
|
|
|
'field-labels' => 'PropertyList', |
85
|
|
|
'default-fields' => 'PropertyList', |
86
|
|
|
]; |
87
|
|
|
if (array_key_exists($key, $propertyFormats)) { |
88
|
|
|
return "parse{$propertyFormats[$key]}"; |
89
|
|
|
} |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function setConfigurationData($configurationData) |
94
|
|
|
{ |
95
|
|
|
$this->configurationData = $configurationData; |
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function setConfigurationValue($key, $value) |
100
|
|
|
{ |
101
|
|
|
$this->configurationData[$key] = $value; |
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function setConfigurationDefault($key, $value) |
106
|
|
|
{ |
107
|
|
|
if (!array_key_exists($key, $this->configurationData)) { |
108
|
|
|
return $this->setConfigurationValue($key, $value); |
109
|
|
|
} |
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function getConfigurationData() |
114
|
|
|
{ |
115
|
|
|
return $this->configurationData; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function setOptions($options) |
119
|
|
|
{ |
120
|
|
|
$this->options = $options; |
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function setOption($key, $value) |
125
|
|
|
{ |
126
|
|
|
$this->options[$key] = $value; |
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function getOptions() |
131
|
|
|
{ |
132
|
|
|
return $this->options; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function setInput(InputInterface $input) |
136
|
|
|
{ |
137
|
|
|
$this->input = $input; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function getInputOptions($defaults) |
141
|
|
|
{ |
142
|
|
|
if (!isset($this->input)) { |
143
|
|
|
return []; |
144
|
|
|
} |
145
|
|
|
$options = []; |
146
|
|
|
foreach ($defaults as $key => $value) { |
147
|
|
|
if ($this->input->hasOption($key)) { |
148
|
|
|
$options[$key] = $this->input->getOption($key); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
return $options; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|