Illuminate/Foundation/Http/Kernel.php


<?php namespace Illuminate\Foundation\Http;

use Exception;
use Illuminate\Routing\Router;
use Illuminate\Pipeline\Pipeline;
use Illuminate\Support\Facades\Facade;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Routing\TerminableMiddleware;
use Illuminate\Contracts\Http\Kernel as KernelContract;

class Kernel implements KernelContract {

    
/** * The application implementation. * * @var \Illuminate\Contracts\Foundation\Application */ protected $app;
/** * The router instance. * * @var \Illuminate\Routing\Router */ protected $router;
/** * The bootstrap classes for the application. * 应用程序启动时需要引导的类。 * * @var array */ protected $bootstrappers = [ // 检测运行环境,载入对应的 `.env` 文件 'Illuminate\Foundation\Bootstrap\DetectEnvironment', // 载入配置项,设置时区、编码 'Illuminate\Foundation\Bootstrap\LoadConfiguration', // 读取 app.log 配置,调用对应的的日志处理方法,绑定日志处理实例为 Monolog 'Illuminate\Foundation\Bootstrap\ConfigureLogging', // 设置“错误”“异常”“shutdown”处理函数,控制错误输出 'Illuminate\Foundation\Bootstrap\HandleExceptions', // 为 Facade 设置应用程序实例,注册别名自动加载 'Illuminate\Foundation\Bootstrap\RegisterFacades', // 注册 providers 配置项中的所有服务提供者 'Illuminate\Foundation\Bootstrap\RegisterProviders', // 启动应用程序的服务提供者 'Illuminate\Foundation\Bootstrap\BootProviders', ];
/** * The application's middleware stack. * * @var array */ protected $middleware = [];
/** * The application's route middleware. * * @var array */ protected $routeMiddleware = [];
/** * Create a new HTTP kernel instance. * * @param \Illuminate\Contracts\Foundation\Application $app * @param \Illuminate\Routing\Router $router * @return void */ public function __construct(Application $app, Router $router) { $this->app = $app; $this->router = $router; foreach ($this->routeMiddleware as $key => $middleware) { $router->middleware($key, $middleware); } }
/** * Handle an incoming HTTP request. * 处理传入的 HTTP 请求。 * * @param \Illuminate\Http\Request $request 请求实例 * @return \Illuminate\Http\Response */ public function handle($request) { try { // 通过中间件或路由发送给定的请求 $response = $this->sendRequestThroughRouter($request); } catch (Exception $e) { $this->reportException($e); $response = $this->renderException($request, $e); } // 触发 kernel.handled 事件,参数 $request, $response $this->app['events']->fire('kernel.handled', [$request, $response]); return $response; }
/** * Send the given request through the middleware / router. * 通过中间件或路由发送给定的请求。 * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ protected function sendRequestThroughRouter($request) { // 在容器中注册共享实例 $this->app->instance('request', $request); // 清除一个已经解析的 facade 实例 Facade::clearResolvedInstance('request'); // 通过 HTTP 请求引导应用程序 // 运行各引导类,调用其 bootstrap 方法,并触发“bootstrapping: Xxx”和“bootstrapped: Xxx”事件 $this->bootstrap(); // 获取响应实例 // 调用 router.before 路由前置过滤器,参数 $request // 若路由前置过滤器没有返回响应实例,则从一个路由调度请求,并取得响应实例 // 调用 router.after 路由后置过滤器,参数 $request, $response return (new Pipeline($this->app)) ->send($request) ->through($this->middleware) ->then($this->dispatchToRouter()); }
/** * Call the terminate method on any terminable middleware. * 在所有需要结束的中间件中调用 terminate 方法。 * * @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Response $response * @return void */ public function terminate($request, $response) { $routeMiddlewares = $this->gatherRouteMiddlewares($request); foreach (array_merge($routeMiddlewares, $this->middleware) as $middleware) { $instance = $this->app->make($middleware); if ($instance instanceof TerminableMiddleware) { $instance->terminate($request, $response); } } $this->app->terminate(); }
/** * Gather the route middleware for the given request. * * @param \Illuminate\Http\Request $request * @return array */ protected function gatherRouteMiddlewares($request) { if ($request->route()) { return $this->router->gatherRouteMiddlewares($request->route()); } return []; }
/** * Add a new middleware to beginning of the stack if it does not already exist. * * @param string $middleware * @return $this */ public function prependMiddleware($middleware) { if (array_search($middleware, $this->middleware) === false) { array_unshift($this->middleware, $middleware); } return $this; }
/** * Add a new middleware to end of the stack if it does not already exist. * * @param string $middleware * @return $this */ public function pushMiddleware($middleware) { if (array_search($middleware, $this->middleware) === false) { $this->middleware[] = $middleware; } return $this; }
/** * Bootstrap the application for HTTP requests. * 通过 HTTP 请求引导应用程序。 * * @return void */ public function bootstrap() { // 若应用程序未被引导 if ( ! $this->app->hasBeenBootstrapped()) { // 运行各引导类,调用其 bootstrap 方法,并触发“bootstrapping: Xxx”和“bootstrapped: Xxx”事件 $this->app->bootstrapWith($this->bootstrappers()); } }
/** * Get the route dispatcher callback. * 获取路由回调。 * * @return \Closure */ protected function dispatchToRouter() { return function($request) { $this->app->instance('request', $request); return $this->router->dispatch($request); }; }
/** * Get the bootstrap classes for the application. * 从应用程序中获取引导类。 * * @return array */ protected function bootstrappers() { return $this->bootstrappers; }
/** * Report the exception to the exception handler. * * @param \Exception $e * @return void */ protected function reportException(Exception $e) { $this->app['Illuminate\Contracts\Debug\ExceptionHandler']->report($e); }
/** * Render the exception to a response. * * @param \Illuminate\Http\Request $request * @param \Exception $e * @return \Symfony\Component\HttpFoundation\Response */ protected function renderException($request, Exception $e) { return $this->app['Illuminate\Contracts\Debug\ExceptionHandler']->render($request, $e); }
/** * Get the Laravel application instance. * * @return \Illuminate\Contracts\Foundation\Application */ public function getApplication() { return $this->app; } }