3 namespace Drupal\Core\Cache\Context;
5 use Symfony\Component\DependencyInjection\ContainerBuilder;
6 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
9 * Adds cache_contexts parameter to the container.
11 class CacheContextsPass implements CompilerPassInterface {
14 * Implements CompilerPassInterface::process().
16 * Collects the cache contexts into the cache_contexts parameter.
18 public function process(ContainerBuilder $container) {
20 foreach (array_keys($container->findTaggedServiceIds('cache.context')) as $id) {
21 if (strpos($id, 'cache_context.') !== 0) {
22 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));
24 $cache_contexts[] = substr($id, 14);
28 sort($cache_contexts);
29 foreach ($cache_contexts as $id) {
30 // Validate the hierarchy of non-root-level cache contexts.
31 if (strpos($id, '.') !== FALSE) {
32 $parent = substr($id, 0, strrpos($id, '.'));
33 if (!in_array($parent, $cache_contexts)) {
34 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));
40 $container->setParameter('cache_contexts', $cache_contexts);