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