c4dad85955c8d28f94670d3a393dfc2851bb3566
[yaffs-website] / web / core / modules / system / tests / modules / service_provider_test / src / TestClass.php
1 <?php
2
3 namespace Drupal\service_provider_test;
4
5 use Drupal\Core\State\StateInterface;
6 use Drupal\Core\DestructableInterface;
7 use Symfony\Component\DependencyInjection\ContainerAwareInterface;
8 use Symfony\Component\DependencyInjection\ContainerAwareTrait;
9 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
11 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
12 use Symfony\Component\HttpKernel\KernelEvents;
13
14 class TestClass implements EventSubscriberInterface, DestructableInterface, ContainerAwareInterface {
15
16   use ContainerAwareTrait;
17
18   /**
19    * The state keyvalue collection.
20    *
21    * @var \Drupal\Core\State\StateInterface
22    */
23   protected $state;
24
25   /**
26    * Constructor.
27    *
28    * @param \Drupal\Core\State\StateInterface $state
29    *   The state key value store.
30    */
31   public function __construct(StateInterface $state) {
32     $this->state = $state;
33   }
34
35   /**
36    * A simple kernel listener method.
37    */
38   public function onKernelRequestTest(GetResponseEvent $event) {
39     \Drupal::messenger()->addStatus(t('The service_provider_test event subscriber fired!'));
40   }
41
42   /**
43    * Flags the response in case a rebuild indicator is used.
44    */
45   public function onKernelResponseTest(FilterResponseEvent $event) {
46     if ($this->container->hasParameter('container_rebuild_indicator')) {
47       $event->getResponse()->headers->set('container_rebuild_indicator', $this->container->getParameter('container_rebuild_indicator'));
48     }
49     if ($this->container->hasParameter('container_rebuild_test_parameter')) {
50       $event->getResponse()->headers->set('container_rebuild_test_parameter', $this->container->getParameter('container_rebuild_test_parameter'));
51     }
52   }
53
54   /**
55    * Registers methods as kernel listeners.
56    *
57    * @return array
58    *   An array of event listener definitions.
59    */
60   public static function getSubscribedEvents() {
61     $events[KernelEvents::REQUEST][] = ['onKernelRequestTest'];
62     $events[KernelEvents::RESPONSE][] = ['onKernelResponseTest'];
63     return $events;
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function destruct() {
70     $this->state->set('service_provider_test.destructed', TRUE);
71   }
72
73 }