Version 1
[yaffs-website] / web / core / modules / system / tests / modules / form_test / src / EventSubscriber / FormTestEventSubscriber.php
1 <?php
2
3 namespace Drupal\form_test\EventSubscriber;
4
5 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
6 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
7 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
8 use Symfony\Component\HttpKernel\KernelEvents;
9
10 /**
11  * Test event subscriber to add new attributes to the request.
12  */
13 class FormTestEventSubscriber implements EventSubscriberInterface {
14
15   /**
16    * Adds custom attributes to the request object.
17    *
18    * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
19    *   The kernel request event.
20    */
21   public function onKernelRequest(GetResponseEvent $event) {
22     $request = $event->getRequest();
23     $request->attributes->set('custom_attributes', 'custom_value');
24     $request->attributes->set('request_attribute', 'request_value');
25   }
26
27   /**
28    * Adds custom headers to the response.
29    *
30    * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
31    *   The kernel request event.
32    */
33   public function onKernelResponse(FilterResponseEvent $event) {
34     $response = $event->getResponse();
35     $response->headers->set('X-Form-Test-Response-Event', 'invoked');
36   }
37
38   /**
39    * {@inheritdoc}
40    */
41   public static function getSubscribedEvents() {
42     $events[KernelEvents::REQUEST][] = ['onKernelRequest'];
43     $events[KernelEvents::RESPONSE][] = ['onKernelResponse'];
44     return $events;
45   }
46
47 }