You are here

public function StaticGenerator::requestPath in Tome 8

Requests and exports a given path.

This method should only be called for paths that need a Drupal bootstrap.

Note that calling this method multiple times in one bootstrap may have result in improper renders - it's recommended that this is called once per request.

Parameters

string $path: A path to export, or an entity to load and export in the format "_entity:entity_type_id:langcode:entity_id".

Return value

string[] An array of paths that should be passed to ::exportPaths.

Overrides StaticGeneratorInterface::requestPath

File

modules/tome_static/src/StaticGenerator.php, line 138

Class

StaticGenerator
Handles static site generation.

Namespace

Drupal\tome_static

Code

public function requestPath($path) {
  $this->accountSwitcher
    ->switchTo(new AnonymousUserSession());
  $invoke_paths = [];
  $original_path = $path;
  $event = new PathPlaceholderEvent($path);
  $this->eventDispatcher
    ->dispatch(TomeStaticEvents::PATH_PLACEHOLDER, $event);
  if ($event
    ->isInvalid()) {
    $this->accountSwitcher
      ->switchBack();
    return [];
  }
  $path = $event
    ->getPath();
  $request = Request::create($path, 'GET', [], [], [], $this->currentRequest->server
    ->all());
  $request->attributes
    ->set(static::REQUEST_KEY, static::REQUEST_KEY);
  $previous_stack = $this
    ->replaceRequestStack($request);
  try {
    $response = $this->httpKernel
      ->handle($request, HttpKernelInterface::MASTER_REQUEST);
  } catch (\Exception $e) {
    $this->accountSwitcher
      ->switchBack();
    $this
      ->restoreRequestStack($previous_stack);
    throw $e;
  }
  $destination = $this
    ->getDestination($path);
  if ($response
    ->isRedirection() || $response
    ->isOk()) {
    $directory = dirname($destination);
    $this->fileSystem
      ->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY);

    // This is probably an image style derivative.
    if ($response instanceof BinaryFileResponse) {
      $file_path = $response
        ->getFile()
        ->getPathname();
      $this
        ->copyPath($file_path, $destination);
    }
    else {
      $content = $response
        ->getContent();
      if (strpos($response->headers
        ->get('Content-Type'), 'text/html') === 0) {
        $event = new ModifyHtmlEvent($content, $path);
        $this->eventDispatcher
          ->dispatch(TomeStaticEvents::MODIFY_HTML, $event);
        $content = $event
          ->getHtml();
        $invoke_paths = array_merge($invoke_paths, $this
          ->getHtmlAssets($content, $path), $event
          ->getInvokePaths());
        $invoke_paths = array_diff($invoke_paths, $event
          ->getExcludePaths());
      }
      file_put_contents($destination, $content);
    }
    $this->eventDispatcher
      ->dispatch(TomeStaticEvents::FILE_SAVED, new FileSavedEvent($destination));
    if ($response instanceof RedirectResponse) {
      $target_url = $this
        ->makeExternalUrlLocal($response
        ->getTargetUrl());
      if (!UrlHelper::isExternal($target_url)) {
        $invoke_paths[] = $target_url;
      }
    }
    $this->cache
      ->setCache($request, $response, $original_path, $destination);
  }
  $this
    ->restoreRequestStack($previous_stack);
  $this->accountSwitcher
    ->switchBack();
  return $this
    ->filterInvokePaths($invoke_paths, $request);
}