W3cubDocs

/Drupal 8

public function CacheContextsManager::convertTokensToKeys

public CacheContextsManager::convertTokensToKeys(array $context_tokens)

Converts cache context tokens to cache keys.

A cache context token is either:

  • a cache context ID (if the service ID is 'cache_context.foo', then 'foo' is a cache context ID); for example, 'foo'.
  • a calculated cache context ID, followed by a colon, followed by the parameter for the calculated cache context; for example, 'bar:some_parameter'.

Parameters

string[] $context_tokens: An array of cache context tokens.

Return value

\Drupal\Core\Cache\Context\ContextCacheKeys The ContextCacheKeys object containing the converted cache keys and cacheability metadata.

File

core/lib/Drupal/Core/Cache/Context/CacheContextsManager.php, line 102

Class

CacheContextsManager
Converts cache context tokens into cache keys.

Namespace

Drupal\Core\Cache\Context

Code

public function convertTokensToKeys(array $context_tokens) {
  assert('$this->assertValidTokens($context_tokens)');
  $cacheable_metadata = new CacheableMetadata();
  $optimized_tokens = $this->optimizeTokens($context_tokens);
  // Iterate over cache contexts that have been optimized away and get their
  // cacheability metadata.
  foreach (static::parseTokens(array_diff($context_tokens, $optimized_tokens)) as $context_token) {
    list($context_id, $parameter) = $context_token;
    $context = $this->getService($context_id);
    $cacheable_metadata = $cacheable_metadata->merge($context->getCacheableMetadata($parameter));
  }

  sort($optimized_tokens);
  $keys = [];
  foreach (array_combine($optimized_tokens, static::parseTokens($optimized_tokens)) as $context_token => $context) {
    list($context_id, $parameter) = $context;
    $keys[] = '[' . $context_token . ']=' . $this->getService($context_id)->getContext($parameter);
  }

  // Create the returned object and merge in the cacheability metadata.
  $context_cache_keys = new ContextCacheKeys($keys);
  return $context_cache_keys->merge($cacheable_metadata);
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Cache!Context!CacheContextsManager.php/function/CacheContextsManager::convertTokensToKeys/8.1.x