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