CacheTagsInvalidator.php 2.1 KB

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