Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / system / src / EventSubscriber / ConfigCacheTag.php
1 <?php
2
3 namespace Drupal\system\EventSubscriber;
4
5 use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
6 use Drupal\Core\Config\ConfigCrudEvent;
7 use Drupal\Core\Config\ConfigEvents;
8 use Drupal\Core\Extension\ThemeHandlerInterface;
9 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10
11 /**
12  * A subscriber invalidating cache tags when system config objects are saved.
13  */
14 class ConfigCacheTag implements EventSubscriberInterface {
15
16   /**
17    * The theme handler.
18    *
19    * @var \Drupal\Core\Extension\ThemeHandlerInterface
20    */
21   protected $themeHandler;
22
23   /**
24    * The cache tags invalidator.
25    *
26    * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
27    */
28   protected $cacheTagsInvalidator;
29
30   /**
31    * Constructs a ConfigCacheTag object.
32    *
33    * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
34    *   The theme handler.
35    * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_invalidator
36    *   The cache tags invalidator.
37    */
38   public function __construct(ThemeHandlerInterface $theme_handler, CacheTagsInvalidatorInterface $cache_tags_invalidator) {
39     $this->themeHandler = $theme_handler;
40     $this->cacheTagsInvalidator = $cache_tags_invalidator;
41   }
42
43   /**
44    * Invalidate cache tags when particular system config objects are saved.
45    *
46    * @param \Drupal\Core\Config\ConfigCrudEvent $event
47    *   The Event to process.
48    */
49   public function onSave(ConfigCrudEvent $event) {
50     // Changing the site settings may mean a different route is selected for the
51     // front page. Additionally a change to the site name or similar must
52     // invalidate the render cache since this could be used anywhere.
53     if ($event->getConfig()->getName() === 'system.site') {
54       $this->cacheTagsInvalidator->invalidateTags(['route_match', 'rendered']);
55     }
56
57     // Theme configuration and global theme settings.
58     if (in_array($event->getConfig()->getName(), ['system.theme', 'system.theme.global'], TRUE)) {
59       $this->cacheTagsInvalidator->invalidateTags(['rendered']);
60     }
61
62     // Theme-specific settings, check if this matches a theme settings
63     // configuration object, in that case, clear the rendered cache tag.
64     foreach (array_keys($this->themeHandler->listInfo()) as $theme_name) {
65       if ($theme_name == $event->getConfig()->getName()) {
66         $this->cacheTagsInvalidator->invalidateTags(['rendered']);
67         break;
68       }
69     }
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public static function getSubscribedEvents() {
76     $events[ConfigEvents::SAVE][] = ['onSave'];
77     return $events;
78   }
79
80 }