class drush_outputformat

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

Interface for output format engines.

Hierarchy

Expanded class hierarchy of drush_outputformat

Members

File

commands/core/outputformat.drush.inc, line 341
Core drush output formats.

View source
class drush_outputformat {
  function __construct($config) {
    $config += array(
      'column-widths' => array(),
      'field-mappings' => array(),
      'engine-info' => array(),
    );
    $config['engine-info'] += array(
      'machine-parsable' => FALSE,
      'metadata' => array(),
    );
    $config += $config['engine-info']['metadata'];
    $this->engine_config = $config;
  }
  function format_error($message) {
    return drush_set_error('DRUSH_FORMAT_ERROR', dt("The output data could not be processed by the selected format '!type'.  !message", array('!type' => $this->engine, '!message' => $message)));
  }
  function formatter_type() {
    return $this->engine;
  }
  function is_list() {
    return FALSE;
  }
  function formatter_is_simple_list() {
    if (!isset($this->sub_engine)) {
      return false;
    }
    return ($this->formatter_type() == 'list') && ($this->sub_engine->supports_single_only());
  }
  function data_type($metadata) {
    if (isset($metadata['metameta']['require-engine-capability']) && is_array($metadata['metameta']['require-engine-capability'])) {
      return $metadata['metameta']['require-engine-capability'][0];
    }
    if (isset($metadata['require-engine-capability']) && is_array($metadata['require-engine-capability'])) {
      return $metadata['require-engine-capability'][0];
    }
    return 'unspecified';
  }
  function supported_data_types($metadata = NULL) {
    if ($metadata == NULL) {
      $metadata = $this->engine_config;
    }
    if (isset($metadata['metameta']['engine-info']['engine-capabilities'])) {
      return $metadata['metameta']['engine-info']['engine-capabilities'];
    }
    if (isset($metadata['engine-info']['engine-capabilities'])) {
      return $metadata['engine-info']['engine-capabilities'];
    }
    return array();
  }
  function supports_single_only($metadata = NULL) {
    $supported = $this->supported_data_types($metadata);
    return (count($supported) == 1) && ($supported[0] == 'format-single');
  }
  function get_info($key) {
    if (array_key_exists($key, $this->engine_config)) {
      return $this->engine_config[$key];
    }
    elseif (isset($this->sub_engine)) {
      return $this->sub_engine->get_info($key);
    }
    return FALSE;
  }
  /**
   * Perform pre-processing and then format() the $input.
   */
  function process($input, $metadata = array()) {
    $metadata = array_merge_recursive($metadata, $this->engine_config);
    if (isset($metadata['private-fields']) && is_array($input)) {
      if (!drush_get_option('show-passwords', FALSE)) {
        if (!is_array($metadata['private-fields'])) {
          $metadata['private-fields'] = array($metadata['private-fields']);
        }
        foreach ($metadata['private-fields'] as $private) {
          drush_unset_recursive($input, $private);
        }
      }
    }
    if (isset($metadata[$this->engine . '-metadata'])) {
      $engine_specific_metadata = $metadata[$this->engine . '-metadata'];
      unset($metadata[$this->engine . '-metadata']);
      $metadata = array_merge($metadata, $engine_specific_metadata);
    }
    if ((drush_get_context('DRUSH_PIPE')) && (isset($metadata['pipe-metadata']))) {
      $pipe_specific_metadata = $metadata['pipe-metadata'];
      unset($metadata['pipe-metadata']);
      $metadata = array_merge($metadata, $pipe_specific_metadata);
    }
    $machine_parsable = $this->engine_config['engine-info']['machine-parsable'];
    $formatter_type = $machine_parsable ? 'parsable' : 'formatted';
    if ((!$machine_parsable) && is_bool($input)) {
      $input = $input ? 'TRUE' : 'FALSE';
    }

    // Run $input through any filters that are specified for this formatter.
    if (isset($metadata[$formatter_type . '-filter'])) {
      $filters = $metadata[$formatter_type . '-filter'];
      if (!is_array($filters)) {
        $filters = array($filters);
      }
      foreach ($filters as $filter) {
        if (function_exists($filter)) {
          $input = $filter($input, $metadata);
        }
      }
    }
    if (isset($metadata['field-labels'])) {
      foreach (drush_hide_output_fields() as $hidden_field) {
        unset($metadata['field-labels'][$hidden_field]);
      }
    }
    return $this->format($input, $metadata);
  }
  function format($input, $metadata) {
    return $input;
  }
}