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