You are here

class StaticCommand in Tome 8

Contains the tome:static command.

@internal

Hierarchy

Expanded class hierarchy of StaticCommand

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

File

modules/tome_static/src/Commands/StaticCommand.php, line 20

Namespace

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

  /**
   * The default number of processes to invoke.
   *
   * @var int
   */
  const PROCESS_COUNT = 5;

  /**
   * The default number of paths to export per process.
   */
  const PATH_COUNT = 5;

  /**
   * The default number of retry per failed process.
   */
  const RETRY_COUNT = 1;

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

  /**
   * The state system.
   *
   * @var \\Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * Constructs a StaticCommand instance.
   *
   * @param \Drupal\tome_static\StaticGeneratorInterface $static
   *   The static service.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state system.
   */
  public function __construct(StaticGeneratorInterface $static, StateInterface $state) {
    parent::__construct();
    $this->static = $static;
    $this->state = $state;
  }

  /**
   * {@inheritdoc}
   */
  protected function configure() {
    $this
      ->setName('tome:static')
      ->setDescription('Exports all pages on your site to static HTML.')
      ->addOption('process-count', NULL, InputOption::VALUE_OPTIONAL, 'Limits the number of processes to run concurrently.', static::PROCESS_COUNT)
      ->addOption('path-count', NULL, InputOption::VALUE_OPTIONAL, 'The number of paths to export per process.', static::PATH_COUNT)
      ->addOption('run-server', NULL, InputOption::VALUE_NONE, 'If a local HTTP server should be started after the export.')
      ->addOption('port', NULL, InputOption::VALUE_OPTIONAL, 'The port to run the server on.', 8889)
      ->addOption('ignore-warnings', NULL, InputOption::VALUE_NONE, 'If configuration warnings should be shown.')
      ->addOption('path-pattern', NULL, InputOption::VALUE_OPTIONAL, 'If you only want to export a specific paths based on pattern.', '')
      ->addOption('retry-count', NULL, InputOption::VALUE_OPTIONAL, 'The number of retry per failed process', static::RETRY_COUNT)
      ->addOption('yes', 'y', InputOption::VALUE_NONE, 'Assume "yes" as answer to all prompts,');
  }

  /**
   * {@inheritdoc}
   */
  protected function execute(InputInterface $input, OutputInterface $output) {
    $options = $input
      ->getOptions();
    if ($this->state
      ->get(StaticGeneratorInterface::STATE_KEY_BUILDING, FALSE)) {
      if (!$options['yes'] && !$this
        ->io()
        ->confirm('Another user may be running a static build, proceed only if the last build failed unexpectedly. Ignore and continue build?', FALSE)) {
        return 0;
      }
    }
    $this->state
      ->set(StaticGeneratorInterface::STATE_KEY_BUILDING, TRUE);
    $warnings = $this
      ->getWarnings();
    if (empty($options['uri']) || $options['uri'] === 'http://default') {
      $warnings[] = 'No "--uri" option provided. This could lead to invalid absolute URLs. To resolve, pass the "--uri" option.';
    }
    if (!$options['ignore-warnings'] && $warnings) {
      $warnings[] = 'To suppress these messages, pass the --ignore-warnings option.';
      $this->io
        ->comment($warnings);
    }

    // @todo Is this a Drush bug?
    if (base_path() !== '/') {
      \Drupal::urlGenerator()
        ->getContext()
        ->setBaseUrl(rtrim(base_path(), '/'));
    }
    $this->static
      ->cleanupStaticDirectory();
    $this->static
      ->prepareStaticDirectory();
    $paths = $this->static
      ->getPaths();

    // Add a filter to export only pattern based paths.
    if ($options['path-pattern'] !== '') {
      $paths = preg_filter($options['path-pattern'], '$0', $paths);
    }
    $this->io
      ->writeln('Generating static HTML...');
    $this
      ->exportPaths($paths, [], $options['process-count'], $options['path-count'], TRUE, $options['retry-count'], $options['uri']);
    $this->io
      ->success('Exported static HTML and related assets.');
    $this->state
      ->set(StaticGeneratorInterface::STATE_KEY_BUILDING, FALSE);
    if ($options['run-server']) {
      $this
        ->runCommand($this->executable . ' tome:preview --port=' . escapeshellarg($options['port']), NULL, NULL);
    }
  }

  /**
   * Exports the given paths to the static directory.
   *
   * @param string[] $paths
   *   An array of paths.
   * @param array $old_paths
   *   An array of paths that have already been processed.
   * @param int $process_count
   *   The number of processes to invoke.
   * @param int $path_count
   *   The number of paths to export per process.
   * @param bool $show_progress
   *   Whether or not a progress bar should be shown.
   * @param int $retry_count
   *   The number of times to retry a failed command.
   * @param string $uri
   *   The URI of the site, probably passed by -l or --uri.
   */
  protected function exportPaths(array $paths, array $old_paths, $process_count, $path_count, $show_progress, $retry_count, $uri) {
    $paths = $this->static
      ->exportPaths($paths);
    if (empty($paths)) {
      return;
    }
    if ($this->io
      ->isVerbose()) {
      $this->io
        ->writeln('Exporting paths:');
      $this->io
        ->listing($paths);
    }
    $commands = [];
    $chunks = array_chunk($paths, $path_count);
    foreach ($chunks as $chunk) {
      $command = $this->executable . ' tome:static-export-path ' . escapeshellarg(implode(',', $chunk)) . ' --return-json --process-count=' . escapeshellarg($process_count) . ' --uri=' . escapeshellarg($uri);
      $commands[] = $command;
    }
    $show_progress && $this->io
      ->progressStart(count($paths));
    $invoke_paths = [];
    $collected_errors = $this
      ->runCommands($commands, $process_count, $retry_count, function (Process $process) use ($show_progress, &$invoke_paths, $path_count) {
      $show_progress && $this->io
        ->progressAdvance($path_count);
      $output = $process
        ->getOutput();
      if (!empty($output) && ($json = json_decode($output, TRUE))) {
        $invoke_paths = array_merge($invoke_paths, $json);
      }
    });
    $invoke_paths = array_diff($invoke_paths, $old_paths);
    $old_paths = array_merge($old_paths, $invoke_paths);
    $show_progress && $this->io
      ->progressFinish();
    if (!empty($collected_errors)) {
      $this
        ->displayErrors($collected_errors);
    }
    if (count($invoke_paths)) {
      $this->io
        ->writeln('Processing related assets and paths...');
      $this
        ->exportPaths($invoke_paths, $old_paths, $process_count, $path_count, $show_progress, $retry_count, $uri);
    }
  }

}

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.
StaticCommand::$state protected property The state system.
StaticCommand::$static protected property The static service.
StaticCommand::configure protected function 1
StaticCommand::execute protected function 1
StaticCommand::exportPaths protected function Exports the given paths to the static directory.
StaticCommand::PATH_COUNT constant The default number of paths to export per process.
StaticCommand::PROCESS_COUNT constant The default number of processes to invoke.
StaticCommand::RETRY_COUNT constant The default number of retry per failed process. 1
StaticCommand::__construct public function Constructs a StaticCommand instance. 1
StaticUITrait::formatPathException protected function Formats an exception caught when requesting a path.
StaticUITrait::getWarnings protected function Collects warnings to help users correct issues in rendered HTML.
StaticUITrait::t abstract protected function Translates a string to the current language or to a given language.
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