summaryrefslogtreecommitdiffstats
path: root/core/modules/system/tests/modules/service_provider_test/src/TestClass.php
blob: d521e1fdb99a98801560774cfe83d466d3bd8e62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php

namespace Drupal\service_provider_test;

use Drupal\Core\State\StateInterface;
use Drupal\Core\DestructableInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class TestClass implements EventSubscriberInterface, DestructableInterface, ContainerAwareInterface {

  use ContainerAwareTrait;

  /**
   * The state keyvalue collection.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * Constructor.
   *
   * @param \Drupal\Core\State\StateInterface $state
   *   The state key value store.
   */
  public function __construct(StateInterface $state) {
    $this->state = $state;
  }

  /**
   * A simple kernel listener method.
   */
  public function onKernelRequestTest(GetResponseEvent $event) {
    drupal_set_message(t('The service_provider_test event subscriber fired!'));
  }

  /**
   * Flags the response in case a rebuild indicator is used.
   */
  public function onKernelResponseTest(FilterResponseEvent $event) {
    if ($this->container->hasParameter('container_rebuild_indicator')) {
      $event->getResponse()->headers->set('container_rebuild_indicator', $this->container->getParameter('container_rebuild_indicator'));
    }
    if ($this->container->hasParameter('container_rebuild_test_parameter')) {
      $event->getResponse()->headers->set('container_rebuild_test_parameter', $this->container->getParameter('container_rebuild_test_parameter'));
    }
  }

  /**
   * Registers methods as kernel listeners.
   *
   * @return array
   *   An array of event listener definitions.
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = ['onKernelRequestTest'];
    $events[KernelEvents::RESPONSE][] = ['onKernelResponseTest'];
    return $events;
  }

  /**
   * {@inheritdoc}
   */
  public function destruct() {
    $this->state->set('service_provider_test.destructed', TRUE);
  }

}