class CacheContextsPass

Same name in other branches
  1. 9 core/lib/Drupal/Core/Cache/Context/CacheContextsPass.php \Drupal\Core\Cache\Context\CacheContextsPass
  2. 8.9.x core/lib/Drupal/Core/Cache/Context/CacheContextsPass.php \Drupal\Core\Cache\Context\CacheContextsPass
  3. 11.x core/lib/Drupal/Core/Cache/Context/CacheContextsPass.php \Drupal\Core\Cache\Context\CacheContextsPass

Adds cache_contexts parameter to the container.

Hierarchy

  • class \Drupal\Core\Cache\Context\CacheContextsPass implements \Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface

Expanded class hierarchy of CacheContextsPass

1 file declares its use of CacheContextsPass
CoreServiceProvider.php in core/lib/Drupal/Core/CoreServiceProvider.php

File

core/lib/Drupal/Core/Cache/Context/CacheContextsPass.php, line 11

Namespace

Drupal\Core\Cache\Context
View source
class CacheContextsPass implements CompilerPassInterface {
    
    /**
     * Implements CompilerPassInterface::process().
     *
     * Collects the cache contexts into the cache_contexts parameter.
     */
    public function process(ContainerBuilder $container) {
        $cache_contexts = [];
        foreach (array_keys($container->findTaggedServiceIds('cache.context')) as $id) {
            if (!str_starts_with($id, 'cache_context.')) {
                throw new \InvalidArgumentException(sprintf('The service "%s" has an invalid service ID: cache context service IDs must use the "cache_context." prefix. (The suffix is the cache context ID developers may use.)', $id));
            }
            $cache_contexts[] = substr($id, 14);
        }
        // Validate.
        sort($cache_contexts);
        foreach ($cache_contexts as $id) {
            // Validate the hierarchy of non-root-level cache contexts.
            if (str_contains($id, '.')) {
                $parent = substr($id, 0, strrpos($id, '.'));
                if (!in_array($parent, $cache_contexts)) {
                    throw new \InvalidArgumentException(sprintf('The service "%s" has an invalid service ID: the period indicates the hierarchy of cache contexts, therefore "%s" is considered the parent cache context, but no cache context service with that name was found.', $id, $parent));
                }
            }
        }
        $container->setParameter('cache_contexts', $cache_contexts);
    }

}

Members

Title Sort descending Modifiers Object type Summary
CacheContextsPass::process public function Implements CompilerPassInterface::process().

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.