You are here

protected function ServerCommand::start in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Command/ServerCommand.php \Drupal\Core\Command\ServerCommand::start()

Starts up a webserver with a running Drupal.

Parameters

string $host: The hostname of the webserver.

int $port: The port to start the webserver on.

\Drupal\Core\DrupalKernelInterface $kernel: The Drupal kernel.

\Symfony\Component\Console\Input\InputInterface $input: The input.

\Symfony\Component\Console\Style\SymfonyStyle $io: The IO.

Return value

int The exit status of the PHP in-built webserver command.

1 call to ServerCommand::start()
ServerCommand::execute in core/lib/Drupal/Core/Command/ServerCommand.php

File

core/lib/Drupal/Core/Command/ServerCommand.php, line 210

Class

ServerCommand
Runs the PHP webserver for a Drupal site for local testing/development.

Namespace

Drupal\Core\Command

Code

protected function start($host, $port, DrupalKernelInterface $kernel, InputInterface $input, SymfonyStyle $io) {
  $finder = new PhpExecutableFinder();
  $binary = $finder
    ->find();
  if ($binary === FALSE) {
    throw new \RuntimeException('Unable to find the PHP binary.');
  }
  $io
    ->writeln("<info>Drupal development server started:</info> <http://{$host}:{$port}>");
  $io
    ->writeln('<info>This server is not meant for production use.</info>');
  $one_time_login = "http://{$host}:{$port}{$this->getOneTimeLoginUrl()}/login";
  $io
    ->writeln("<info>One time login url:</info> <{$one_time_login}>");
  $io
    ->writeln('Press Ctrl-C to quit the Drupal development server.');
  if (!$input
    ->getOption('suppress-login')) {
    if ($this
      ->openBrowser("{$one_time_login}?destination=" . urlencode("/"), $io) === 1) {
      $io
        ->error('Error while opening up a one time login URL');
    }
  }

  // Use the Process object to construct an escaped command line.
  $process = new Process([
    $binary,
    '-S',
    $host . ':' . $port,
    '.ht.router.php',
  ], $kernel
    ->getAppRoot(), [], NULL, NULL);
  if ($io
    ->isVerbose()) {
    $io
      ->writeln("<info>Server command:</info> {$process->getCommandLine()}");
  }

  // Carefully manage output so we can display output only in verbose mode.
  $descriptors = [];
  $descriptors[0] = STDIN;
  $descriptors[1] = [
    'pipe',
    'w',
  ];
  $descriptors[2] = [
    'pipe',
    'w',
  ];
  $server = proc_open($process
    ->getCommandLine(), $descriptors, $pipes, $kernel
    ->getAppRoot());
  if (is_resource($server)) {
    if ($io
      ->isVerbose()) {

      // Write a blank line so that server output and the useful information are
      // visually separated.
      $io
        ->writeln('');
    }
    $server_status = proc_get_status($server);
    while ($server_status['running']) {
      if ($io
        ->isVerbose()) {
        fpassthru($pipes[2]);
      }
      sleep(1);
      $server_status = proc_get_status($server);
    }
  }
  return proc_close($server);
}