Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / system / tests / modules / entity_test / src / EntityTestDefinitionSubscriber.php
1 <?php
2
3 namespace Drupal\entity_test;
4
5 use Drupal\Core\Entity\EntityTypeEvents;
6 use Drupal\Core\Entity\EntityTypeEventSubscriberTrait;
7 use Drupal\Core\Entity\EntityTypeInterface;
8 use Drupal\Core\Entity\EntityTypeListenerInterface;
9 use Drupal\Core\Field\FieldStorageDefinitionEvents;
10 use Drupal\Core\Field\FieldStorageDefinitionEventSubscriberTrait;
11 use Drupal\Core\Field\FieldStorageDefinitionInterface;
12 use Drupal\Core\Field\FieldStorageDefinitionListenerInterface;
13 use Drupal\Core\State\StateInterface;
14 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15
16 /**
17  * Test entity type and field storage definition event subscriber.
18  */
19 class EntityTestDefinitionSubscriber implements EventSubscriberInterface, EntityTypeListenerInterface, FieldStorageDefinitionListenerInterface {
20
21   use EntityTypeEventSubscriberTrait;
22   use FieldStorageDefinitionEventSubscriberTrait;
23
24   /**
25    * The state service.
26    *
27    * @var \Drupal\Core\State\StateInterface
28    */
29   protected $state;
30
31   /**
32    * Flag determining whether events should be tracked.
33    *
34    * @var bool
35    */
36   protected $trackEvents = FALSE;
37
38   /**
39    * {@inheritdoc}
40    */
41   public function __construct(StateInterface $state) {
42     $this->state = $state;
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public static function getSubscribedEvents() {
49     return static::getEntityTypeEvents() + static::getFieldStorageDefinitionEvents();
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public function onEntityTypeCreate(EntityTypeInterface $entity_type) {
56     $this->storeEvent(EntityTypeEvents::CREATE);
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
63     $this->storeEvent(EntityTypeEvents::UPDATE);
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function onEntityTypeDelete(EntityTypeInterface $entity_type) {
70     $this->storeEvent(EntityTypeEvents::DELETE);
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function onFieldStorageDefinitionCreate(FieldStorageDefinitionInterface $storage_definition) {
77     $this->storeEvent(FieldStorageDefinitionEvents::CREATE);
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   public function onFieldStorageDefinitionUpdate(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) {
84     $this->storeEvent(FieldStorageDefinitionEvents::UPDATE);
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public function onFieldStorageDefinitionDelete(FieldStorageDefinitionInterface $storage_definition) {
91     $this->storeEvent(FieldStorageDefinitionEvents::DELETE);
92   }
93
94   /**
95    * Enables event tracking.
96    */
97   public function enableEventTracking() {
98     $this->trackEvents = TRUE;
99   }
100
101   /**
102    * Checks whether an event has been dispatched.
103    *
104    * @param string $event_name
105    *   The event name.
106    *
107    * @return bool
108    *   TRUE if the event has been dispatched, FALSE otherwise.
109    */
110   public function hasEventFired($event_name) {
111     return (bool) $this->state->get($event_name);
112   }
113
114   /**
115    * Stores the specified event.
116    *
117    * @param string $event_name
118    *   The event name.
119    */
120   protected function storeEvent($event_name) {
121     if ($this->trackEvents) {
122       $this->state->set($event_name, TRUE);
123     }
124   }
125
126 }