class StaticPreviewCommand in Tome 8
Contains the tome:preview command.
@internal
Hierarchy
- class \Drupal\tome_base\CommandBase extends \Symfony\Component\Console\Command\Command uses ExecutableFinderTrait, ProcessTrait
- class \Drupal\tome_static\Commands\StaticPreviewCommand uses StringTranslationTrait
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\CommandsView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CommandBase:: |
protected | property | The current executable path. | |
CommandBase:: |
protected | property | The IO decorator. | |
CommandBase:: |
protected | function | ||
CommandBase:: |
protected | function |
Returns the IO decorator, for reporting errors. Overrides ProcessTrait:: |
|
ExecutableFinderTrait:: |
protected | function | Finds an executable string for the current process. | |
ProcessTrait:: |
protected | function | Displays errors using the IO component. | |
ProcessTrait:: |
protected | function | Runs a single command and outputs errors if encountered. | |
ProcessTrait:: |
protected | function | Runs commands with concurrency. | |
StaticPreviewCommand:: |
protected | property | The static service. | |
StaticPreviewCommand:: |
protected | function | ||
StaticPreviewCommand:: |
protected | function | ||
StaticPreviewCommand:: |
protected | function | Opens a web browser for the given URL. | |
StaticPreviewCommand:: |
public | function | Constructs a StaticPreviewCommand instance. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |