annotate vendor/symfony/http-kernel/Fragment/InlineFragmentRenderer.php @ 0:c75dbcec494b

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