Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / symfony / http-kernel / Fragment / InlineFragmentRenderer.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpKernel\Fragment;
13
14 use Symfony\Component\HttpFoundation\Request;
15 use Symfony\Component\HttpFoundation\Response;
16 use Symfony\Component\HttpKernel\HttpKernelInterface;
17 use Symfony\Component\HttpKernel\Controller\ControllerReference;
18 use Symfony\Component\HttpKernel\KernelEvents;
19 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
20 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
21
22 /**
23  * Implements the inline rendering strategy where the Request is rendered by the current HTTP kernel.
24  *
25  * @author Fabien Potencier <fabien@symfony.com>
26  */
27 class InlineFragmentRenderer extends RoutableFragmentRenderer
28 {
29     private $kernel;
30     private $dispatcher;
31
32     public function __construct(HttpKernelInterface $kernel, EventDispatcherInterface $dispatcher = null)
33     {
34         $this->kernel = $kernel;
35         $this->dispatcher = $dispatcher;
36     }
37
38     /**
39      * {@inheritdoc}
40      *
41      * Additional available options:
42      *
43      *  * alt: an alternative URI to render in case of an error
44      */
45     public function render($uri, Request $request, array $options = array())
46     {
47         $reference = null;
48         if ($uri instanceof ControllerReference) {
49             $reference = $uri;
50
51             // Remove attributes from the generated URI because if not, the Symfony
52             // routing system will use them to populate the Request attributes. We don't
53             // want that as we want to preserve objects (so we manually set Request attributes
54             // below instead)
55             $attributes = $reference->attributes;
56             $reference->attributes = array();
57
58             // The request format and locale might have been overridden by the user
59             foreach (array('_format', '_locale') as $key) {
60                 if (isset($attributes[$key])) {
61                     $reference->attributes[$key] = $attributes[$key];
62                 }
63             }
64
65             $uri = $this->generateFragmentUri($uri, $request, false, false);
66
67             $reference->attributes = array_merge($attributes, $reference->attributes);
68         }
69
70         $subRequest = $this->createSubRequest($uri, $request);
71
72         // override Request attributes as they can be objects (which are not supported by the generated URI)
73         if (null !== $reference) {
74             $subRequest->attributes->add($reference->attributes);
75         }
76
77         $level = ob_get_level();
78         try {
79             return $this->kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
80         } catch (\Exception $e) {
81             // we dispatch the exception event to trigger the logging
82             // the response that comes back is simply ignored
83             if (isset($options['ignore_errors']) && $options['ignore_errors'] && $this->dispatcher) {
84                 $event = new GetResponseForExceptionEvent($this->kernel, $request, HttpKernelInterface::SUB_REQUEST, $e);
85
86                 $this->dispatcher->dispatch(KernelEvents::EXCEPTION, $event);
87             }
88
89             // let's clean up the output buffers that were created by the sub-request
90             Response::closeOutputBuffers($level, false);
91
92             if (isset($options['alt'])) {
93                 $alt = $options['alt'];
94                 unset($options['alt']);
95
96                 return $this->render($alt, $request, $options);
97             }
98
99             if (!isset($options['ignore_errors']) || !$options['ignore_errors']) {
100                 throw $e;
101             }
102
103             return new Response();
104         }
105     }
106
107     protected function createSubRequest($uri, Request $request)
108     {
109         $cookies = $request->cookies->all();
110         $server = $request->server->all();
111
112         // Override the arguments to emulate a sub-request.
113         // Sub-request object will point to localhost as client ip and real client ip
114         // will be included into trusted header for client ip
115         try {
116             if (Request::HEADER_X_FORWARDED_FOR & Request::getTrustedHeaderSet()) {
117                 $currentXForwardedFor = $request->headers->get('X_FORWARDED_FOR', '');
118
119                 $server['HTTP_X_FORWARDED_FOR'] = ($currentXForwardedFor ? $currentXForwardedFor.', ' : '').$request->getClientIp();
120             } elseif (method_exists(Request::class, 'getTrustedHeaderName') && $trustedHeaderName = Request::getTrustedHeaderName(Request::HEADER_CLIENT_IP, false)) {
121                 $currentXForwardedFor = $request->headers->get($trustedHeaderName, '');
122
123                 $server['HTTP_'.$trustedHeaderName] = ($currentXForwardedFor ? $currentXForwardedFor.', ' : '').$request->getClientIp();
124             }
125         } catch (\InvalidArgumentException $e) {
126             // Do nothing
127         }
128
129         $server['REMOTE_ADDR'] = '127.0.0.1';
130         unset($server['HTTP_IF_MODIFIED_SINCE']);
131         unset($server['HTTP_IF_NONE_MATCH']);
132
133         $subRequest = Request::create($uri, 'get', array(), $cookies, array(), $server);
134         if ($request->headers->has('Surrogate-Capability')) {
135             $subRequest->headers->set('Surrogate-Capability', $request->headers->get('Surrogate-Capability'));
136         }
137
138         if ($session = $request->getSession()) {
139             $subRequest->setSession($session);
140         }
141
142         return $subRequest;
143     }
144
145     /**
146      * {@inheritdoc}
147      */
148     public function getName()
149     {
150         return 'inline';
151     }
152 }