You are here

protected function StaticPreviewCommand::startBrowser in Tome 8

Opens a web browser for the given URL.

Parameters

string $url: An absolute URL.

int $sleep: An amount of time to wait before opening the browser, in seconds.

1 call to StaticPreviewCommand::startBrowser()
StaticPreviewCommand::execute in modules/tome_static/src/Commands/StaticPreviewCommand.php

File

modules/tome_static/src/Commands/StaticPreviewCommand.php, line 71

Class

StaticPreviewCommand
Contains the tome:preview command.

Namespace

Drupal\tome_static\Commands

Code

protected function startBrowser($url, $sleep = NULL) {
  $browser = FALSE;
  foreach ([
    'xdg-open',
    'open',
  ] as $command) {
    if (shell_exec("which {$command} 2> /dev/null")) {
      $browser = $command;
      break;
    }
  }
  if (!$browser) {
    $this->io
      ->success("Static site server running at {$url}");
    return;
  }
  $this->io
    ->success("Opening browser at {$url}");
  if ($sleep) {
    sleep($sleep);
  }
  $process = new Process([
    $browser,
    $url,
  ]);
  $process
    ->run();
}