StaticPreviewCommand.php in Tome 8
File
modules/tome_static/src/Commands/StaticPreviewCommand.php
View source
<?php
namespace Drupal\tome_static\Commands;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\tome_base\CommandBase;
use Drupal\tome_static\StaticGeneratorInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
class StaticPreviewCommand extends CommandBase {
use StringTranslationTrait;
protected $static;
public function __construct(StaticGeneratorInterface $static) {
parent::__construct();
$this->static = $static;
}
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);
}
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);
}
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();
}
}