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