Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\StackMiddleware;
|
Chris@0
|
4
|
Chris@0
|
5 use Symfony\Component\HttpFoundation\Request;
|
Chris@0
|
6 use Symfony\Component\HttpKernel\HttpKernelInterface;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Provides a middleware to determine the content type upon the accept header.
|
Chris@0
|
10 *
|
Chris@0
|
11 * @todo This is a temporary solution, remove this in https://www.drupal.org/node/2364011
|
Chris@0
|
12 */
|
Chris@0
|
13 class NegotiationMiddleware implements HttpKernelInterface {
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * The wrapped HTTP kernel.
|
Chris@0
|
17 *
|
Chris@0
|
18 * @var \Symfony\Component\HttpKernel\HttpKernelInterface
|
Chris@0
|
19 */
|
Chris@0
|
20 protected $app;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Contains a hashmap of format as key and mimetype as value.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @var array
|
Chris@0
|
26 */
|
Chris@0
|
27 protected $formats = [];
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * Constructs a new NegotiationMiddleware.
|
Chris@0
|
31 *
|
Chris@0
|
32 * @param \Symfony\Component\HttpKernel\HttpKernelInterface $app
|
Chris@0
|
33 * The wrapper HTTP kernel
|
Chris@0
|
34 */
|
Chris@0
|
35 public function __construct(HttpKernelInterface $app) {
|
Chris@0
|
36 $this->app = $app;
|
Chris@0
|
37 }
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * {@inheritdoc}
|
Chris@0
|
41 */
|
Chris@0
|
42 public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
|
Chris@0
|
43 // Register available mime types.
|
Chris@0
|
44 foreach ($this->formats as $format => $mime_type) {
|
Chris@0
|
45 $request->setFormat($format, $mime_type);
|
Chris@0
|
46 }
|
Chris@0
|
47
|
Chris@0
|
48 // Determine the request format using the negotiator.
|
Chris@16
|
49 if ($requested_format = $this->getContentType($request)) {
|
Chris@16
|
50 $request->setRequestFormat($requested_format);
|
Chris@16
|
51 }
|
Chris@0
|
52 return $this->app->handle($request, $type, $catch);
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@0
|
55 /**
|
Chris@0
|
56 * Registers a format for a given MIME type.
|
Chris@0
|
57 *
|
Chris@0
|
58 * @param string $format
|
Chris@0
|
59 * The format.
|
Chris@0
|
60 * @param string $mime_type
|
Chris@0
|
61 * The MIME type.
|
Chris@0
|
62 *
|
Chris@0
|
63 * @return $this
|
Chris@0
|
64 */
|
Chris@0
|
65 public function registerFormat($format, $mime_type) {
|
Chris@0
|
66 $this->formats[$format] = $mime_type;
|
Chris@0
|
67 return $this;
|
Chris@0
|
68 }
|
Chris@0
|
69
|
Chris@0
|
70 /**
|
Chris@0
|
71 * Gets the normalized type of a request.
|
Chris@0
|
72 *
|
Chris@0
|
73 * The normalized type is a short, lowercase version of the format, such as
|
Chris@0
|
74 * 'html', 'json' or 'atom'.
|
Chris@0
|
75 *
|
Chris@0
|
76 * @param \Symfony\Component\HttpFoundation\Request $request
|
Chris@0
|
77 * The request object from which to extract the content type.
|
Chris@0
|
78 *
|
Chris@0
|
79 * @return string
|
Chris@0
|
80 * The normalized type of a given request.
|
Chris@0
|
81 */
|
Chris@0
|
82 protected function getContentType(Request $request) {
|
Chris@0
|
83 // AJAX iframe uploads need special handling, because they contain a JSON
|
Chris@0
|
84 // response wrapped in <textarea>.
|
Chris@0
|
85 if ($request->request->get('ajax_iframe_upload', FALSE)) {
|
Chris@0
|
86 return 'iframeupload';
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 if ($request->query->has('_format')) {
|
Chris@0
|
90 return $request->query->get('_format');
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@16
|
93 // No format was specified in the request.
|
Chris@16
|
94 return NULL;
|
Chris@0
|
95 }
|
Chris@0
|
96
|
Chris@0
|
97 }
|