| 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 |  |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         $this->formatters = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |             'default' => '\Consolidation\OutputFormatters\Formatters\DefaultFormatter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |             'yaml' => '\Consolidation\OutputFormatters\Formatters\YamlFormatter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |             'json' => '\Consolidation\OutputFormatters\Formatters\JsonFormatter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |             'print-r' => '\Consolidation\OutputFormatters\Formatters\PrintRFormatter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |             'php' => '\Consolidation\OutputFormatters\Formatters\SerializeFormatter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             'var_export' => '\Consolidation\OutputFormatters\Formatters\VarExportFormatter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             'list' => '\Consolidation\OutputFormatters\Formatters\ListFormatter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             'csv' => '\Consolidation\OutputFormatters\Formatters\CsvFormatter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             'table' => '\Consolidation\OutputFormatters\Formatters\TableFormatter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         // Make the empty string an alias for 'default'. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $this->formatters[''] = $this->formatters['default']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |     public function write(OutputInterface $output, $format, $structuredOutput, $configurationData = [], $options = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $formatter = $this->getFormatter((string)$format, $configurationData); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $structuredOutput = $this->validateAndRestructure($formatter, $structuredOutput, $configurationData, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $formatter->write($output, $structuredOutput, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     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 |  |  |         $overrideRestructure = $this->overrideRestructure($formatter, $structuredOutput, $configurationData, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         if ($overrideRestructure) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             return $overrideRestructure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         // Restructure the output data (e.g. select fields to display, etc.). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $structuredOutput = $this->restructureData($structuredOutput, $configurationData, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         // Make sure that the provided data is in the correct format for the selected formatter. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $structuredOutput = $this->validateData($formatter, $structuredOutput); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         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 |  |  |     public function getFormatter($format, $configurationData = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         if (!$this->hasFormatter($format)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             throw new UnknownFormatException($format); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $formatter = new $this->formatters[$format]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         if ($formatter instanceof ConfigureInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $formatter->configure($configurationData); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         return $formatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |     public function hasFormatter($format) | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         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 |  |  |     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 |  |  |         if ($formatter instanceof ValidationInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             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 |  |  |         if ($structuredOutput instanceof \ArrayObject) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             return $structuredOutput->getArrayCopy(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         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 |  |  |     public function restructureData($structuredOutput, $configurationData, $options) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         if ($structuredOutput instanceof RestructureInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             return $structuredOutput->restructure($configurationData, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         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 |  |  |     public function overrideRestructure(FormatterInterface $formatter, $structuredOutput, $configurationData, $options) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         if ($formatter instanceof OverrideRestructureInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             return $formatter->overrideRestructure($structuredOutput, $configurationData, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 151 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 152 |  |  |  |