function drush_core_runserver

8.0.x runserver.drush.inc drush_core_runserver($uri = NULL)
6.x runserver.drush.inc drush_core_runserver($uri = NULL)
7.x runserver.drush.inc drush_core_runserver($uri = NULL)
5.x runserver.drush.inc drush_core_runserver($uri = NULL)
master runserver.drush.inc drush_core_runserver($uri = NULL)

Callback for runserver command.

File

commands/runserver/runserver.drush.inc, line 132
Built in http server commands.

Code

function drush_core_runserver($uri = NULL) {
  global $user, $base_url;

  // Determine active configuration.
  $drush_default = array(
    'host' => '127.0.0.1',
    'port' => '8888',
    'path' => '',
  );
  $user_default = runserver_parse_uri(drush_get_option('default-server', '127.0.0.1:8888'));
  $uri = runserver_parse_uri($uri);
  if (!$uri) {
    return $uri;
  }
  // Populate defaults.
  $uri = $uri + $user_default + $drush_default;
  if (ltrim($uri['path'], '/') == '-') {
    // Allow a path of a single hyphen to clear a default path.
    $uri['path'] = '';
  }
  // Remove any leading slashes from the path, since that is what url() expects.
  $path = ltrim($uri['path'], '/');

  // Determine and set the new URI.
  $hostname = $addr = $uri['host'];
  if (drush_get_option('dns', FALSE)) {
    if (ip2long($hostname)) {
      $hostname = gethostbyaddr($hostname);
    }
    else {
      $addr = gethostbyname($hostname);
    }
  }
  drush_set_context('DRUSH_URI', 'http://' . $hostname . ':' . $uri['port']);


  // We pass in the currently logged in user (if set via the --user option),
  // which will automatically log this user in the browser during the first
  // request.
  if (drush_get_option('user', FALSE)) {
    drush_bootstrap_max(DRUSH_BOOTSTRAP_DRUPAL_LOGIN);
  }

  // We delete any registered files here, since they are not caught by Ctrl-C.
  _drush_delete_registered_files();

  // We set the effective base_url, since we have now detected the current site,
  // and need to ensure generated URLs point to our runserver host.
  // We also pass in the effective base_url to our auto_prepend_script via the
  // CGI environment. This allows Drupal to generate working URLs to this http
  // server, whilst finding the correct multisite from the HTTP_HOST header.
  $base_url = 'http://' . $addr . ':' . $uri['port'];
  $env['RUNSERVER_BASE_URL'] = $base_url;

  // We pass in an array of $conf overrides using the same approach.
  // By default we set drupal_http_request_fails to FALSE, as the httpserver
  // is unable to process simultaneous requests on some systems.
  // This is available as an option for developers to pass in their own
  // favorite $conf overrides (e.g. disabling css aggregation).
  $current_override = drush_get_option_list('variables', array('drupal_http_request_fails' => FALSE));
  foreach ($current_override as $name => $value) {
    if (is_numeric($name) && (strpos($value, '=') !== FALSE)) {
      list($name, $value) = explode('=', $value, 2);
    }
    $override[$name] = $value;
  }
  $env['RUNSERVER_CONF'] = urlencode(serialize($override));

  // We log in with the specified user ID (if set) via the password reset URL.
  $user_message = '';
  if ($user->uid) {
    $options = array('query' => array('destination' => $path));
    $browse = url(user_pass_reset_url($user) . '/login', $options);
    $user_message = ', logged in as ' . $user->name;
  }
  else {
    $browse = url($path);
  }

  drush_print(dt('HTTP server listening on !addr, port !port (see http://!hostname:!port/!path), serving site !site!user...', array('!addr' => $addr, '!hostname' => $hostname, '!port' => $uri['port'], '!path' => $path, '!site' => drush_get_context('DRUSH_DRUPAL_SITE', 'default'), '!user' => $user_message)));
  if (drush_get_option('server', FALSE) == 'builtin') {
    // Start php 5.4 builtin server.
    // Store data used by runserver-prepend.php in the shell environment.
    foreach ($env as $key => $value) {
      putenv($key . '=' . $value);
    }
    if (!empty($uri['path'])) {
      // Start a browser if desired. Include a 2 second delay to allow the
      // server to come up.
      drush_start_browser($browse, 2);
    }
    // Start the server using 'php -S'.
    $php = drush_get_option('php');
    drush_shell_exec_interactive($php . ' -S ' . $addr . ':' . $uri['port'] . ' --define auto_prepend_file="' . DRUSH_BASE_PATH . '/commands/runserver/runserver-prepend.php"');
  }
  else {
    // Include the library and our class that extends it.
    $lib = drush_get_option('lib', DRUSH_BASE_PATH . '/lib');
    $httpserverfile = $lib . '/' . DRUSH_HTTPSERVER_DIR_BASE . substr(DRUSH_HTTPSERVER_VERSION, 0, 7) . '/httpserver.php';
    require_once $httpserverfile;
    require_once 'runserver-drupal.inc';
    // Create a new httpserver instance and start it running.
    $server = new DrupalServer(array(
      'addr' => $addr,
      'port' => $uri['port'],
      'browse' => $browse,
      'hostname' => $hostname,
      'site' => drush_get_context('DRUSH_DRUPAL_SITE', 'default'),
      'server_id' => 'Drush runserver',
      'php_cgi' => drush_get_option('php-cgi', 'php-cgi') . ' --define auto_prepend_file="' . DRUSH_BASE_PATH . '/commands/runserver/runserver-prepend.php"',
      'env' => $env,
      'debug' => drush_get_context('DRUSH_DEBUG'),
    ));
    $server->run_forever();
  }
}