You are here

class RequestPreparer in Tome 8

Utility class to prepare Drupal for new requests.

Hopefully, in the future, this will not be necessary, which is why it isn't used by the static generator directly. This is a hotfix for a core problem.

Hierarchy

Expanded class hierarchy of RequestPreparer

See also

\Drupal\tome_static\StaticGeneratorInterface::requestPath

3 files declare their use of RequestPreparer
StaticExportPathCommand.php in modules/tome_static/src/Commands/StaticExportPathCommand.php
StaticGeneratorForm.php in modules/tome_static/src/Form/StaticGeneratorForm.php
TomeStaticQueueWorker.php in modules/tome_static/modules/tome_static_cron/src/Plugin/QueueWorker/TomeStaticQueueWorker.php
1 string reference to 'RequestPreparer'
tome_static.services.yml in modules/tome_static/tome_static.services.yml
modules/tome_static/tome_static.services.yml
1 service uses RequestPreparer
tome_static.request_preparer in modules/tome_static/tome_static.services.yml
Drupal\tome_static\RequestPreparer

File

modules/tome_static/src/RequestPreparer.php, line 22

Namespace

Drupal\tome_static
View source
class RequestPreparer {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The decorated context repository.
   *
   * @var \Drupal\tome_static\LazyResettableContextRepository
   */
  protected $contextRepository;

  /**
   * The decorated path matcher.
   *
   * @var \Drupal\tome_static\ResettablePathMatcher
   */
  protected $pathMatcher;

  /**
   * The menu active trail cache collector.
   *
   * @var \Drupal\Core\Menu\MenuActiveTrailInterface
   */
  protected $menuActiveTrail;

  /**
   * The event dispatcher.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * Constructs a RequestPreparer object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\tome_static\LazyResettableContextRepository $context_repository
   *   The decorated context repository.
   * @param \Drupal\tome_static\ResettablePathMatcher $path_matcher
   *   The decorated path matcher.
   * @param \Drupal\Core\Menu\MenuActiveTrailInterface $menu_active_trail
   *   The menu active trail cache collector.
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
   *   The event dispatcher.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, LazyResettableContextRepository $context_repository, ResettablePathMatcher $path_matcher, MenuActiveTrailInterface $menu_active_trail, EventDispatcherInterface $event_dispatcher, LanguageManagerInterface $language_manager) {
    $this->entityTypeManager = $entity_type_manager;
    $this->contextRepository = $context_repository;
    $this->pathMatcher = $path_matcher;
    $this->menuActiveTrail = $menu_active_trail;
    $this->eventDispatcher = $event_dispatcher;
    $this->languageManager = $language_manager;
  }

  /**
   * Prepares Drupal for a new request.
   */
  public function prepareForRequest() {

    // Some access control handlers, like block's, determine entity access
    // based on the route (via conditions, in this case).
    foreach ($this->entityTypeManager
      ->getDefinitions() as $definition) {
      $this->entityTypeManager
        ->getAccessControlHandler($definition
        ->id())
        ->resetCache();
    }

    // Some contrib modules, like metatag, statically cache route data.
    drupal_static_reset();

    // The context repository caches all context values in memory.
    $this->contextRepository
      ->resetCache();

    // The front page route is statically cached.
    $this->pathMatcher
      ->resetCache();

    // Persist cached active trail information cache as if the request ended.
    if ($this->menuActiveTrail instanceof DestructableInterface) {
      $this->menuActiveTrail
        ->destruct();
    }

    // Reset active trail cache.
    if ($this->menuActiveTrail instanceof CacheCollectorInterface) {
      $this->menuActiveTrail
        ->reset();
    }

    // Reset the language manager.
    $this->languageManager
      ->reset();

    // Allow module-specific code to prepare as well.
    $this->eventDispatcher
      ->dispatch(TomeStaticEvents::REQUEST_PREPARE);

    // Reset unique HTML IDs.
    Html::resetSeenIds();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RequestPreparer::$contextRepository protected property The decorated context repository.
RequestPreparer::$entityTypeManager protected property The entity type manager.
RequestPreparer::$eventDispatcher protected property The event dispatcher.
RequestPreparer::$languageManager protected property The language manager.
RequestPreparer::$menuActiveTrail protected property The menu active trail cache collector.
RequestPreparer::$pathMatcher protected property The decorated path matcher.
RequestPreparer::prepareForRequest public function Prepares Drupal for a new request.
RequestPreparer::__construct public function Constructs a RequestPreparer object.