Вход Регистрация
Файл: vendor/symfony/http-kernel/Fragment/InlineFragmentRenderer.php
Строк: 212
<?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 
SymfonyComponentHttpKernelControllerControllerReference;
use 
SymfonyComponentHttpKernelEventExceptionEvent;
use 
SymfonyComponentHttpKernelHttpCacheSubRequestHandler;
use 
SymfonyComponentHttpKernelHttpKernelInterface;
use 
SymfonyComponentHttpKernelKernelEvents;
use 
SymfonyContractsEventDispatcherEventDispatcherInterface;

/**
 * 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 
HttpKernelInterface $kernel;
    private ?
EventDispatcherInterface $dispatcher;

    public function 
__construct(HttpKernelInterface $kernel, ?EventDispatcherInterface $dispatcher null)
    {
        
$this->kernel $kernel;
        
$this->dispatcher $dispatcher;
    }

    
/**
     * Additional available options:
     *
     *  * alt: an alternative URI to render in case of an error
     */
    
public function render(string|ControllerReference $uriRequest $request, array $options = []): Response
    
{
        
$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 = [];

            
// The request format and locale might have been overridden by the user
            
foreach (['_format''_locale'] as $key) {
                if (isset(
$attributes[$key])) {
                    
$reference->attributes[$key] = $attributes[$key];
                }
            }

            
$uri $this->generateFragmentUri($uri$requestfalsefalse);

            
$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 
SubRequestHandler::handle($this->kernel$subRequestHttpKernelInterface::SUB_REQUESTfalse);
        } catch (
Exception $e) {
            
// we dispatch the exception event to trigger the logging
            // the response that comes back is ignored
            
if (isset($options['ignore_errors']) && $options['ignore_errors'] && $this->dispatcher) {
                
$event = new ExceptionEvent($this->kernel$requestHttpKernelInterface::SUB_REQUEST$e);

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

            
// let's clean up the output buffers that were created by the sub-request
            
Response::closeOutputBuffers($levelfalse);

            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();
        }
    }

    
/**
     * @return Request
     */
    
protected function createSubRequest(string $uriRequest $request)
    {
        
$cookies $request->cookies->all();
        
$server $request->server->all();

        unset(
$server['HTTP_IF_MODIFIED_SINCE']);
        unset(
$server['HTTP_IF_NONE_MATCH']);

        
$subRequest Request::create($uri'get', [], $cookies, [], $server);
        if (
$request->headers->has('Surrogate-Capability')) {
            
$subRequest->headers->set('Surrogate-Capability'$request->headers->get('Surrogate-Capability'));
        }

        static 
$setSession;

        
$setSession ??= Closure::bind(static function ($subRequest$request) { $subRequest->session $request->session; }, nullRequest::class);
        
$setSession($subRequest$request);

        if (
$request->get('_format')) {
            
$subRequest->attributes->set('_format'$request->get('_format'));
        }
        if (
$request->getDefaultLocale() !== $request->getLocale()) {
            
$subRequest->setLocale($request->getLocale());
        }
        if (
$request->attributes->has('_stateless')) {
            
$subRequest->attributes->set('_stateless'$request->attributes->get('_stateless'));
        }
        if (
$request->attributes->has('_check_controller_is_allowed')) {
            
$subRequest->attributes->set('_check_controller_is_allowed'$request->attributes->get('_check_controller_is_allowed'));
        }

        return 
$subRequest;
    }

    public function 
getName(): string
    
{
        return 
'inline';
    }
}
Онлайн: 1
Реклама