d3f3721715
* 3.4: (33 commits) Remove remaining `@experimental` annotations Tests and fix for issue in array model data in EntityType field with multiple=true [Validator] Add unique entity violation cause [Lock] Automaticaly release lock when user forget it [Form] Fixed PercentToLocalizedStringTransformer to accept both comma and dot as decimal separator, if possible fixed CS [FrameworkBundle] Don't clear app pools on cache:clear Hide label button when its setted to false removed useless PHPDoc [HttpFoundation] Return instance in StreamedResponse [Form] Fix FormInterface::submit() annotation [PHPUnitBridge] don't remove when set to empty string PdoSessionHandler: fix advisory lock for pgsql when session.sid_bits_per_character > 4 HttpCache does not consider ESI resources in HEAD requests Fix translation for "This field was not expected" [Routing] Enhance Route(Collection) docblocks Added improvement for accuracy in MoneyToLocalizedStringTransformer. Removed unused private property Use correct verb form in the pull request template Use PHP_MAXPATHLEN in Filesystem. ...
146 lines
5.0 KiB
PHP
146 lines
5.0 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the Symfony package.
|
|
*
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Symfony\Component\HttpKernel\Fragment;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
|
use Symfony\Component\HttpKernel\Controller\ControllerReference;
|
|
use Symfony\Component\HttpKernel\KernelEvents;
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
|
|
|
/**
|
|
* Implements the inline rendering strategy where the Request is rendered by the current HTTP kernel.
|
|
*
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
|
*/
|
|
class InlineFragmentRenderer extends RoutableFragmentRenderer
|
|
{
|
|
private $kernel;
|
|
private $dispatcher;
|
|
|
|
/**
|
|
* @param HttpKernelInterface $kernel A HttpKernelInterface instance
|
|
* @param EventDispatcherInterface $dispatcher A EventDispatcherInterface instance
|
|
*/
|
|
public function __construct(HttpKernelInterface $kernel, EventDispatcherInterface $dispatcher = null)
|
|
{
|
|
$this->kernel = $kernel;
|
|
$this->dispatcher = $dispatcher;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*
|
|
* Additional available options:
|
|
*
|
|
* * alt: an alternative URI to render in case of an error
|
|
*/
|
|
public function render($uri, Request $request, array $options = array())
|
|
{
|
|
$reference = null;
|
|
if ($uri instanceof ControllerReference) {
|
|
$reference = $uri;
|
|
|
|
// Remove attributes from the generated URI because if not, the Symfony
|
|
// routing system will use them to populate the Request attributes. We don't
|
|
// want that as we want to preserve objects (so we manually set Request attributes
|
|
// below instead)
|
|
$attributes = $reference->attributes;
|
|
$reference->attributes = array();
|
|
|
|
// The request format and locale might have been overridden by the user
|
|
foreach (array('_format', '_locale') as $key) {
|
|
if (isset($attributes[$key])) {
|
|
$reference->attributes[$key] = $attributes[$key];
|
|
}
|
|
}
|
|
|
|
$uri = $this->generateFragmentUri($uri, $request, false, false);
|
|
|
|
$reference->attributes = array_merge($attributes, $reference->attributes);
|
|
}
|
|
|
|
$subRequest = $this->createSubRequest($uri, $request);
|
|
|
|
// override Request attributes as they can be objects (which are not supported by the generated URI)
|
|
if (null !== $reference) {
|
|
$subRequest->attributes->add($reference->attributes);
|
|
}
|
|
|
|
$level = ob_get_level();
|
|
try {
|
|
return $this->kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
|
|
} catch (\Exception $e) {
|
|
// we dispatch the exception event to trigger the logging
|
|
// the response that comes back is simply ignored
|
|
if (isset($options['ignore_errors']) && $options['ignore_errors'] && $this->dispatcher) {
|
|
$event = new GetResponseForExceptionEvent($this->kernel, $request, HttpKernelInterface::SUB_REQUEST, $e);
|
|
|
|
$this->dispatcher->dispatch(KernelEvents::EXCEPTION, $event);
|
|
}
|
|
|
|
// let's clean up the output buffers that were created by the sub-request
|
|
Response::closeOutputBuffers($level, false);
|
|
|
|
if (isset($options['alt'])) {
|
|
$alt = $options['alt'];
|
|
unset($options['alt']);
|
|
|
|
return $this->render($alt, $request, $options);
|
|
}
|
|
|
|
if (!isset($options['ignore_errors']) || !$options['ignore_errors']) {
|
|
throw $e;
|
|
}
|
|
|
|
return new Response();
|
|
}
|
|
}
|
|
|
|
protected function createSubRequest($uri, Request $request)
|
|
{
|
|
$cookies = $request->cookies->all();
|
|
$server = $request->server->all();
|
|
|
|
if (Request::HEADER_X_FORWARDED_FOR & Request::getTrustedHeaderSet()) {
|
|
$currentXForwardedFor = $request->headers->get('X_FORWARDED_FOR', '');
|
|
|
|
$server['HTTP_X_FORWARDED_FOR'] = ($currentXForwardedFor ? $currentXForwardedFor.', ' : '').$request->getClientIp();
|
|
}
|
|
|
|
$server['REMOTE_ADDR'] = '127.0.0.1';
|
|
unset($server['HTTP_IF_MODIFIED_SINCE']);
|
|
unset($server['HTTP_IF_NONE_MATCH']);
|
|
|
|
$subRequest = Request::create($uri, 'get', array(), $cookies, array(), $server);
|
|
if ($request->headers->has('Surrogate-Capability')) {
|
|
$subRequest->headers->set('Surrogate-Capability', $request->headers->get('Surrogate-Capability'));
|
|
}
|
|
|
|
if ($session = $request->getSession()) {
|
|
$subRequest->setSession($session);
|
|
}
|
|
|
|
return $subRequest;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function getName()
|
|
{
|
|
return 'inline';
|
|
}
|
|
}
|