|
1
|
|
|
<?php |
|
2
|
|
|
namespace Consolidation\OutputFormatters; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
5
|
|
|
use Consolidation\OutputFormatters\FormatterInterface; |
|
6
|
|
|
use Consolidation\OutputFormatters\Exception\UnknownFormatException; |
|
7
|
|
|
use Consolidation\OutputFormatters\Formatters\RenderDataInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Manage a collection of formatters; return one on request. |
|
11
|
|
|
*/ |
|
12
|
|
|
class FormatterManager |
|
13
|
|
|
{ |
|
14
|
|
|
protected $formatters = []; |
|
15
|
|
|
|
|
16
|
28 |
|
public function __construct() |
|
17
|
|
|
{ |
|
18
|
28 |
|
$this->formatters = [ |
|
19
|
28 |
|
'string' => '\Consolidation\OutputFormatters\Formatters\StringFormatter', |
|
20
|
28 |
|
'yaml' => '\Consolidation\OutputFormatters\Formatters\YamlFormatter', |
|
21
|
28 |
|
'json' => '\Consolidation\OutputFormatters\Formatters\JsonFormatter', |
|
22
|
28 |
|
'print-r' => '\Consolidation\OutputFormatters\Formatters\PrintRFormatter', |
|
23
|
28 |
|
'php' => '\Consolidation\OutputFormatters\Formatters\SerializeFormatter', |
|
24
|
28 |
|
'var_export' => '\Consolidation\OutputFormatters\Formatters\VarExportFormatter', |
|
25
|
28 |
|
'list' => '\Consolidation\OutputFormatters\Formatters\ListFormatter', |
|
26
|
28 |
|
'csv' => '\Consolidation\OutputFormatters\Formatters\CsvFormatter', |
|
27
|
28 |
|
'table' => '\Consolidation\OutputFormatters\Formatters\TableFormatter', |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
// Make the empty format an alias for the 'string' formatter. |
|
31
|
28 |
|
$this->formatters[''] = $this->formatters['string']; |
|
32
|
28 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Format and write output |
|
36
|
|
|
* |
|
37
|
|
|
* @param OutputInterface $output Output stream to write to |
|
38
|
|
|
* @param string $format Data format to output in |
|
39
|
|
|
* @param mixed $structuredOutput Data to output |
|
40
|
|
|
* @param array $configurationData Configuration information for formatter |
|
41
|
|
|
* @param array $options User options |
|
42
|
|
|
*/ |
|
43
|
28 |
|
public function write(OutputInterface $output, $format, $structuredOutput, $configurationData = [], $options = []) |
|
44
|
|
|
{ |
|
45
|
28 |
|
$formatter = $this->getFormatter((string)$format, $configurationData); |
|
46
|
27 |
|
$structuredOutput = $this->validateAndRestructure($formatter, $structuredOutput, $configurationData, $options); |
|
47
|
24 |
|
$formatter->write($output, $structuredOutput, $options); |
|
48
|
24 |
|
} |
|
49
|
|
|
|
|
50
|
27 |
|
protected function validateAndRestructure(FormatterInterface $formatter, $structuredOutput, $configurationData, $options) |
|
51
|
|
|
{ |
|
52
|
|
|
// Give the formatter a chance to do something with the |
|
53
|
|
|
// raw data before it is restructured. |
|
54
|
27 |
|
$overrideRestructure = $this->overrideRestructure($formatter, $structuredOutput, $configurationData, $options); |
|
55
|
27 |
|
if ($overrideRestructure) { |
|
56
|
4 |
|
return $overrideRestructure; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// Restructure the output data (e.g. select fields to display, etc.). |
|
60
|
27 |
|
$restructuredOutput = $this->restructureData($structuredOutput, $configurationData, $options); |
|
61
|
|
|
|
|
62
|
|
|
// Make sure that the provided data is in the correct format for the selected formatter. |
|
63
|
27 |
|
$restructuredOutput = $this->validateData($formatter, $restructuredOutput); |
|
64
|
|
|
|
|
65
|
|
|
// Give the original data a chance to re-render the structured |
|
66
|
|
|
// output after it has been restructured and validated. |
|
67
|
24 |
|
$restructuredOutput = $this->renderData($formatter, $structuredOutput, $restructuredOutput, $configurationData, $options); |
|
68
|
|
|
|
|
69
|
24 |
|
return $restructuredOutput; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Fetch the requested formatter. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $format Identifier for requested formatter |
|
76
|
|
|
* @param array $configurationData Configuration data for formatter |
|
77
|
|
|
* @return FormatterInterface |
|
78
|
|
|
*/ |
|
79
|
28 |
|
public function getFormatter($format, $configurationData = []) |
|
80
|
|
|
{ |
|
81
|
28 |
|
if (!$this->hasFormatter($format)) { |
|
82
|
1 |
|
throw new UnknownFormatException($format); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
27 |
|
$formatter = new $this->formatters[$format]; |
|
86
|
27 |
|
if ($formatter instanceof ConfigureInterface) { |
|
87
|
9 |
|
$formatter->configure($configurationData); |
|
88
|
9 |
|
} |
|
89
|
27 |
|
return $formatter; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
28 |
|
public function hasFormatter($format) |
|
93
|
|
|
{ |
|
94
|
28 |
|
return array_key_exists($format, $this->formatters); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Render the data as necessary (e.g. to select or reorder fields). |
|
99
|
|
|
* |
|
100
|
|
|
* @param FormatterInterface $formatter |
|
101
|
|
|
* @param mixed $originalData |
|
102
|
|
|
* @param mixed $restructuredData |
|
103
|
|
|
* @param array $configurationData |
|
104
|
|
|
* @param array $options |
|
105
|
|
|
* @return mixed |
|
106
|
|
|
*/ |
|
107
|
24 |
|
public function renderData(FormatterInterface $formatter, $originalData, $restructuredData, $configurationData, $options) |
|
108
|
|
|
{ |
|
109
|
24 |
|
if ($formatter instanceof RenderDataInterface) { |
|
110
|
14 |
|
return $formatter->renderData($originalData, $restructuredData, $configurationData, $options); |
|
111
|
|
|
} |
|
112
|
14 |
|
return $restructuredData; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Determine if the provided data is compatible with the formatter being used. |
|
117
|
|
|
* |
|
118
|
|
|
* @param FormatterInterface $formatter Formatter being used |
|
119
|
|
|
* @param mixed $structuredOutput Data to validate |
|
120
|
|
|
* @return mixed |
|
121
|
|
|
*/ |
|
122
|
27 |
|
public function validateData(FormatterInterface $formatter, $structuredOutput) |
|
123
|
|
|
{ |
|
124
|
|
|
// If the formatter implements ValidationInterface, then let it |
|
125
|
|
|
// test the data and throw or return an error |
|
126
|
27 |
|
if ($formatter instanceof ValidationInterface) { |
|
127
|
16 |
|
return $formatter->validate($structuredOutput); |
|
128
|
|
|
} |
|
129
|
|
|
// If the formatter does not implement ValidationInterface, then |
|
130
|
|
|
// it will never be passed an ArrayObject; we will always give |
|
131
|
|
|
// it a simple array. |
|
132
|
15 |
|
if ($structuredOutput instanceof \ArrayObject) { |
|
133
|
4 |
|
return $structuredOutput->getArrayCopy(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
11 |
|
return $structuredOutput; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Restructure the data as necessary (e.g. to select or reorder fields). |
|
141
|
|
|
* |
|
142
|
|
|
* @param mixed $structuredOutput |
|
143
|
|
|
* @param array $configurationData |
|
144
|
|
|
* @param array $options |
|
145
|
|
|
* @return mixed |
|
146
|
|
|
*/ |
|
147
|
27 |
|
public function restructureData($structuredOutput, $configurationData, $options) |
|
148
|
|
|
{ |
|
149
|
27 |
|
if ($structuredOutput instanceof RestructureInterface) { |
|
150
|
7 |
|
return $structuredOutput->restructure($configurationData, $options); |
|
151
|
|
|
} |
|
152
|
20 |
|
return $structuredOutput; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Allow the formatter access to the raw structured data prior |
|
157
|
|
|
* to restructuring. For example, the 'list' formatter may wish |
|
158
|
|
|
* to display the row keys when provided table output. If this |
|
159
|
|
|
* function returns a result that does not evaluate to 'false', |
|
160
|
|
|
* then that result will be used as-is, and restructuring and |
|
161
|
|
|
* validation will not occur. |
|
162
|
|
|
* |
|
163
|
|
|
* @param mixed $structuredOutput |
|
164
|
|
|
* @param array $configurationData |
|
165
|
|
|
* @param array $options |
|
166
|
|
|
* @return mixed |
|
167
|
|
|
*/ |
|
168
|
27 |
|
public function overrideRestructure(FormatterInterface $formatter, $structuredOutput, $configurationData, $options) |
|
169
|
|
|
{ |
|
170
|
27 |
|
if ($formatter instanceof OverrideRestructureInterface) { |
|
171
|
5 |
|
return $formatter->overrideRestructure($structuredOutput, $configurationData, $options); |
|
172
|
|
|
} |
|
173
|
26 |
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|