You are here

class StaticPreviewRequestSubscriber in Tome 8

Routes valid requests to the static build.

@internal

Hierarchy

Expanded class hierarchy of StaticPreviewRequestSubscriber

2 files declare their use of StaticPreviewRequestSubscriber
StaticPreviewController.php in modules/tome_static/src/Controller/StaticPreviewController.php
StaticPreviewForm.php in modules/tome_static/src/Form/StaticPreviewForm.php
1 string reference to 'StaticPreviewRequestSubscriber'
tome_static.services.yml in modules/tome_static/tome_static.services.yml
modules/tome_static/tome_static.services.yml
1 service uses StaticPreviewRequestSubscriber
tome_static.preview_request_subscriber in modules/tome_static/tome_static.services.yml
Drupal\tome_static\EventSubscriber\StaticPreviewRequestSubscriber

File

modules/tome_static/src/EventSubscriber/StaticPreviewRequestSubscriber.php, line 22

Namespace

Drupal\tome_static\EventSubscriber
View source
class StaticPreviewRequestSubscriber implements EventSubscriberInterface {
  use PathTrait;
  use StringTranslationTrait;

  /**
   * The key used to identify a static session.
   */
  const SESSION_KEY = 'tome_static_preview';

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

  /**
   * The session.
   *
   * @var \Symfony\Component\HttpFoundation\Session\Session
   */
  protected $session;

  /**
   * The current route match.
   *
   * @var \Drupal\Core\Routing\CurrentRouteMatch
   */
  protected $routeMatch;

  /**
   * Constructs a new StaticPreviewRequestSubscriber object.
   *
   * @param \Drupal\tome_static\StaticGeneratorInterface $static
   *   The static generator.
   * @param \Symfony\Component\HttpFoundation\Session\Session $session
   *   The session.
   * @param \Drupal\Core\Routing\CurrentRouteMatch $route_match
   *   The current route match.
   */
  public function __construct(StaticGeneratorInterface $static, Session $session, CurrentRouteMatch $route_match) {
    $this->static = $static;
    $this->session = $session;
    $this->routeMatch = $route_match;
  }

  /**
   * Sets a response in case of a Dynamic Page Cache hit.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   The event to process.
   */
  public function onRequest(GetResponseEvent $event) {
    $request = $event
      ->getRequest();
    if (!$this->session
      ->get(static::SESSION_KEY) || $this->routeMatch
      ->getRouteName() === 'tome_static.preview_exit') {
      return;
    }
    $path = realpath($this
      ->joinPaths($this->static
      ->getStaticDirectory(), $request
      ->getPathInfo()));
    if (is_dir($path)) {
      $path = $this
        ->joinPaths($path, 'index.html');
    }
    $exit_message = $this
      ->t('<a style="@style" href=":link">Exit preview</a>', [
      ':link' => Url::fromRoute('tome_static.preview_exit')
        ->setAbsolute(TRUE)
        ->toString(),
      '@style' => 'position: fixed;top: 0;left: calc(50% - 60px);z-index: 2147483647;text-align: center;padding: 10px 20px;background: #00000063;border: 0;border-radius: 0 0 10px 10px;color: white;text-decoration: none;font-size: 13px;font-weight: bold;font-family: "Source Sans Pro","Lucida Grande",Verdana,sans-serif;',
    ]);
    if (strpos($path, realpath($this->static
      ->getStaticDirectory())) === 0 && file_exists($path)) {
      $contents = file_get_contents($path);
      if (pathinfo($path, PATHINFO_EXTENSION) === 'html') {
        if (strpos($contents, '</body>') !== FALSE) {
          $contents = str_replace('</body>', $exit_message . '</body>', $contents);
        }
        else {
          $contents .= $exit_message;
        }
      }
      $mime_type = (new File($path))
        ->getMimeType();
      $code = 200;
    }
    else {
      $contents = $this
        ->t('<p>Request path not present in the static build.</p>') . $exit_message;
      $mime_type = 'text/html';
      $code = 404;
    }
    $event
      ->setResponse(new Response($contents, $code, [
      'Content-Type' => $mime_type,
    ]));
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = [];
    $events[KernelEvents::REQUEST][] = [
      'onRequest',
      30,
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PathTrait::joinPaths protected function Joins multiple paths.
StaticPreviewRequestSubscriber::$routeMatch protected property The current route match.
StaticPreviewRequestSubscriber::$session protected property The session.
StaticPreviewRequestSubscriber::$static protected property The static generator.
StaticPreviewRequestSubscriber::getSubscribedEvents public static function
StaticPreviewRequestSubscriber::onRequest public function Sets a response in case of a Dynamic Page Cache hit.
StaticPreviewRequestSubscriber::SESSION_KEY constant The key used to identify a static session.
StaticPreviewRequestSubscriber::__construct public function Constructs a new StaticPreviewRequestSubscriber object.
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.