Version 1
[yaffs-website] / web / core / modules / system / tests / modules / early_rendering_controller_test / src / TestDomainObjectViewSubscriber.php
1 <?php
2
3 namespace Drupal\early_rendering_controller_test;
4
5 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
6 use Symfony\Component\HttpFoundation\Response;
7 use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
8 use Symfony\Component\HttpKernel\KernelEvents;
9
10 /**
11  * View subscriber for turning TestDomainObject objects into Response objects.
12  */
13 class TestDomainObjectViewSubscriber implements EventSubscriberInterface {
14
15   /**
16    * Sets a response given a TestDomainObject instance.
17    *
18    * @param \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event
19    *   The event to process.
20    */
21   public function onViewTestDomainObject(GetResponseForControllerResultEvent $event) {
22     $result = $event->getControllerResult();
23
24     if ($result instanceof TestDomainObject) {
25       if ($result instanceof AttachmentsTestDomainObject) {
26         $event->setResponse(new AttachmentsTestResponse('AttachmentsTestDomainObject'));
27       }
28       elseif ($result instanceof CacheableTestDomainObject) {
29         $event->setResponse(new CacheableTestResponse('CacheableTestDomainObject'));
30       }
31       else {
32         $event->setResponse(new Response('TestDomainObject'));
33       }
34     }
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public static function getSubscribedEvents() {
41     $events[KernelEvents::VIEW][] = ['onViewTestDomainObject'];
42
43     return $events;
44   }
45
46 }