Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/http-kernel/Fragment/InlineFragmentRenderer.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
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 /** | |
33 * Constructor. | |
34 * | |
35 * @param HttpKernelInterface $kernel A HttpKernelInterface instance | |
36 * @param EventDispatcherInterface $dispatcher A EventDispatcherInterface instance | |
37 */ | |
38 public function __construct(HttpKernelInterface $kernel, EventDispatcherInterface $dispatcher = null) | |
39 { | |
40 $this->kernel = $kernel; | |
41 $this->dispatcher = $dispatcher; | |
42 } | |
43 | |
44 /** | |
45 * {@inheritdoc} | |
46 * | |
47 * Additional available options: | |
48 * | |
49 * * alt: an alternative URI to render in case of an error | |
50 */ | |
51 public function render($uri, Request $request, array $options = array()) | |
52 { | |
53 $reference = null; | |
54 if ($uri instanceof ControllerReference) { | |
55 $reference = $uri; | |
56 | |
57 // Remove attributes from the generated URI because if not, the Symfony | |
58 // routing system will use them to populate the Request attributes. We don't | |
59 // want that as we want to preserve objects (so we manually set Request attributes | |
60 // below instead) | |
61 $attributes = $reference->attributes; | |
62 $reference->attributes = array(); | |
63 | |
64 // The request format and locale might have been overridden by the user | |
65 foreach (array('_format', '_locale') as $key) { | |
66 if (isset($attributes[$key])) { | |
67 $reference->attributes[$key] = $attributes[$key]; | |
68 } | |
69 } | |
70 | |
71 $uri = $this->generateFragmentUri($uri, $request, false, false); | |
72 | |
73 $reference->attributes = array_merge($attributes, $reference->attributes); | |
74 } | |
75 | |
76 $subRequest = $this->createSubRequest($uri, $request); | |
77 | |
78 // override Request attributes as they can be objects (which are not supported by the generated URI) | |
79 if (null !== $reference) { | |
80 $subRequest->attributes->add($reference->attributes); | |
81 } | |
82 | |
83 $level = ob_get_level(); | |
84 try { | |
85 return $this->kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false); | |
86 } catch (\Exception $e) { | |
87 // we dispatch the exception event to trigger the logging | |
88 // the response that comes back is simply ignored | |
89 if (isset($options['ignore_errors']) && $options['ignore_errors'] && $this->dispatcher) { | |
90 $event = new GetResponseForExceptionEvent($this->kernel, $request, HttpKernelInterface::SUB_REQUEST, $e); | |
91 | |
92 $this->dispatcher->dispatch(KernelEvents::EXCEPTION, $event); | |
93 } | |
94 | |
95 // let's clean up the output buffers that were created by the sub-request | |
96 Response::closeOutputBuffers($level, false); | |
97 | |
98 if (isset($options['alt'])) { | |
99 $alt = $options['alt']; | |
100 unset($options['alt']); | |
101 | |
102 return $this->render($alt, $request, $options); | |
103 } | |
104 | |
105 if (!isset($options['ignore_errors']) || !$options['ignore_errors']) { | |
106 throw $e; | |
107 } | |
108 | |
109 return new Response(); | |
110 } | |
111 } | |
112 | |
113 protected function createSubRequest($uri, Request $request) | |
114 { | |
115 $cookies = $request->cookies->all(); | |
116 $server = $request->server->all(); | |
117 | |
118 // Override the arguments to emulate a sub-request. | |
119 // Sub-request object will point to localhost as client ip and real client ip | |
120 // will be included into trusted header for client ip | |
121 try { | |
122 if ($trustedHeaderName = Request::getTrustedHeaderName(Request::HEADER_CLIENT_IP)) { | |
123 $currentXForwardedFor = $request->headers->get($trustedHeaderName, ''); | |
124 | |
125 $server['HTTP_'.$trustedHeaderName] = ($currentXForwardedFor ? $currentXForwardedFor.', ' : '').$request->getClientIp(); | |
126 } | |
127 } catch (\InvalidArgumentException $e) { | |
128 // Do nothing | |
129 } | |
130 | |
131 $server['REMOTE_ADDR'] = '127.0.0.1'; | |
132 unset($server['HTTP_IF_MODIFIED_SINCE']); | |
133 unset($server['HTTP_IF_NONE_MATCH']); | |
134 | |
135 $subRequest = Request::create($uri, 'get', array(), $cookies, array(), $server); | |
136 if ($request->headers->has('Surrogate-Capability')) { | |
137 $subRequest->headers->set('Surrogate-Capability', $request->headers->get('Surrogate-Capability')); | |
138 } | |
139 | |
140 if ($session = $request->getSession()) { | |
141 $subRequest->setSession($session); | |
142 } | |
143 | |
144 return $subRequest; | |
145 } | |
146 | |
147 /** | |
148 * {@inheritdoc} | |
149 */ | |
150 public function getName() | |
151 { | |
152 return 'inline'; | |
153 } | |
154 } |