bf49e540ed8166341cd465448d15218f65d27b45
[yaffs-website] / web / core / lib / Drupal / Core / Cache / Context / CacheContextsPass.php
1 <?php
2
3 namespace Drupal\Core\Cache\Context;
4
5 use Symfony\Component\DependencyInjection\ContainerBuilder;
6 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
8 /**
9  * Adds cache_contexts parameter to the container.
10  */
11 class CacheContextsPass implements CompilerPassInterface {
12
13   /**
14    * Implements CompilerPassInterface::process().
15    *
16    * Collects the cache contexts into the cache_contexts parameter.
17    */
18   public function process(ContainerBuilder $container) {
19     $cache_contexts = [];
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));
23       }
24       $cache_contexts[] = substr($id, 14);
25     }
26
27     // Validate.
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));
35         }
36       }
37     }
38
39
40     $container->setParameter('cache_contexts', $cache_contexts);
41   }
42
43 }