RouteCacheContext.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace Drupal\Core\Cache\Context;
  3. use Drupal\Core\Cache\CacheableMetadata;
  4. use Drupal\Core\Routing\RouteMatchInterface;
  5. /**
  6. * Defines the RouteCacheContext service, for "per route" caching.
  7. *
  8. * Cache context ID: 'route'.
  9. */
  10. class RouteCacheContext implements CacheContextInterface {
  11. /**
  12. * The route match.
  13. *
  14. * @var \Drupal\Core\Routing\RouteMatchInterface
  15. */
  16. protected $routeMatch;
  17. /**
  18. * Constructs a new RouteCacheContext class.
  19. *
  20. * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
  21. * The route match.
  22. */
  23. public function __construct(RouteMatchInterface $route_match) {
  24. $this->routeMatch = $route_match;
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public static function getLabel() {
  30. return t('Route');
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function getContext() {
  36. return $this->routeMatch->getRouteName() . hash('sha256', serialize($this->routeMatch->getRawParameters()->all()));
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function getCacheableMetadata() {
  42. return new CacheableMetadata();
  43. }
  44. }