list.inc

  1. 8.0.x commands/core/outputformat/list.inc
  2. 6.x commands/core/outputformat/list.inc
  3. 7.x commands/core/outputformat/list.inc
  4. master commands/core/outputformat/list.inc

Classes

Namesort descending Description
drush_outputformat_list Output formatter 'list'

File

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