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)
1 call to drush_shell_alias_replace()
_drush_bootstrap_drush in includes/bootstrap.inc
Initial Drush bootstrap phase.

File

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

Code

function drush_shell_alias_replace() {
  $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');
      $target_site_alias = drush_get_context('DRUSH_TARGET_SITE_ALIAS', FALSE);
      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 (function_exists('str_getcsv')) {
        $alias_value = str_getcsv($alias_value, ' ');
      }
      else {
        $alias_value = explode(' ', $alias_value);
      }
    }
    drush_log(dt('Shell alias found: !key => !value', array('!key' => $first, '!value' => implode(' ', $alias_value))), 'debug');
    $replacement = $alias_value;
    if (substr($alias_value[0], 0, 1) == '!') {
      $replacement[0] = ltrim($replacement[0], '!');
      array_unshift($replacement, 'core-execute');
    }
    $pos = array_search($first, $argv);
    array_splice($argv, $pos, 1, $replacement);
    drush_set_context('argv', $argv);
    drush_parse_args();
    _drush_bootstrap_global_options();
  }
}