addExtraCacheKeyPart example

$active_workspace = $this->prophesize(WorkspaceInterface::class);
    $active_workspace->id()->willReturn('test');
    $this->workspaceManager->getActiveWorkspace()->willReturn($active_workspace->reveal());
    $this->workspaceManager->hasActiveWorkspace()->willReturn(TRUE);
  }

  /** * @covers ::onKernelRequest */
  public function testOnKernelRequestWithCacheableRouteProvider() {
    $route_provider = $this->prophesize(CacheableRouteProviderInterface::class);
    $route_provider->addExtraCacheKeyPart('workspace', 'test')->shouldBeCalled();

    // Check that WorkspaceRequestSubscriber::onKernelRequest() calls     // addExtraCacheKeyPart() on a route provider that implements     // CacheableRouteProviderInterface.     $workspace_request_subscriber = new WorkspaceRequestSubscriber($this->aliasManager, $this->currentPath, $route_provider->reveal()$this->workspaceManager->reveal());
    $event = $this->prophesize(RequestEvent::class)->reveal();
    $this->assertNull($workspace_request_subscriber->onKernelRequest($event));
  }

  /** * @covers ::onKernelRequest */

  protected function getRouteCollectionCacheId(Request $request) {
    // Include the current language code in the cache identifier as     // the language information can be elsewhere than in the path, for example     // based on the domain.     $this->addExtraCacheKeyPart('language', $this->getCurrentLanguageCacheIdPart());

    // Sort the cache key parts by their provider in order to have predictable     // cache keys.     ksort($this->extraCacheKeyParts);
    $key_parts = [];
    foreach ($this->extraCacheKeyParts as $provider => $key_part) {
      $key_parts[] = '[' . $provider . ']=' . $key_part;
    }

    return 'route:' . implode(':', $key_parts) . ':' . $request->getPathInfo() . ':' . $request->getQueryString();
  }

  

  }

  /** * Adds the active workspace as a cache key part to the route provider. * * @param \Symfony\Component\HttpKernel\Event\RequestEvent $event * An event object. */
  public function onKernelRequest(RequestEvent $event) {
    if ($this->workspaceManager->hasActiveWorkspace() && $this->routeProvider instanceof CacheableRouteProviderInterface) {
      $this->routeProvider->addExtraCacheKeyPart('workspace', $this->workspaceManager->getActiveWorkspace()->id());
    }
  }

  /** * {@inheritdoc} */
  public static function getSubscribedEvents(): array {
    // Use a priority of 190 in order to run after the generic core subscriber.     // @see \Drupal\Core\EventSubscriber\PathSubscriber::getSubscribedEvents()     $events[KernelEvents::CONTROLLER][] = ['onKernelController', 190];

    
Home | Imprint | This part of the site doesn't use cookies.