Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. CacheContextsPass.php

function CacheContextsPass::process

Implements CompilerPassInterface::process().

Collects the cache contexts into the cache_contexts parameter.

Overrides CompilerPassInterface::process

File

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

Class

CacheContextsPass
Adds cache_contexts parameter to the container.

Namespace

Drupal\Core\Cache\Context

Code

public function process(ContainerBuilder $container) : void {
    $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);
}
RSS feed
Powered by Drupal