f607a895accce21e17142d453677e200b594534d
[yaffs-website] / web / core / modules / system / tests / modules / accept_header_routing_test / src / AcceptHeaderMiddleware.php
1 <?php
2
3 namespace Drupal\accept_header_routing_test;
4
5 use Symfony\Component\HttpFoundation\Request;
6 use Symfony\Component\HttpKernel\HttpKernelInterface;
7
8 /**
9  * Example implementation of "accept header"-based content negotiation.
10  */
11 class AcceptHeaderMiddleware implements HttpKernelInterface {
12
13   /**
14    * Constructs a new AcceptHeaderMiddleware instance.
15    *
16    * @param \Symfony\Component\HttpKernel\HttpKernelInterface $app
17    *   The app.
18    */
19   public function __construct(HttpKernelInterface $app) {
20     $this->app = $app;
21   }
22
23   /**
24    * {@inheritdoc}
25    */
26   public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
27     $mapping = [
28       'application/json' => 'json',
29       'application/hal+json' => 'hal_json',
30       'application/xml' => 'xml',
31       'text/html' => 'html',
32     ];
33
34     $accept = $request->headers->get('Accept') ?: ['text/html'];
35     if (isset($mapping[$accept[0]])) {
36       $request->setRequestFormat($mapping[$accept[0]]);
37     }
38
39     return $this->app->handle($request, $type, $catch);
40   }
41
42 }