Вход Регистрация
Файл: vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Fragment/InlineFragmentRenderer.php
Строк: 222
<?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 SymfonyComponentHttpKernelFragment;

use 
SymfonyComponentHttpFoundationRequest;
use 
SymfonyComponentHttpFoundationResponse;
use 
SymfonyComponentHttpKernelHttpKernelInterface;
use 
SymfonyComponentHttpKernelControllerControllerReference;
use 
SymfonyComponentHttpKernelKernelEvents;
use 
SymfonyComponentHttpKernelEventGetResponseForExceptionEvent;
use 
SymfonyComponentEventDispatcherEventDispatcherInterface;

/**
 * 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;

    
/**
     * Constructor.
     *
     * @param HttpKernelInterface           $kernel     A HttpKernelInterface instance
     * @param EventDispatcherInterface|null $dispatcher A EventDispatcherInterface instance
     */
    
public function __construct(HttpKernelInterface $kernelEventDispatcherInterface $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($uriRequest $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 overriden by the user
            
foreach (array('_format''_locale') as $key) {
                if (isset(
$attributes[$key])) {
                    
$reference->attributes[$key] = $attributes[$key];
                }
            }

            
$uri $this->generateFragmentUri($uri$request);
            
$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($subRequestHttpKernelInterface::SUB_REQUESTfalse);
        } 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$requestHttpKernelInterface::SUB_REQUEST$e);

                
$this->dispatcher->dispatch(KernelEvents::EXCEPTION$event);
            }

            
// let's clean up the output buffers that were created by the sub-request
            
while (ob_get_level() > $level) {
                
ob_get_clean();
            }

            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($uriRequest $request)
    {
        
$cookies $request->cookies->all();
        
$server $request->server->all();

        
// Override the arguments to emulate a sub-request.
        // Sub-request object will point to localhost as client ip and real client ip
        // will be included into trusted header for client ip
        
try {
            if (
$trustedHeaderName Request::getTrustedHeaderName(Request::HEADER_CLIENT_IP)) {
                
$currentXForwardedFor $request->headers->get($trustedHeaderName'');

                
$server['HTTP_'.$trustedHeaderName] = ($currentXForwardedFor $currentXForwardedFor.', ' '').$request->getClientIp();
            }
        } catch (
InvalidArgumentException $e) {
            
// Do nothing
        
}

        
$server['REMOTE_ADDR'] = '127.0.0.1';

        
$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';
    }
}
Онлайн: 2
Реклама