diff --git a/core/lib/Drupal/Core/Cache/Context/CacheContextInterface.php b/core/lib/Drupal/Core/Cache/Context/CacheContextInterface.php index f7ce3ab..ce6947f 100644 --- a/core/lib/Drupal/Core/Cache/Context/CacheContextInterface.php +++ b/core/lib/Drupal/Core/Cache/Context/CacheContextInterface.php @@ -34,11 +34,14 @@ public function getContext(); /** * Gets the cacheability metadata for the context. * - * If the cache context is being optimized away, cache tags and max-age - * provided by this method will be bubbled up into the cache item. + * There are three valid cases for the returned CacheableMetadata object: + * - An empty object means this can be optimized away safely. + * - A max-age of 0 means that this context can never be optimized away. It + * will never bubble up and cache tags will not be used. + * - Any non-zero max-age and cache tags will bubble up into the cache item + * if this is optimized away to allow for invalidation if the context + * value changes. * - * If a max-age of 0 is returned then it means that this context can not - * be optimized away. * * @return \Drupal\Core\Cache\CacheableMetadata * A cacheable metadata object. diff --git a/core/lib/Drupal/Core/Cache/Context/CacheContextsManager.php b/core/lib/Drupal/Core/Cache/Context/CacheContextsManager.php index a0b3b4e..4bf3459 100644 --- a/core/lib/Drupal/Core/Cache/Context/CacheContextsManager.php +++ b/core/lib/Drupal/Core/Cache/Context/CacheContextsManager.php @@ -7,7 +7,6 @@ namespace Drupal\Core\Cache\Context; -use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\Cache\CacheableMetadata; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -104,7 +103,8 @@ public function getLabels($include_calculated_cache_contexts = FALSE) { * The ContextCacheKeys object containing the converted cache keys and * cacheability metadata. * - * @throws \InvalidArgumentException + * @throws \LogicException + * Thrown if any of the context tokens or parameters are not valid. */ public function convertTokensToKeys(array $context_tokens) { $this->validateTokens($context_tokens); diff --git a/core/lib/Drupal/Core/Cache/Context/CalculatedCacheContextInterface.php b/core/lib/Drupal/Core/Cache/Context/CalculatedCacheContextInterface.php index 5d38935..0647fad 100644 --- a/core/lib/Drupal/Core/Cache/Context/CalculatedCacheContextInterface.php +++ b/core/lib/Drupal/Core/Cache/Context/CalculatedCacheContextInterface.php @@ -34,23 +34,31 @@ public static function getLabel(); * @return string * The string representation of the cache context. When $parameter is NULL, * a value representing all possible parameters must be generated. + * + * @throws \LogicException + * Thrown if the passed in parameter is invalid. */ public function getContext($parameter = NULL); /** * Gets the cacheability metadata for the context based on the parameter value. * - * If the cache context is being optimized away, cache tags and max-age - * provided by this method will be bubbled up into the cache item. - * - * If a max-age of 0 is returned then it means that this context can not - * be optimized away. + * There are three valid cases for the returned CacheableMetadata object: + * - An empty object means this can be optimized away safely. + * - A max-age of 0 means that this context can never be optimized away. It + * will never bubble up and cache tags will not be used. + * - Any non-zero max-age and cache tags will bubble up into the cache item + * if this is optimized away to allow for invalidation if the context + * value changes. * * @param string|null $parameter * The parameter, or NULL to indicate all possible parameter values. * * @return \Drupal\Core\Cache\CacheableMetadata * A cacheable metadata object. + * + * @throws \LogicException + * Thrown if the passed in parameter is invalid. */ public function getCacheableMetadata($parameter = NULL); diff --git a/core/lib/Drupal/Core/Cache/Context/MenuActiveTrailsCacheContext.php b/core/lib/Drupal/Core/Cache/Context/MenuActiveTrailsCacheContext.php index 2d2a4d6..3ff2dc4 100644 --- a/core/lib/Drupal/Core/Cache/Context/MenuActiveTrailsCacheContext.php +++ b/core/lib/Drupal/Core/Cache/Context/MenuActiveTrailsCacheContext.php @@ -30,7 +30,7 @@ public static function getLabel() { */ public function getContext($menu_name = NULL) { if (!$menu_name) { - throw new \InvalidArgumentException('No menu name provided for menu.active_trails cache context.'); + throw new \LogicException('No menu name provided for menu.active_trails cache context.'); } $active_trail = $this->container->get('menu.active_trail') @@ -43,7 +43,7 @@ public function getContext($menu_name = NULL) { */ public function getCacheableMetadata($menu_name = NULL) { if (!$menu_name) { - throw new \InvalidArgumentException('No menu name provided for menu.active_trails cache context.'); + throw new \LogicException('No menu name provided for menu.active_trails cache context.'); } $cacheable_metadata = new CacheableMetadata(); return $cacheable_metadata->setCacheTags(["config:system.menu.$menu_name"]); diff --git a/core/lib/Drupal/Core/Cache/Context/RequestStackCacheContextBase.php b/core/lib/Drupal/Core/Cache/Context/RequestStackCacheContextBase.php index d6adff2..d6ffd13 100644 --- a/core/lib/Drupal/Core/Cache/Context/RequestStackCacheContextBase.php +++ b/core/lib/Drupal/Core/Cache/Context/RequestStackCacheContextBase.php @@ -11,6 +11,10 @@ /** * Defines a base class for cache contexts depending only on the request stack. + * + * Subclasses need to implement either + * \Drupal\Core\Cache\Context\CacheContextInterface or + * \Drupal\Core\Cache\Context\CalculatedCacheContextInterface themself. */ abstract class RequestStackCacheContextBase { diff --git a/core/lib/Drupal/Core/Cache/Context/UserCacheContextBase.php b/core/lib/Drupal/Core/Cache/Context/UserCacheContextBase.php index 0113295..602fde6 100644 --- a/core/lib/Drupal/Core/Cache/Context/UserCacheContextBase.php +++ b/core/lib/Drupal/Core/Cache/Context/UserCacheContextBase.php @@ -11,8 +11,12 @@ /** * Base class for user-based cache contexts. + * + * Subclasses need to implement either + * \Drupal\Core\Cache\Context\CacheContextInterface or + * \Drupal\Core\Cache\Context\CalculatedCacheContextInterface themself. */ -abstract class UserCacheContextBase implements CacheContextInterface { +abstract class UserCacheContextBase { /** * The account object. diff --git a/core/modules/book/src/Cache/BookNavigationCacheContext.php b/core/modules/book/src/Cache/BookNavigationCacheContext.php index 97a405e..9dbe1db 100644 --- a/core/modules/book/src/Cache/BookNavigationCacheContext.php +++ b/core/modules/book/src/Cache/BookNavigationCacheContext.php @@ -79,8 +79,8 @@ public function getCacheableMetadata() { // That information is however not stored as part of the node. $cacheable_metadata = new CacheableMetadata(); if ($node = $this->requestStack->getCurrentRequest()->get('node')) { - // If the node is part of a book that we can use the cache tag for that - // book. If not, then we can't be optimized away. + // If the node is part of a book then we can use the cache tag for that + // book. If not, then it can't be optimized away. if (!empty($node->book['bid'])) { $cacheable_metadata->addCacheTags(['bid:' . $node->book['bid']]); }