function _drush_backend_generate_command

8.0.x backend.inc _drush_backend_generate_command($site_record, $command, $args = array(), $command_options = array(), $backend_options = array())
6.x backend.inc _drush_backend_generate_command($site_record, $command, $args = array(), $command_options = array(), $backend_options = array())
7.x backend.inc _drush_backend_generate_command($site_record, $command, $args = array(), $command_options = array(), $backend_options = array())
3.x backend.inc _drush_backend_generate_command($command, $args, &$data, $method = 'GET', $drush_path = null, $hostname = null, $username = null, $ssh_options = NULL)
4.x backend.inc _drush_backend_generate_command($command, $args, &$data, $method = 'GET', $drush_path = null, $hostname = null, $username = null, $ssh_options = NULL)
5.x backend.inc _drush_backend_generate_command($site_record, $command, $args = array(), $command_options = array(), $backend_options = array())
master backend.inc _drush_backend_generate_command($site_record, $command, $args = array(), $command_options = array(), $backend_options = array())

Generate a command to execute.

Parameters

site_record: An array containing information used to generate the command. 'remote-host' Optional. A remote host to execute the drush command on. 'remote-user' Optional. Defaults to the current user. If you specify this, you can choose which module to send. 'ssh-options' Optional. Defaults to "-o PasswordAuthentication=no" '#env-vars' Optional. An associative array of environmental variables to prefix the Drush command with. 'path-aliases' Optional; contains paths to folders and executables useful to the command. '%drush-script' Optional. Defaults to the current drush.php file on the local machine, and to simply 'drush' (the drush script in the current PATH) on remote servers. You may also specify a different drush.php script explicitly. You will need to set this when calling drush on a remote server if 'drush' is not in the PATH on that machine.

command: A defined drush command such as 'cron', 'status' or any of the available ones such as 'drush pm'.

args: An array of arguments for the command.

command_options: Optional. An array containing options to pass to the remote script. Array items with a numeric key are treated as optional arguments to the command. This parameter is a reference, as any options that have been represented as either an option, or an argument will be removed. This allows you to pass the left over options as a JSON encoded string, without duplicating data.

backend_options: Optional. An array of options for the invocation. @see drush_backend_invoke for documentation.

Return value

A text string representing a fully escaped command.

1 call to _drush_backend_generate_command()
drush_backend_invoke_concurrent in includes/backend.inc
Execute a new local or remote command in a new process.

File

includes/backend.inc, line 1121
Drush backend API

Code

function _drush_backend_generate_command($site_record, $command, $args = array(), $command_options = array(), $backend_options = array()) {
  $site_record += array(
    'remote-host' => NULL,
    'remote-user' => NULL,
    'ssh-options' => NULL,
    'path-aliases' => array(),
  );
  $backend_options += array(
    '#tty' => FALSE,
  );

  $hostname = $site_record['remote-host'];
  $username = $site_record['remote-user'];
  $ssh_options = $site_record['ssh-options'];
  $os = drush_os($site_record);

  if (drush_is_local_host($hostname)) {
    $hostname = null;
  }

  foreach ($command_options as $key => $arg) {
    if (is_numeric($key)) {
      $args[] = $arg;
      unset($command_options[$key]);
    }
  }

  $cmd[] = $command;
  foreach ($args as $arg) {
    $cmd[] = drush_escapeshellarg($arg, $os);
  }
  $option_str = _drush_backend_argument_string($command_options, $os);
  if (!empty($option_str)) {
    $cmd[] = " " . $option_str;
  }
  $command = implode(' ', array_filter($cmd, 'strlen'));
  if (isset($hostname)) {
    if (drush_is_windows($os)) {
      if (isset($username)) {
        $username = " -u:" . drush_escapeshellarg($username, "LOCAL");
        if (array_key_exists('winrs-password', $site_record)) {
          $username .= " -p:" . drush_escapeshellarg($site_record['winrs-password'], "LOCAL");
        }
      }
      $command = "winrs" . $username . " -r:" . drush_escapeshellarg($hostname, "LOCAL") . " " . drush_escapeshellarg($command, "LOCAL");
    }
    else {
      $username = (isset($username)) ? drush_escapeshellarg($username, "LOCAL") . "@" : '';
      $ssh_options = $site_record['ssh-options'];
      $ssh_options = (isset($ssh_options)) ? $ssh_options : drush_get_option('ssh-options', "-o PasswordAuthentication=no");

      $ssh_cmd[] = "ssh";
      $ssh_cmd[] = $ssh_options;
      if ($backend_options['#tty']) {
        $ssh_cmd[] = '-t';
      }
      $ssh_cmd[] = $username . drush_escapeshellarg($hostname, "LOCAL");
      $ssh_cmd[] = drush_escapeshellarg($command . ' 2>&1', "LOCAL");

      // Remove NULLs and separate with spaces
      $command = implode(' ', array_filter($ssh_cmd, 'strlen'));
    }
  }

  return $command;
}