| 148 |
lars |
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\Controller\ControllerReference;
|
|
|
17 |
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
|
|
|
18 |
use Symfony\Component\HttpKernel\HttpCache\SubRequestHandler;
|
|
|
19 |
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
|
|
20 |
use Symfony\Component\HttpKernel\KernelEvents;
|
|
|
21 |
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* Implements the inline rendering strategy where the Request is rendered by the current HTTP kernel.
|
|
|
25 |
*
|
|
|
26 |
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
27 |
*/
|
|
|
28 |
class InlineFragmentRenderer extends RoutableFragmentRenderer
|
|
|
29 |
{
|
|
|
30 |
private HttpKernelInterface $kernel;
|
|
|
31 |
private ?EventDispatcherInterface $dispatcher;
|
|
|
32 |
|
|
|
33 |
public function __construct(HttpKernelInterface $kernel, EventDispatcherInterface $dispatcher = null)
|
|
|
34 |
{
|
|
|
35 |
$this->kernel = $kernel;
|
|
|
36 |
$this->dispatcher = $dispatcher;
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Additional available options:
|
|
|
41 |
*
|
|
|
42 |
* * alt: an alternative URI to render in case of an error
|
|
|
43 |
*/
|
|
|
44 |
public function render(string|ControllerReference $uri, Request $request, array $options = []): Response
|
|
|
45 |
{
|
|
|
46 |
$reference = null;
|
|
|
47 |
if ($uri instanceof ControllerReference) {
|
|
|
48 |
$reference = $uri;
|
|
|
49 |
|
|
|
50 |
// Remove attributes from the generated URI because if not, the Symfony
|
|
|
51 |
// routing system will use them to populate the Request attributes. We don't
|
|
|
52 |
// want that as we want to preserve objects (so we manually set Request attributes
|
|
|
53 |
// below instead)
|
|
|
54 |
$attributes = $reference->attributes;
|
|
|
55 |
$reference->attributes = [];
|
|
|
56 |
|
|
|
57 |
// The request format and locale might have been overridden by the user
|
|
|
58 |
foreach (['_format', '_locale'] as $key) {
|
|
|
59 |
if (isset($attributes[$key])) {
|
|
|
60 |
$reference->attributes[$key] = $attributes[$key];
|
|
|
61 |
}
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
$uri = $this->generateFragmentUri($uri, $request, false, false);
|
|
|
65 |
|
|
|
66 |
$reference->attributes = array_merge($attributes, $reference->attributes);
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
$subRequest = $this->createSubRequest($uri, $request);
|
|
|
70 |
|
|
|
71 |
// override Request attributes as they can be objects (which are not supported by the generated URI)
|
|
|
72 |
if (null !== $reference) {
|
|
|
73 |
$subRequest->attributes->add($reference->attributes);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
$level = ob_get_level();
|
|
|
77 |
try {
|
|
|
78 |
return SubRequestHandler::handle($this->kernel, $subRequest, HttpKernelInterface::SUB_REQUEST, false);
|
|
|
79 |
} catch (\Exception $e) {
|
|
|
80 |
// we dispatch the exception event to trigger the logging
|
|
|
81 |
// the response that comes back is ignored
|
|
|
82 |
if (isset($options['ignore_errors']) && $options['ignore_errors'] && $this->dispatcher) {
|
|
|
83 |
$event = new ExceptionEvent($this->kernel, $request, HttpKernelInterface::SUB_REQUEST, $e);
|
|
|
84 |
|
|
|
85 |
$this->dispatcher->dispatch($event, KernelEvents::EXCEPTION);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
// let's clean up the output buffers that were created by the sub-request
|
|
|
89 |
Response::closeOutputBuffers($level, false);
|
|
|
90 |
|
|
|
91 |
if (isset($options['alt'])) {
|
|
|
92 |
$alt = $options['alt'];
|
|
|
93 |
unset($options['alt']);
|
|
|
94 |
|
|
|
95 |
return $this->render($alt, $request, $options);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
if (!isset($options['ignore_errors']) || !$options['ignore_errors']) {
|
|
|
99 |
throw $e;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
return new Response();
|
|
|
103 |
}
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
protected function createSubRequest(string $uri, Request $request)
|
|
|
107 |
{
|
|
|
108 |
$cookies = $request->cookies->all();
|
|
|
109 |
$server = $request->server->all();
|
|
|
110 |
|
|
|
111 |
unset($server['HTTP_IF_MODIFIED_SINCE']);
|
|
|
112 |
unset($server['HTTP_IF_NONE_MATCH']);
|
|
|
113 |
|
|
|
114 |
$subRequest = Request::create($uri, 'get', [], $cookies, [], $server);
|
|
|
115 |
if ($request->headers->has('Surrogate-Capability')) {
|
|
|
116 |
$subRequest->headers->set('Surrogate-Capability', $request->headers->get('Surrogate-Capability'));
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
static $setSession;
|
|
|
120 |
|
|
|
121 |
$setSession ??= \Closure::bind(static function ($subRequest, $request) { $subRequest->session = $request->session; }, null, Request::class);
|
|
|
122 |
$setSession($subRequest, $request);
|
|
|
123 |
|
|
|
124 |
if ($request->get('_format')) {
|
|
|
125 |
$subRequest->attributes->set('_format', $request->get('_format'));
|
|
|
126 |
}
|
|
|
127 |
if ($request->getDefaultLocale() !== $request->getLocale()) {
|
|
|
128 |
$subRequest->setLocale($request->getLocale());
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
return $subRequest;
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
public function getName(): string
|
|
|
135 |
{
|
|
|
136 |
return 'inline';
|
|
|
137 |
}
|
|
|
138 |
}
|