You are here

class StaticPreviewCommand in Tome 8

Contains the tome:preview command.

@internal

Hierarchy

Expanded class hierarchy of StaticPreviewCommand

1 string reference to 'StaticPreviewCommand'
tome_static.services.yml in modules/tome_static/tome_static.services.yml
modules/tome_static/tome_static.services.yml
1 service uses StaticPreviewCommand
tome_static.preview_command in modules/tome_static/tome_static.services.yml
Drupal\tome_static\Commands\StaticPreviewCommand

File

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

Namespace

Drupal\tome_static\Commands
View source
class StaticPreviewCommand extends CommandBase {
  use StringTranslationTrait;

  /**
   * The static service.
   *
   * @var \Drupal\tome_static\StaticGeneratorInterface
   */
  protected $static;

  /**
   * Constructs a StaticPreviewCommand instance.
   *
   * @param \Drupal\tome_static\StaticGeneratorInterface $static
   *   The static service.
   */
  public function __construct(StaticGeneratorInterface $static) {
    parent::__construct();
    $this->static = $static;
  }

  /**
   * {@inheritdoc}
   */
  protected function configure() {
    $this
      ->setName('tome:preview')
      ->setDescription('Preview your static site.')
      ->addOption('port', NULL, InputOption::VALUE_OPTIONAL, 'The port to run the server on.', 8889);
  }

  /**
   * {@inheritdoc}
   */
  protected function execute(InputInterface $input, OutputInterface $output) {
    if (!file_exists($this->static
      ->getStaticDirectory())) {
      $this
        ->io()
        ->error('Static directory does not exist. Have you ran the "tome:static" command yet?');
      return 1;
    }
    $options = $input
      ->getOptions();
    $url = '127.0.0.1:' . $options['port'];
    $this
      ->startBrowser('http://' . $url . base_path(), 2);
    $this
      ->runCommand('php -S ' . escapeshellarg($url), $this->static
      ->getStaticDirectory(), NULL);
  }

  /**
   * Opens a web browser for the given URL.
   *
   * @param string $url
   *   An absolute URL.
   * @param int $sleep
   *   An amount of time to wait before opening the browser, in seconds.
   */
  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();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommandBase::$executable protected property The current executable path.
CommandBase::$io protected property The IO decorator.
CommandBase::initialize protected function
CommandBase::io protected function Returns the IO decorator, for reporting errors. Overrides ProcessTrait::io
ExecutableFinderTrait::findExecutable protected function Finds an executable string for the current process.
ProcessTrait::displayErrors protected function Displays errors using the IO component.
ProcessTrait::runCommand protected function Runs a single command and outputs errors if encountered.
ProcessTrait::runCommands protected function Runs commands with concurrency.
StaticPreviewCommand::$static protected property The static service.
StaticPreviewCommand::configure protected function
StaticPreviewCommand::execute protected function
StaticPreviewCommand::startBrowser protected function Opens a web browser for the given URL.
StaticPreviewCommand::__construct public function Constructs a StaticPreviewCommand instance.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.