3 namespace Drupal\service_provider_test;
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;
14 class TestClass implements EventSubscriberInterface, DestructableInterface, ContainerAwareInterface {
16 use ContainerAwareTrait;
19 * The state keyvalue collection.
21 * @var \Drupal\Core\State\StateInterface
28 * @param \Drupal\Core\State\StateInterface $state
29 * The state key value store.
31 public function __construct(StateInterface $state) {
32 $this->state = $state;
36 * A simple kernel listener method.
38 public function onKernelRequestTest(GetResponseEvent $event) {
39 \Drupal::messenger()->addStatus(t('The service_provider_test event subscriber fired!'));
43 * Flags the response in case a rebuild indicator is used.
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'));
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'));
55 * Registers methods as kernel listeners.
58 * An array of event listener definitions.
60 public static function getSubscribedEvents() {
61 $events[KernelEvents::REQUEST][] = ['onKernelRequestTest'];
62 $events[KernelEvents::RESPONSE][] = ['onKernelResponseTest'];
69 public function destruct() {
70 $this->state->set('service_provider_test.destructed', TRUE);