Yaffs site version 1.1
[yaffs-website] / vendor / drush / drush / commands / core / outputformat / list.inc
1 <?php
2
3 /**
4  * Output formatter 'list'
5  *
6  * @param $data
7  *   The $data parameter is expected to be an array of key / value pairs.
8  *   Each key / value pair is passed to some other output formatter for
9  *   rendering; the key becomes the label, $metadata['label'], and the
10  *   value becomes the $data for the sub-formatter.
11  * @param $metadata
12  *   'matches' - Specifies the exact kind of list to be rendered in an
13  *     array of two elements.  $matches[0] is the full name of the
14  *     list format (e.g. 'string-list'), and $matches[1] is the type
15  *     of the sub-formatter (e.g. 'string').  If not specified, 'string'
16  *     is assumed.
17  *
18  * Code:
19  *
20  *   return array('a', 'b', 'c');
21  *
22  * Output with --format=list: (list of string):
23  *
24  *   a
25  *   b
26  *   c
27  */
28 class drush_outputformat_list extends drush_outputformat {
29   function is_list() {
30     return TRUE;
31   }
32   function validate() {
33     // Separate the 'list' and 'filter' metadata from everything
34     // else in $engine_config['engine-info']
35     $list_metadata = array();
36     foreach ($this->engine_config as $key => $value) {
37       if ((substr($key, 0, 4) == 'list') || (substr($key, -6) == 'filter') || (substr($key, 0, 5) == 'field') || ($key == 'options')) {
38         unset($this->engine_config[$key]);
39         $list_metadata[$key] = $value;
40       }
41     }
42     foreach ($this->engine_config['engine-info'] as $key => $value) {
43       if ((substr($key, 0, 4) == 'list') || (substr($key, -6) == 'filter')) {
44         unset($this->engine_config['engine-info'][$key]);
45         $list_metadata[$key] = $value;
46       }
47     }
48     $sub_formatter = isset($list_metadata['list-item-type']) ? $list_metadata['list-item-type'] : 'string';
49     $this->sub_engine = drush_load_engine('outputformat', $sub_formatter, $this->engine_config);
50     if (!is_object($this->sub_engine)) {
51       return drush_set_error('DRUSH_INVALID_SUBFORMATTER', dt("The list output formatter could not load its subformatter: !sub", array('!sub' => $sub_formatter)));
52     }
53     $engine_info = $this->engine_config['engine-info'];
54     $this->engine_config = array(
55       'engine-info' => array(
56         'machine-parsable' => $this->sub_engine->engine_config['engine-info']['machine-parsable'],
57       ),
58       'metameta' => $this->sub_engine->engine_config,
59     ) + $list_metadata;
60     return TRUE;
61   }
62
63   function format($input, $metadata) {
64     $output = '';
65     if (is_array($input)) {
66       // If this list is processing output from a command that produces table
67       // @todo - need different example below?
68       // output, but our subformatter only supports 'single' items (e.g. csv),
69       // then we will convert our data such that the output will be the keys
70       // of the table rows.
71       if (($this->data_type($metadata) == 'format-table') && ($this->supports_single_only($metadata)) && !isset($metadata['list-item'])) {
72         // If the user specified exactly one field with --fields, then
73         // use it to select the data column to use instead of the array key.
74         if (isset($metadata['field-labels']) && (count($metadata['field-labels']) == 1)) {
75           $first_label = key($metadata['field-labels']);
76           $input = drush_output_get_selected_field($input, $first_label);
77         }
78         else {
79           $input = array_keys($input);
80         }
81       }
82       $first = TRUE;
83       $field_selection_control = isset($metadata['list-field-selection-control']) ? $metadata['list-field-selection-control'] : 0;
84       $selected_output_fields = false;
85       if (empty($metadata['metameta'])) {
86         $metameta = $metadata;
87         unset($metameta['list-item']);
88         unset($metameta['list-item-default-value']);
89       }
90       else {
91         $metameta = $metadata['metameta'];
92       }
93       $list_separator_key = 'list-separator';
94       if ($this->sub_engine->is_list()) {
95         $list_separator_key = 'line-separator';
96         if (isset($metadata['list-separator'])) {
97           $metameta['list-separator'] = $metadata['list-separator'];
98         }
99       }
100       $separator = isset($metadata[$list_separator_key]) && !empty($metadata[$list_separator_key]) ? $metadata[$list_separator_key] : "\n";
101       // @todo - bug? we iterate over a hard coded, single item array?
102       foreach (array('field-labels') as $key) {
103         if (isset($metadata[$key])) {
104           $metameta[$key] = $metadata[$key];
105         }
106       }
107
108       // Include field labels, if specified
109       if (!isset($metadata['list-item']) && isset($metadata['labeled-list']) && is_array($input) && isset($metadata['field-labels'])) {
110         if (isset($metadata['include-field-labels']) && $metadata['include-field-labels']) {
111           array_unshift($input, $metadata['field-labels']);
112         }
113       }
114       foreach ($input as $label => $data) {
115         // If this output formatter is set to print a single item from each
116         // element, select that item here.
117         if (isset($metadata['list-item'])) {
118           $data = isset($data[$metadata['list-item']]) ? $data[$metadata['list-item']] : $metadata['list-item-default-value'];
119         }
120         // If this formatter supports the --fields option, then filter and
121         // order the fields the user wants here.  Note that we need to be
122         // careful about when we call drush_select_output_fields(); sometimes,
123         // there will be nested formatters of type 'list', and it would not
124         // do to select the output fields more than once.
125         // 'list-field-selection-control can be set to a positive number to
126         // cause output fields to be selected at a later point in the call chain.
127         elseif (is_array($data) && isset($metadata['field-labels'])) {
128           if (!$field_selection_control) {
129             $data = drush_select_output_fields($data, $metadata['field-labels'], $metadata['field-mappings']);
130             $selected_output_fields = true;
131           }
132         }
133         $metameta['label'] = $label;
134         if ($selected_output_fields) {
135           $metameta['list-field-selection-control'] = -1;
136         }
137         elseif ($field_selection_control) {
138           $metameta['list-field-selection-control'] = $field_selection_control - 1;
139         }
140         $formatted_item = $this->sub_engine->format($data, $metameta);
141         if ($formatted_item === FALSE) {
142           return FALSE;
143         }
144         if (!$first) {
145           $output .= $separator;
146         }
147         if (($separator != "\n") && !empty($separator) && (strpos($formatted_item, $separator) !== FALSE)) {
148           $formatted_item = drush_wrap_with_quotes($formatted_item);
149         }
150         $output .= $formatted_item;
151         $first = FALSE;
152       }
153     }
154     return $output;
155   }
156 }