1
|
|
|
<?php |
2
|
|
|
namespace Consolidation\OutputFormatters; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
5
|
|
|
use Consolidation\OutputFormatters\Exception\UnknownFormatException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Manage a collection of formatters; return one on request. |
9
|
|
|
*/ |
10
|
|
|
class FormatterManager |
11
|
|
|
{ |
12
|
|
|
protected $formatters = []; |
13
|
|
|
|
14
|
|
|
public function __construct() |
15
|
|
|
{ |
16
|
|
|
$this->formatters = [ |
17
|
|
|
'default' => '\Consolidation\OutputFormatters\Formatters\DefaultFormatter', |
18
|
|
|
'yaml' => '\Consolidation\OutputFormatters\Formatters\YamlFormatter', |
19
|
|
|
'json' => '\Consolidation\OutputFormatters\Formatters\JsonFormatter', |
20
|
|
|
'print-r' => '\Consolidation\OutputFormatters\Formatters\PrintRFormatter', |
21
|
|
|
'var_export' => '\Consolidation\OutputFormatters\Formatters\VarExportFormatter', |
22
|
|
|
'list' => '\Consolidation\OutputFormatters\Formatters\ListFormatter', |
23
|
|
|
'csv' => '\Consolidation\OutputFormatters\Formatters\CsvFormatter', |
24
|
|
|
'table' => '\Consolidation\OutputFormatters\Formatters\TableFormatter', |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
// Make the empty string an alias for 'default'. |
28
|
|
|
$this->formatters[''] = $this->formatters['default']; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Format and write output |
33
|
|
|
* |
34
|
|
|
* @param OutputInterface $output Output stream to write to |
35
|
|
|
* @param string $format Data format to output in |
36
|
|
|
* @param mixed $structuredOutput Data to output |
37
|
|
|
* @param array $annotationData Configuration information for formatter |
38
|
|
|
* @param array $options User options |
39
|
|
|
*/ |
40
|
|
|
public function write(OutputInterface $output, $format, $structuredOutput, $annotationData = [], $options = []) |
41
|
|
|
{ |
42
|
|
|
$formatter = $this->getFormatter($format, $annotationData); |
43
|
|
|
|
44
|
|
|
// Restructure the output data (e.g. select fields to display, etc.). |
45
|
|
|
$structuredOutput = $this->restructureData($structuredOutput, $annotationData, $options); |
46
|
|
|
|
47
|
|
|
// Make sure that the provided data is in the correct format for the selected formatter. |
48
|
|
|
$structuredOutput = $this->validateData($formatter, $structuredOutput); |
49
|
|
|
|
50
|
|
|
$formatter->write($output, $structuredOutput, $options); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Fetch the requested formatter. |
55
|
|
|
* |
56
|
|
|
* @param string $format Identifier for requested formatter |
57
|
|
|
* @param array $configurationData Configuration data for formatter |
58
|
|
|
* @return FormatterInterface |
59
|
|
|
*/ |
60
|
|
|
public function getFormatter($format, $configurationData = []) |
61
|
|
|
{ |
62
|
|
|
if (!$this->hasFormatter($format)) { |
63
|
|
|
throw new UnknownFormatException($format); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$formatter = new $this->formatters[$format]; |
67
|
|
|
if ($formatter instanceof ConfigureInterface) { |
68
|
|
|
$formatter->configure($configurationData); |
69
|
|
|
} |
70
|
|
|
return $formatter; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function hasFormatter($format) |
74
|
|
|
{ |
75
|
|
|
return array_key_exists($format, $this->formatters); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Determine if the provided data is compatible with the formatter being used. |
80
|
|
|
* |
81
|
|
|
* @param FormatterInterface $formatter Formatter being used |
82
|
|
|
* @param mixed $structuredOutput Data to validate |
83
|
|
|
* @return mixed |
84
|
|
|
*/ |
85
|
|
|
public function validateData(FormatterInterface $formatter, $structuredOutput) |
86
|
|
|
{ |
87
|
|
|
// If the formatter implements ValidationInterface, then let it |
88
|
|
|
// test the data and throw or return an error |
89
|
|
|
if ($formatter instanceof ValidationInterface) { |
90
|
|
|
return $formatter->validate($structuredOutput); |
91
|
|
|
} |
92
|
|
|
// If the formatter does not implement ValidationInterface, then |
93
|
|
|
// it will never be passed an ArrayObject; we will always give |
94
|
|
|
// it a simple array. |
95
|
|
|
if ($structuredOutput instanceof \ArrayObject) { |
96
|
|
|
return $structuredOutput->getArrayCopy(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $structuredOutput; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Restructure the data as necessary (e.g. to select or reorder fields). |
104
|
|
|
* |
105
|
|
|
* @param mixed $structuredOutput |
106
|
|
|
* @param array $configurationData |
107
|
|
|
* @param array $options |
108
|
|
|
* @return mixed |
109
|
|
|
*/ |
110
|
|
|
public function restructureData($structuredOutput, $configurationData, $options) |
111
|
|
|
{ |
112
|
|
|
if ($structuredOutput instanceof RestructureInterface) { |
113
|
|
|
return $structuredOutput->restructure($configurationData, $options); |
114
|
|
|
} |
115
|
|
|
return $structuredOutput; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|