function drush_shell_alias_replace

8.0.x command.inc drush_shell_alias_replace($target_site_alias)
6.x command.inc drush_shell_alias_replace()
7.x command.inc drush_shell_alias_replace($target_site_alias)
5.x command.inc drush_shell_alias_replace()
master command.inc drush_shell_alias_replace($target_site_alias)

Check if a shell alias exists for current request. If so, re-route to core-execute and pass alias value along with rest of CLI arguments.

1 call to drush_shell_alias_replace()
drush_preflight in includes/preflight.inc
During the initialization of Drush, this is the first step where we load our configuration and commandfiles, and select the site we are going to operate on; however, we take no irreversible actions (e.g. site bootstrapping). This allows commands that…

File

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

Code

function drush_shell_alias_replace($target_site_alias) {
  $escape = TRUE;
  $args = drush_get_arguments();
  $argv = drush_get_context('argv');
  $first = current($args);
  // @todo drush_get_option is awkward here.
  $shell_aliases = drush_get_context('shell-aliases', array());
  if (isset($shell_aliases[$first])) {
    // Shell alias found for first argument in the request.
    $alias_value = $shell_aliases[$first];
    if (!is_array($alias_value)) {
      // Shell aliases can have embedded variables such as {{@target}} and {{%root}}
      // that are replaced with the name of the target site alias, or the value of a
      // path alias defined in the target site alias record.  We only support replacements
      // when the alias value is a string; if it is already broken out into an array,
      // then the values therein are used literally.
      $alias_variables = array('{{@target}}' => '@none');
      if ($target_site_alias) {
        $alias_variables = array('{{@target}}' => $target_site_alias);
        $target = drush_sitealias_get_record($target_site_alias);
        foreach ($target as $key => $value) {
          if (!is_array($value)) {
            $alias_variables['{{' . $key . '}}'] = $value;
          }
        }
        if (array_key_exists('path-aliases', $target)) {
          foreach ($target['path-aliases'] as $key => $value) {
            // n.b. $key will contain something like "%root" or "%files".
            $alias_variables['{{' . $key . '}}'] = $value;
          }
        }
      }
      $alias_value = str_replace(array_keys($alias_variables), array_values($alias_variables), $alias_value);
      // Check for unmatched replacements
      $matches = array();
      $match_result = preg_match('/{{[%@#]*[a-z0-9.]*}}/', $alias_value, $matches);
      if ($match_result) {
        $unmatched_replacements = implode(', ', $matches);
        $unmatched_replacements = preg_replace('/[{}]/', '', $unmatched_replacements);
        return drush_set_error('DRUSH_SHELL_ALIAS_UNMATCHED_REPLACEMENTS', dt('The shell alias @alias-name uses replacements "@unmatched". You must use this command with a site alias (e.g. `drush @myalias @alias-name ...`) that defines all of these variables.', array('@alias-name' => $first, '@unmatched' => $unmatched_replacements)));
      }
      if (substr($alias_value, 0, 1) == '!') {
        $alias_value = ltrim($alias_value, '!');
        $alias_value = array('core-execute', $alias_value);
        $escape = FALSE;
      }
      else {
        // Respect quoting. See http://stackoverflow.com/questions/2202435/php-ex
        $alias_value = str_getcsv($alias_value, ' ');
      }
    }
    drush_log(dt('Shell alias found: !key => !value', array('!key' => $first, '!value' => implode(' ', $alias_value))), LogLevel::DEBUG);
    $pos = array_search($first, $argv);
    $number = 1;
    if ($target_site_alias && ($argv[$pos - 1] == $target_site_alias)) {
      --$pos;
      ++$number;
    }
    array_splice($argv, $pos, $number, $alias_value);
    if (!$escape) {
      drush_set_option('escape', FALSE);
    }
    drush_set_context('argv', $argv);
    drush_parse_args();
    _drush_preflight_global_options();
  }
}