You are here

function patterns_drush_filter_components in Patterns 7

Same name and namespace in other branches
  1. 7.2 patterns.drush.inc \patterns_drush_filter_components()

Validate, and filters user input for export components

The input is of the type

component.tag.export_id

and returns a valid moduletags index containing only the exporting functions

Parameters

array $out:

1 call to patterns_drush_filter_components()
drush_patterns_export in ./patterns.drush.inc
Export data from the patterns components to file, zip archive, or database

File

./patterns.drush.inc, line 551
Drush Patterns module commands

Code

function patterns_drush_filter_components($components) {
  $out = array();

  // Find the components.
  $all_components = patterns_io_list_components_names(TRUE);

  //drush_print(print_r($all_components, true));
  foreach ($components as $c) {
    $func = NULL;
    $func_name = NULL;
    $t = NULL;
    $tag = NULL;
    $tag_name = NULL;
    $export_func = NULL;
    list($component, $tag, $export_func) = explode('.', $c);
    if (!in_array($component, $all_components)) {
      drush_set_error(dt('Component not found: ' . $component));
      continue;
    }
    $info = call_user_func($component . '_patterns');
    if (empty($info)) {
      drush_set_error(dt('An error occurred while retrieving information from ' . $component));
      continue;
    }

    // If no tag was specified, get all export functions
    if (empty($tag)) {
      foreach ($info as $tag => $t) {
        if (!isset($t[PATTERNS_EXPORT]) || empty($t[PATTERNS_EXPORT])) {
          continue;
        }
        foreach ($t[PATTERNS_EXPORT] as $func_name => $func) {
          $out[$component][$tag][PATTERNS_EXPORT][$func_name] = $func;
        }

        // add files
        if (isset($t[PATTERNS_FILES])) {
          $out[$component][$tag][PATTERNS_FILES] = $t[PATTERNS_FILES];
        }
      }
      continue;
    }
    if (!isset($info[$tag])) {
      drush_set_error(dt('Component "' . $component . '" has no tag "' . $tag . '" defined.'));
      continue;
    }
    if (!isset($info[$tag][PATTERNS_EXPORT]) || empty($info[$tag][PATTERNS_EXPORT])) {
      drush_set_error(dt('Component "' . $component . '", tag "' . $tag . '" has no export function defined.'));
      continue;
    }

    // If no export function was specified, get all export functions for the tag
    if (empty($export_func)) {
      foreach ($info[$tag][PATTERNS_EXPORT] as $func_name => $func) {
        $out[$component][$tag][PATTERNS_EXPORT][$func_name] = $func;
      }

      // add files
      if (isset($info[$tag][PATTERNS_FILES])) {
        $out[$component][$tag][PATTERNS_FILES] = $info[$tag][PATTERNS_FILES];
      }
      continue;
    }
    if (!isset($info[$tag][PATTERNS_EXPORT][$export_func])) {
      drush_set_error(dt('Component "' . $component . '", tag "' . $tag . '" has no xport function "' . $export_func . '"'));
      continue;
    }
    $out[$component][$tag][PATTERNS_EXPORT][$export_func] = $info[$tag][PATTERNS_EXPORT][$export_func];

    // add files
    if (isset($info[$tag][PATTERNS_FILES])) {
      $out[$component][$tag][PATTERNS_FILES] = $info[$tag][PATTERNS_FILES];
    }
  }
  return $out;
}