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