Вход Регистрация
Файл: vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/HandlePrecognitiveRequests.php
Строк: 220
<?php

namespace IlluminateFoundationHttpMiddleware;

use 
IlluminateContainerContainer;
use 
IlluminateFoundationRoutingPrecognitionCallableDispatcher;
use 
IlluminateFoundationRoutingPrecognitionControllerDispatcher;
use 
IlluminateRoutingContractsCallableDispatcher as CallableDispatcherContract;
use 
IlluminateRoutingContractsControllerDispatcher as ControllerDispatcherContract;

class 
HandlePrecognitiveRequests
{
    
/**
     * The container instance.
     *
     * @var IlluminateContainerContainer
     */
    
protected $container;

    
/**
     * Create a new middleware instance.
     *
     * @param  IlluminateContainerContainer  $container
     */
    
public function __construct(Container $container)
    {
        
$this->container $container;
    }

    
/**
     * Handle an incoming request.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  Closure  $next
     * @return IlluminateHttpResponse
     */
    
public function handle($request$next)
    {
        if (! 
$request->isAttemptingPrecognition()) {
            return 
$this->appendVaryHeader($request$next($request));
        }

        
$bindings $this->container->getBindings();
        
$callableBinding $bindings[CallableDispatcherContract::class] ?? null;
        
$controllerBinding $bindings[ControllerDispatcherContract::class] ?? null;

        
$this->prepareForPrecognition($request);

        return 
tap($next($request), function ($response) use ($request$callableBinding$controllerBinding) {
            
$response->headers->set('Precognition''true');

            
$this->appendVaryHeader($request$response);

            
$this->restoreDispatchers($callableBinding$controllerBinding);
        });
    }

    
/**
     * Prepare to handle a precognitive request.
     *
     * @param  IlluminateHttpRequest  $request
     * @return void
     */
    
protected function prepareForPrecognition($request)
    {
        
$request->attributes->set('precognitive'true);

        
$this->container->bind(CallableDispatcherContract::class, fn ($app) => new PrecognitionCallableDispatcher($app));
        
$this->container->bind(ControllerDispatcherContract::class, fn ($app) => new PrecognitionControllerDispatcher($app));
    }

    
/**
     * Append the appropriate "Vary" header to the given response.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  IlluminateHttpResponse  $response
     * @return IlluminateHttpResponse
     */
    
protected function appendVaryHeader($request$response)
    {
        return 
tap($responsefn () => $response->headers->set('Vary'implode(', 'array_filter([
            
$response->headers->get('Vary'),
            
'Precognition',
        ]))));
    }

    
/**
     * Restore the original route dispatcher bindings.
     *
     * @param  array|null  $callableBinding
     * @param  array|null  $controllerBinding
     * @return void
     */
    
protected function restoreDispatchers($callableBinding$controllerBinding)
    {
        if (
$callableBinding) {
            
$this->container->bind(CallableDispatcherContract::class, $callableBinding['concrete'], $callableBinding['shared']);
        }

        if (
$controllerBinding) {
            
$this->container->bind(ControllerDispatcherContract::class, $controllerBinding['concrete'], $controllerBinding['shared']);
        }
    }
}
Онлайн: 1
Реклама