function drush_commands_categorize

8.0.x command.inc drush_commands_categorize($commands)
master command.inc drush_commands_categorize($commands)

Organize commands into categories. Used by help listing and core-cli.

Parameters

array $commands: A commands array as per drush_get_commands().

Return value

array $command_categories A categorized associative array of commands.

2 calls to drush_commands_categorize()
drush_cli_core_cli in commands/core/cli.drush.inc
Command callback.
drush_core_help in commands/core/help.drush.inc
Command callback for help command. This is the default command, when none other has been specified.

File

includes/command.inc, line 1061
The drush command engine.

Code

function drush_commands_categorize($commands) {
  $command_categories = array();
  $category_map = array();
  foreach ($commands as $key => $candidate) {
    if ((!array_key_exists('is_alias', $candidate) || !$candidate['is_alias']) && !$candidate['hidden']) {
      $category = $candidate['category'];
      // If we have decided to remap a category, remap every command
      if (array_key_exists($category, $category_map)) {
        $category = $category_map[$category];
      }
      if (!array_key_exists($category, $command_categories)) {
        $title = drush_command_invoke_all('drush_help', "meta:$category:title");
        $alternate_title = '';
        if (!$title) {
          // If there is no title, then check to see if the
          // command file is stored in a folder with the same
          // name as some other command file (e.g. 'core') that
          // defines a title.
          $alternate = basename($candidate['path']);
          $alternate_title = drush_command_invoke_all('drush_help', "meta:$alternate:title");
        }
        if (!empty($alternate_title)) {
          $category_map[$category] = $alternate;
          $category = $alternate;
          $title = $alternate_title;
        }
        $command_categories[$category]['title'] = empty($title) ? '' : $title[0];
        $summary = drush_command_invoke_all('drush_help', "meta:$category:summary");
        if ($summary) {
          $command_categories[$category]['summary'] = $summary[0];
        }
      }
      $candidate['category'] = $category;
      $command_categories[$category]['commands'][$key] = $candidate;
    }
  }

  // Make sure that 'core' is always first in the list
  $core_category = array('core' => $command_categories['core']);
  unset($command_categories['core']);

  // Post-process the categories that have no title.
  // Any that have fewer than 4 commands go into a section called "other".
  $processed_categories = array();
  $misc_categories = array();
  $other_commands = array();
  $other_categories = array();
  foreach ($command_categories as $key => $info) {
    if (empty($info['title'])) {
      $one_category = $key;
      if (count($info['commands']) < 4) {
        $other_commands = array_merge($other_commands, $info['commands']);
        $other_categories[] = $one_category;
      }
      else {
        $info['title'] = dt("All commands in !category", array('!category' => $key));
        $misc_categories[$one_category] = $info;
      }
    }
    else {
      $processed_categories[$key] = $info;
    }
  }
  $other_category = array();
  if (!empty($other_categories)) {
    $other_category[implode(',', $other_categories)] = array(
      'title' => dt("Other commands"),
      'commands' => $other_commands,
    );
  }
  asort($processed_categories);
  asort($misc_categories);
  $command_categories = array_merge($core_category, $processed_categories, $misc_categories, $other_category);

  // If the user specified --sort, then merge all of the remaining
  // categories together
  if (drush_get_option('sort', FALSE)) {
    $combined_commands = array();
    foreach ($command_categories as $key => $info) {
      $combined_commands = array_merge($combined_commands, $info['commands']);
    }
    $command_categories = array('all' => array(
      'commands' => $combined_commands,
      'title' => dt("Commands:"),
    ));
  }

  return $command_categories;
}