Version 1
[yaffs-website] / web / core / lib / Drupal / Core / EventSubscriber / AcceptNegotiation406.php
1 <?php
2
3 namespace Drupal\Core\EventSubscriber;
4
5 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
6 use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
7 use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
8 use Symfony\Component\HttpKernel\KernelEvents;
9
10 /**
11  * View subscriber rendering a 406 if we could not route or render a request.
12  *
13  * @todo fix or replace this in https://www.drupal.org/node/2364011
14  */
15 class AcceptNegotiation406 implements EventSubscriberInterface {
16
17   /**
18    * Throws an HTTP 406 error if we get this far, which we normally shouldn't.
19    *
20    * @param \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event
21    *   The event to process.
22    */
23   public function onViewDetect406(GetResponseForControllerResultEvent $event) {
24     $request = $event->getRequest();
25     $result = $event->getControllerResult();
26
27     // If this is a render array then we assume that the router went with the
28     // generic controller and not one with a format. If the format requested is
29     // not HTML though we can also assume that the requested format is invalid
30     // so we provide a 406 response.
31     if (is_array($result) && $request->getRequestFormat() !== 'html') {
32       throw new NotAcceptableHttpException('Not acceptable format: ' . $request->getRequestFormat());
33     }
34   }
35
36   /**
37    * {@inheritdoc}
38    */
39   public static function getSubscribedEvents() {
40     $events[KernelEvents::VIEW][] = ['onViewDetect406', -10];
41
42     return $events;
43   }
44
45 }