Version 1
[yaffs-website] / web / core / modules / system / tests / modules / form_test / src / EventSubscriber / FormTestEventSubscriber.php
diff --git a/web/core/modules/system/tests/modules/form_test/src/EventSubscriber/FormTestEventSubscriber.php b/web/core/modules/system/tests/modules/form_test/src/EventSubscriber/FormTestEventSubscriber.php
new file mode 100644 (file)
index 0000000..3fb9750
--- /dev/null
@@ -0,0 +1,47 @@
+<?php
+
+namespace Drupal\form_test\EventSubscriber;
+
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
+use Symfony\Component\HttpKernel\Event\GetResponseEvent;
+use Symfony\Component\HttpKernel\KernelEvents;
+
+/**
+ * Test event subscriber to add new attributes to the request.
+ */
+class FormTestEventSubscriber implements EventSubscriberInterface {
+
+  /**
+   * Adds custom attributes to the request object.
+   *
+   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
+   *   The kernel request event.
+   */
+  public function onKernelRequest(GetResponseEvent $event) {
+    $request = $event->getRequest();
+    $request->attributes->set('custom_attributes', 'custom_value');
+    $request->attributes->set('request_attribute', 'request_value');
+  }
+
+  /**
+   * Adds custom headers to the response.
+   *
+   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
+   *   The kernel request event.
+   */
+  public function onKernelResponse(FilterResponseEvent $event) {
+    $response = $event->getResponse();
+    $response->headers->set('X-Form-Test-Response-Event', 'invoked');
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function getSubscribedEvents() {
+    $events[KernelEvents::REQUEST][] = ['onKernelRequest'];
+    $events[KernelEvents::RESPONSE][] = ['onKernelResponse'];
+    return $events;
+  }
+
+}