Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / lib / Drupal / Core / Cache / CacheTagsInvalidator.php
1 <?php
2
3 namespace Drupal\Core\Cache;
4
5 use Drupal\Component\Assertion\Inspector;
6 use Symfony\Component\DependencyInjection\ContainerAwareTrait;
7
8 /**
9  * Passes cache tag events to classes that wish to respond to them.
10  */
11 class CacheTagsInvalidator implements CacheTagsInvalidatorInterface {
12
13   use ContainerAwareTrait;
14
15   /**
16    * Holds an array of cache tags invalidators.
17    *
18    * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface[]
19    */
20   protected $invalidators = [];
21
22   /**
23    * {@inheritdoc}
24    */
25   public function invalidateTags(array $tags) {
26     assert(Inspector::assertAllStrings($tags), 'Cache tags must be strings.');
27
28     // Notify all added cache tags invalidators.
29     foreach ($this->invalidators as $invalidator) {
30       $invalidator->invalidateTags($tags);
31     }
32
33     // Additionally, notify each cache bin if it implements the service.
34     foreach ($this->getInvalidatorCacheBins() as $bin) {
35       $bin->invalidateTags($tags);
36     }
37   }
38
39   /**
40    * Reset statically cached tags in all cache tag checksum services.
41    *
42    * This is only used by tests.
43    */
44   public function resetChecksums() {
45     foreach ($this->invalidators as $invalidator) {
46       if ($invalidator instanceof CacheTagsChecksumInterface) {
47         $invalidator->reset();
48       }
49     }
50   }
51
52   /**
53    * Adds a cache tags invalidator.
54    *
55    * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $invalidator
56    *   A cache invalidator.
57    */
58   public function addInvalidator(CacheTagsInvalidatorInterface $invalidator) {
59     $this->invalidators[] = $invalidator;
60   }
61
62   /**
63    * Returns all cache bins that need to be notified about invalidations.
64    *
65    * @return \Drupal\Core\Cache\CacheTagsInvalidatorInterface[]
66    *   An array of cache backend objects that implement the invalidator
67    *   interface, keyed by their cache bin.
68    */
69   protected function getInvalidatorCacheBins() {
70     $bins = [];
71     foreach ($this->container->getParameter('cache_bins') as $service_id => $bin) {
72       $service = $this->container->get($service_id);
73       if ($service instanceof CacheTagsInvalidatorInterface) {
74         $bins[$bin] = $service;
75       }
76     }
77     return $bins;
78   }
79
80 }