diff --git a/core/lib/Drupal/Core/Cache/Context/AccountPermissionsCacheContext.php b/core/lib/Drupal/Core/Cache/Context/AccountPermissionsCacheContext.php index f6a412a..aeb4fea 100644 --- a/core/lib/Drupal/Core/Cache/Context/AccountPermissionsCacheContext.php +++ b/core/lib/Drupal/Core/Cache/Context/AccountPermissionsCacheContext.php @@ -16,12 +16,12 @@ * * Cache context ID: 'user.permissions'. */ -class AccountPermissionsCacheContext extends UserCacheContext { +class AccountPermissionsCacheContext extends UserCacheContextBase implements CacheContextInterface { /** * The permissions hash generator. * - * @var \Drupal\user\PermissionsHashInterface + * @var \Drupal\Core\Session\PermissionsHashGeneratorInterface */ protected $permissionsHashGenerator; @@ -30,7 +30,7 @@ class AccountPermissionsCacheContext extends UserCacheContext { * * @param \Drupal\Core\Session\AccountInterface $user * The current user. - * @param \Drupal\user\PermissionsHashInterface $permissions_hash_generator + * @param \Drupal\Core\Session\PermissionsHashGeneratorInterface * The permissions hash generator. */ public function __construct(AccountInterface $user, PermissionsHashGeneratorInterface $permissions_hash_generator) { diff --git a/core/lib/Drupal/Core/Cache/Context/CacheContextsManager.php b/core/lib/Drupal/Core/Cache/Context/CacheContextsManager.php index 81c7e37..a0157f5 100644 --- a/core/lib/Drupal/Core/Cache/Context/CacheContextsManager.php +++ b/core/lib/Drupal/Core/Cache/Context/CacheContextsManager.php @@ -107,15 +107,13 @@ public function getLabels($include_calculated_cache_contexts = FALSE) { * @throws \InvalidArgumentException */ public function convertTokensToKeys(array $context_tokens) { + $this->validateTokens($context_tokens); $cacheable_metadata = new CacheableMetadata(); $optimized_tokens = $this->optimizeTokens($context_tokens); // Iterate over cache contexts that have been optimized away and get their // cacheable metadata. foreach (static::parseTokens(array_diff($context_tokens, $optimized_tokens)) as $context_token) { list($context_id, $parameter) = $context_token; - if (!in_array($context_id, $this->contexts)) { - throw new \InvalidArgumentException(SafeMarkup::format('"@context" is not a valid cache context ID.', ['@context' => $context_id])); - } $context = $this->getService($context_id); $cacheable_metadata = $cacheable_metadata->merge($context->getCacheableMetadata($parameter)); } @@ -124,9 +122,6 @@ public function convertTokensToKeys(array $context_tokens) { $keys = []; foreach (static::parseTokens($optimized_tokens) as $context) { list($context_id, $parameter) = $context; - if (!in_array($context_id, $this->contexts)) { - throw new \InvalidArgumentException(SafeMarkup::format('"@context" is not a valid cache context ID.', ['@context' => $context_id])); - } $keys[] = $this->getService($context_id)->getContext($parameter); } diff --git a/core/lib/Drupal/Core/Cache/Context/CookiesCacheContext.php b/core/lib/Drupal/Core/Cache/Context/CookiesCacheContext.php index 534d10c..bd5ab66 100644 --- a/core/lib/Drupal/Core/Cache/Context/CookiesCacheContext.php +++ b/core/lib/Drupal/Core/Cache/Context/CookiesCacheContext.php @@ -7,6 +7,8 @@ namespace Drupal\Core\Cache\Context; +use Drupal\Core\Cache\CacheableMetadata; + /** * Defines the CookiesCacheContext service, for "per cookie" caching. * @@ -35,4 +37,11 @@ public function getContext($cookie = NULL) { } } + /** + * {@inheritdoc} + */ + public function getCacheableMetadata($cookie = NULL) { + return new CacheableMetadata(); + } + } diff --git a/core/lib/Drupal/Core/Cache/Context/HeadersCacheContext.php b/core/lib/Drupal/Core/Cache/Context/HeadersCacheContext.php index 8286b36..8b4e450 100644 --- a/core/lib/Drupal/Core/Cache/Context/HeadersCacheContext.php +++ b/core/lib/Drupal/Core/Cache/Context/HeadersCacheContext.php @@ -7,6 +7,8 @@ namespace Drupal\Core\Cache\Context; +use Drupal\Core\Cache\CacheableMetadata; + /** * Defines the HeadersCacheContext service, for "per header" caching. * @@ -35,4 +37,11 @@ public function getContext($header = NULL) { } } + /** + * {@inheritdoc} + */ + public function getCacheableMetadata($header = NULL) { + return new CacheableMetadata(); + } + } diff --git a/core/lib/Drupal/Core/Cache/Context/IpCacheContext.php b/core/lib/Drupal/Core/Cache/Context/IpCacheContext.php index 9cc6713..aac2926 100644 --- a/core/lib/Drupal/Core/Cache/Context/IpCacheContext.php +++ b/core/lib/Drupal/Core/Cache/Context/IpCacheContext.php @@ -7,12 +7,14 @@ namespace Drupal\Core\Cache\Context; +use Drupal\Core\Cache\CacheableMetadata; + /** * Defines the IpCacheContext service, for "per IP address" caching. * * Cache context ID: 'ip'. */ -class IpCacheContext extends RequestStackCacheContextBase { +class IpCacheContext extends RequestStackCacheContextBase implements CacheContextInterface { /** * {@inheritdoc} @@ -28,4 +30,11 @@ public function getContext() { return $this->requestStack->getCurrentRequest()->getClientIp(); } + /** + * {@inheritdoc} + */ + public function getCacheableMetadata() { + return new CacheableMetadata(); + } + } diff --git a/core/lib/Drupal/Core/Cache/Context/IsSuperUserCacheContext.php b/core/lib/Drupal/Core/Cache/Context/IsSuperUserCacheContext.php index 1f30cf1..801de25 100644 --- a/core/lib/Drupal/Core/Cache/Context/IsSuperUserCacheContext.php +++ b/core/lib/Drupal/Core/Cache/Context/IsSuperUserCacheContext.php @@ -7,12 +7,14 @@ namespace Drupal\Core\Cache\Context; +use Drupal\Core\Cache\CacheableMetadata; + /** * Defines the IsSuperUserCacheContext service, for "super user or not" caching. * * Cache context ID: 'user.is_super_user'. */ -class IsSuperUserCacheContext extends UserCacheContext { +class IsSuperUserCacheContext extends UserCacheContextBase implements CacheContextInterface { /** * {@inheritdoc} @@ -28,4 +30,11 @@ public function getContext() { return ((int) $this->user->id()) === 1 ? '1' : '0'; } + /** + * {@inheritdoc} + */ + public function getCacheableMetadata($menu_name = NULL) { + return new CacheableMetadata(); + } + } diff --git a/core/lib/Drupal/Core/Cache/Context/PagersCacheContext.php b/core/lib/Drupal/Core/Cache/Context/PagersCacheContext.php index f53d464..dc6143b 100644 --- a/core/lib/Drupal/Core/Cache/Context/PagersCacheContext.php +++ b/core/lib/Drupal/Core/Cache/Context/PagersCacheContext.php @@ -7,6 +7,8 @@ namespace Drupal\Core\Cache\Context; +use Drupal\Core\Cache\CacheableMetadata; + /** * Defines a cache context for "per page in a pager" caching. * @@ -38,4 +40,11 @@ public function getContext($pager_id = NULL) { return 'pager.' . $pager_id . '.' . pager_find_page($pager_id); } + /** + * {@inheritdoc} + */ + public function getCacheableMetadata($pager_id = NULL) { + return new CacheableMetadata(); + } + } diff --git a/core/lib/Drupal/Core/Cache/Context/QueryArgsCacheContext.php b/core/lib/Drupal/Core/Cache/Context/QueryArgsCacheContext.php index faceb07..cab1da8 100644 --- a/core/lib/Drupal/Core/Cache/Context/QueryArgsCacheContext.php +++ b/core/lib/Drupal/Core/Cache/Context/QueryArgsCacheContext.php @@ -7,6 +7,8 @@ namespace Drupal\Core\Cache\Context; +use Drupal\Core\Cache\CacheableMetadata; + /** * Defines the QueryArgsCacheContext service, for "per query args" caching. * @@ -35,4 +37,11 @@ public function getContext($query_arg = NULL) { } } + /** + * {@inheritdoc} + */ + public function getCacheableMetadata($query_arg = NULL) { + return new CacheableMetadata(); + } + } diff --git a/core/lib/Drupal/Core/Cache/Context/RequestFormatCacheContext.php b/core/lib/Drupal/Core/Cache/Context/RequestFormatCacheContext.php index b6784f7..e8cb154 100644 --- a/core/lib/Drupal/Core/Cache/Context/RequestFormatCacheContext.php +++ b/core/lib/Drupal/Core/Cache/Context/RequestFormatCacheContext.php @@ -7,6 +7,8 @@ namespace Drupal\Core\Cache\Context; +use Drupal\Core\Cache\CacheableMetadata; + /** * Defines the RequestFormatCacheContext service, for "per format" caching. * @@ -28,4 +30,11 @@ public function getContext() { return $this->requestStack->getCurrentRequest()->getRequestFormat(); } + /** + * {@inheritdoc} + */ + public function getCacheableMetadata() { + return new CacheableMetadata(); + } + } diff --git a/core/lib/Drupal/Core/Cache/Context/RequestStackCacheContextBase.php b/core/lib/Drupal/Core/Cache/Context/RequestStackCacheContextBase.php index f4c960f..d6adff2 100644 --- a/core/lib/Drupal/Core/Cache/Context/RequestStackCacheContextBase.php +++ b/core/lib/Drupal/Core/Cache/Context/RequestStackCacheContextBase.php @@ -7,13 +7,12 @@ namespace Drupal\Core\Cache\Context; -use Drupal\Core\Cache\CacheableMetadata; use Symfony\Component\HttpFoundation\RequestStack; /** * Defines a base class for cache contexts depending only on the request stack. */ -abstract class RequestStackCacheContextBase implements CacheContextInterface { +abstract class RequestStackCacheContextBase { /** * The request stack. @@ -32,11 +31,4 @@ public function __construct(RequestStack $request_stack) { $this->requestStack = $request_stack; } - /** - * {@inheritdoc} - */ - public function getCacheableMetadata() { - return new CacheableMetadata(); - } - } diff --git a/core/lib/Drupal/Core/Cache/Context/SiteCacheContext.php b/core/lib/Drupal/Core/Cache/Context/SiteCacheContext.php index e4d28cb..539ba9d 100644 --- a/core/lib/Drupal/Core/Cache/Context/SiteCacheContext.php +++ b/core/lib/Drupal/Core/Cache/Context/SiteCacheContext.php @@ -7,6 +7,8 @@ namespace Drupal\Core\Cache\Context; +use Drupal\Core\Cache\CacheableMetadata; + /** * Defines the SiteCacheContext service, for "per site" caching. * @@ -20,7 +22,7 @@ * @see \Symfony\Component\HttpFoundation\Request::getSchemeAndHttpHost() * @see \Symfony\Component\HttpFoundation\Request::getBaseUrl() */ -class SiteCacheContext extends RequestStackCacheContextBase { +class SiteCacheContext extends RequestStackCacheContextBase implements CacheContextInterface { /** * {@inheritdoc} @@ -37,4 +39,11 @@ public function getContext() { return $request->getSchemeAndHttpHost() . $request->getBaseUrl(); } + /** + * {@inheritdoc} + */ + public function getCacheableMetadata() { + return new CacheableMetadata(); + } + } diff --git a/core/lib/Drupal/Core/Cache/Context/UrlCacheContext.php b/core/lib/Drupal/Core/Cache/Context/UrlCacheContext.php index c0b0453..f240128 100644 --- a/core/lib/Drupal/Core/Cache/Context/UrlCacheContext.php +++ b/core/lib/Drupal/Core/Cache/Context/UrlCacheContext.php @@ -7,12 +7,14 @@ namespace Drupal\Core\Cache\Context; +use Drupal\Core\Cache\CacheableMetadata; + /** * Defines the UrlCacheContext service, for "per page" caching. * * Cache context ID: 'url'. */ -class UrlCacheContext extends RequestStackCacheContextBase { +class UrlCacheContext extends RequestStackCacheContextBase implements CacheContextInterface { /** * {@inheritdoc} @@ -28,4 +30,11 @@ public function getContext() { return $this->requestStack->getCurrentRequest()->getUri(); } + /** + * {@inheritdoc} + */ + public function getCacheableMetadata() { + return new CacheableMetadata(); + } + } diff --git a/core/lib/Drupal/Core/Cache/Context/UserCacheContext.php b/core/lib/Drupal/Core/Cache/Context/UserCacheContext.php index 983f8a3..1a0357a 100644 --- a/core/lib/Drupal/Core/Cache/Context/UserCacheContext.php +++ b/core/lib/Drupal/Core/Cache/Context/UserCacheContext.php @@ -8,24 +8,13 @@ namespace Drupal\Core\Cache\Context; use Drupal\Core\Cache\CacheableMetadata; -use Drupal\Core\Session\AccountInterface; /** * Defines the UserCacheContext service, for "per user" caching. * * Cache context ID: 'user'. */ -class UserCacheContext implements CacheContextInterface { - - /** - * Constructs a new UserCacheContext service. - * - * @param \Drupal\Core\Session\AccountInterface $user - * The current user. - */ - public function __construct(AccountInterface $user) { - $this->user = $user; - } +class UserCacheContext extends UserCacheContextBase implements CacheContextInterface { /** * {@inheritdoc} diff --git a/core/lib/Drupal/Core/Cache/Context/UserCacheContextBase.php b/core/lib/Drupal/Core/Cache/Context/UserCacheContextBase.php new file mode 100644 index 0000000..4cad8da --- /dev/null +++ b/core/lib/Drupal/Core/Cache/Context/UserCacheContextBase.php @@ -0,0 +1,34 @@ +user = $user; + } + +} diff --git a/core/lib/Drupal/Core/Cache/Context/UserRolesCacheContext.php b/core/lib/Drupal/Core/Cache/Context/UserRolesCacheContext.php index 4221f14..1181634 100644 --- a/core/lib/Drupal/Core/Cache/Context/UserRolesCacheContext.php +++ b/core/lib/Drupal/Core/Cache/Context/UserRolesCacheContext.php @@ -7,6 +7,8 @@ namespace Drupal\Core\Cache\Context; +use Drupal\Core\Cache\CacheableMetadata; + /** * Defines the UserRolesCacheContext service, for "per role" caching. * @@ -17,7 +19,7 @@ * Calculated cache context ID: 'user.roles:%role', e.g. 'user.roles:anonymous' * (to vary by the presence/absence of a specific role). */ -class UserRolesCacheContext extends UserCacheContext implements CalculatedCacheContextInterface { +class UserRolesCacheContext extends UserCacheContextBase implements CalculatedCacheContextInterface { /** * {@inheritdoc} @@ -45,4 +47,11 @@ public function getContext($role = NULL) { } } + /** + * {@inheritdoc} + */ + public function getCacheableMetadata($role = NULL) { + return new CacheableMetadata(); + } + } diff --git a/core/modules/node/src/Cache/NodeAccessGrantsCacheContext.php b/core/modules/node/src/Cache/NodeAccessGrantsCacheContext.php index 10c09f4..d304bf7 100644 --- a/core/modules/node/src/Cache/NodeAccessGrantsCacheContext.php +++ b/core/modules/node/src/Cache/NodeAccessGrantsCacheContext.php @@ -9,7 +9,7 @@ use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Cache\Context\CalculatedCacheContextInterface; -use Drupal\Core\Cache\Context\UserCacheContext; +use Drupal\Core\Cache\Context\UserCacheContextBase; /** * Defines the node access view cache context service. @@ -23,7 +23,7 @@ * @see node_query_node_access_alter() * @ingroup node_access */ -class NodeAccessGrantsCacheContext extends UserCacheContext implements CalculatedCacheContextInterface { +class NodeAccessGrantsCacheContext extends UserCacheContextBase implements CalculatedCacheContextInterface { /** * {@inheritdoc}