Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 18
CacheContextsPass
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
42
0.00% covered (danger)
0.00%
0 / 18
 process
0.00% covered (danger)
0.00%
0 / 1
42
0.00% covered (danger)
0.00%
0 / 18
<?php
/**
 * @file
 * Contains \Drupal\Core\Cache\Context\CacheContextsPass.
 */
namespace Drupal\Core\Cache\Context;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
/**
 * Adds cache_contexts parameter to the container.
 */
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 (strpos($id, 'cache_context.') !== 0) {
        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 (strpos($id, '.') !== FALSE) {
        $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);
  }
}