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