请求到响应的核个执行过程,主要可以归纳为四个阶段,即程序启动准备阶段、请求实例化阶段、请求处理阶段、响应发送和程序终止阶段。
publicindex.php中有这么一段代码
$app = require_once __DIR__.'/../bootstrap/app.php';
//app.php
<?php //用来实现服务容器的实例化过程 $app = new IlluminateFoundationApplication( realpath(__DIR__.'/../') ); //下面代码向服务容器中绑定核心类服务,并返回核心类服务 $app->singleton( IlluminateContractsHttpKernel::class, AppHttpKernel::class ); $app->singleton( IlluminateContractsConsoleKernel::class, AppConsoleKernel::class ); $app->singleton( IlluminateContractsDebugExceptionHandler::class, AppExceptionsHandler::class ); return $app;
IlluminateFoundationApplication
class Application extends Container implements ApplicationContract, HttpKernelInterface
public function __construct($basePath = null) { if ($basePath) { //注册应用的基础路径 $this->setBasePath($basePath); } //用来绑定基础服务,主要是绑定容器实例本身,是的其它对象可以很容易得到服务容器实例 $this->registerBaseBindings(); //注册基础服务提供者 $this->registerBaseServiceProviders(); //注册核心类别名和应用的基础路径 $this->registerCoreContainerAliases(); }
setBasePath
public function setBasePath($basePath) { $this->basePath = rtrim($basePath, '/'); //在容器中绑定应用程序的基础路径 $this->bindPathsInContainer(); return $this; }
registerBaseBindings
protected function registerBaseBindings() { static::setInstance($this); $this->instance('app', $this); $this->instance(Container::class, $this); $this->instance(PackageManifest::class, new PackageManifest( new Filesystem, $this->basePath(), $this->getCachedPackagesPath() )); } public function instance($abstract, $instance) { $this->removeAbstractAlias($abstract); $isBound = $this->bound($abstract); unset($this->aliases[$abstract]); // We'll check to determine if this type has been bound before, and if it has // we will fire the rebound callbacks registered with the container and it // can be updated with consuming classes that have gotten resolved here. $this->instances[$abstract] = $instance; if ($isBound) { $this->rebound($abstract); } return $instance; }
registerBaseServiceProviders
protected function registerBaseServiceProviders() { $this->register(new EventServiceProvider($this)); $this->register(new LogServiceProvider($this)); $this->register(new RoutingServiceProvider($this)); } public function register($provider, $options = [], $force = false) { //如果服务者已经存在 则获取这个实例对象 if (($registered = $this->getProvider($provider)) && ! $force) { return $registered; } //通过类名实例化一个服务提供者 if (is_string($provider)) { $provider = $this->resolveProvider($provider); } //向服务容器中注册服务 if (method_exists($provider, 'register')) { //每个服务提供者必须实现这个函数,用来填充服务器并提供服务 $provider->register(); } //标识该服务提供者已经注册过了 $this->markAsRegistered($provider); if ($this->booted) { //启动规定的服务器供者 并且会调用服务提供者的boot函数 //boot这个函数服务提供者可以不用实现 $this->bootProvider($provider); } return $provider; }
registerCoreContainerAliases :定义了整个框架的核心服务别名
public function registerCoreContainerAliases() { foreach ([ 'app' => [IlluminateFoundationApplication::class, IlluminateContractsContainerContainer::class, IlluminateContractsFoundationApplication::class, PsrContainerContainerInterface::class], 'auth' => [IlluminateAuthAuthManager::class, IlluminateContractsAuthFactory::class], 'auth.driver' => [IlluminateContractsAuthGuard::class], 'blade.compiler' => [IlluminateViewCompilersBladeCompiler::class], 'cache' => [IlluminateCacheCacheManager::class, IlluminateContractsCacheFactory::class], 'cache.store' => [IlluminateCacheRepository::class, IlluminateContractsCacheRepository::class], 'config' => [IlluminateConfigRepository::class, IlluminateContractsConfigRepository::class], 'cookie' => [IlluminateCookieCookieJar::class, IlluminateContractsCookieFactory::class, IlluminateContractsCookieQueueingFactory::class], 'encrypter' => [IlluminateEncryptionEncrypter::class, IlluminateContractsEncryptionEncrypter::class], 'db' => [IlluminateDatabaseDatabaseManager::class], 'db.connection' => [IlluminateDatabaseConnection::class, IlluminateDatabaseConnectionInterface::class], 'events' => [IlluminateEventsDispatcher::class, IlluminateContractsEventsDispatcher::class], 'files' => [IlluminateFilesystemFilesystem::class], 'filesystem' => [IlluminateFilesystemFilesystemManager::class, IlluminateContractsFilesystemFactory::class], 'filesystem.disk' => [IlluminateContractsFilesystemFilesystem::class], 'filesystem.cloud' => [IlluminateContractsFilesystemCloud::class], 'hash' => [IlluminateContractsHashingHasher::class], 'translator' => [IlluminateTranslationTranslator::class, IlluminateContractsTranslationTranslator::class], 'log' => [IlluminateLogWriter::class, IlluminateContractsLoggingLog::class, PsrLogLoggerInterface::class], 'mailer' => [IlluminateMailMailer::class, IlluminateContractsMailMailer::class, IlluminateContractsMailMailQueue::class], 'auth.password' => [IlluminateAuthPasswordsPasswordBrokerManager::class, IlluminateContractsAuthPasswordBrokerFactory::class], 'auth.password.broker' => [IlluminateAuthPasswordsPasswordBroker::class, IlluminateContractsAuthPasswordBroker::class], 'queue' => [IlluminateQueueQueueManager::class, IlluminateContractsQueueFactory::class, IlluminateContractsQueueMonitor::class], 'queue.connection' => [IlluminateContractsQueueQueue::class], 'queue.failer' => [IlluminateQueueFailedFailedJobProviderInterface::class], 'redirect' => [IlluminateRoutingRedirector::class], 'redis' => [IlluminateRedisRedisManager::class, IlluminateContractsRedisFactory::class], 'request' => [IlluminateHttpRequest::class, SymfonyComponentHttpFoundationRequest::class], 'router' => [IlluminateRoutingRouter::class, IlluminateContractsRoutingRegistrar::class, IlluminateContractsRoutingBindingRegistrar::class], 'session' => [IlluminateSessionSessionManager::class], 'session.store' => [IlluminateSessionStore::class, IlluminateContractsSessionSession::class], 'url' => [IlluminateRoutingUrlGenerator::class, IlluminateContractsRoutingUrlGenerator::class], 'validator' => [IlluminateValidationFactory::class, IlluminateContractsValidationFactory::class], 'view' => [IlluminateViewFactory::class, IlluminateContractsViewFactory::class], ] as $key => $aliases) { foreach ($aliases as $alias) { $this->alias($key, $alias); } } }
至此应用程序的准备工作已经完成
接下来我们再来看看index.php
/**
* 主要实现了服务容器的实例化和基本注册
*包括服务容器本身的注册,基础服务提供者注册,核心类别名注册和基本路径注册
*
*/
$kernel = $app->make(IlluminateContractsHttpKernel::class); //处理请求 $response = $kernel->handle( //请求实例的创建 $request = IlluminateHttpRequest::capture() );
namespace IlluminateFoundationHttpKernel
/** * Handle an incoming HTTP request. *处理一个http请求 * @param IlluminateHttpRequest $request * @return IlluminateHttpResponse */ public function handle($request) { try { //在请求过程中会添加CSRF保护,服务端会发送一个csrf令牌给客户端(cookie) $request->enableHttpMethodParameterOverride(); //请求处理,通过路由传输请求实例 $response = $this->sendRequestThroughRouter($request); } catch (Exception $e) { $this->reportException($e); $response = $this->renderException($request, $e); } catch (Throwable $e) { $this->reportException($e = new FatalThrowableError($e)); $response = $this->renderException($request, $e); } $this->app['events']->dispatch( new EventsRequestHandled($request, $response) ); return $response; }
protected function sendRequestThroughRouter($request) { $this->app->instance('request', $request); Facade::clearResolvedInstance('request'); //针对请求为应用程序'拔靴带' 执行bootstrap类的数组 //在请求处理阶段共有7个环节,每一个环节由一个类来实现的 //而且每个类都会有一个bootstrap()函数用于实现准备工作 $this->bootstrap(); return (new Pipeline($this->app)) ->send($request) ->through($this->app->shouldSkipMiddleware() ? [] : $this->middleware) ->then($this->dispatchToRouter()); }
/** * Bootstrap the application for HTTP requests. * * @return void */ public function bootstrap() { if (! $this->app->hasBeenBootstrapped()) { $this->app->bootstrapWith($this->bootstrappers()); } }
vendorlaravelframeworksrcIlluminateFoundationApplication.php
public function bootstrapWith(array $bootstrappers) { $this->hasBeenBootstrapped = true; foreach ($bootstrappers as $bootstrapper) { $this['events']->fire('bootstrapping: '.$bootstrapper, [$this]); $this->make($bootstrapper)->bootstrap($this); $this['events']->fire('bootstrapped: '.$bootstrapper, [$this]); } }
protected $bootstrappers = [ IlluminateFoundationBootstrapLoadEnvironmentVariables::class, IlluminateFoundationBootstrapLoadConfiguration::class,//环境检测与配置加载 IlluminateFoundationBootstrapHandleExceptions::class, IlluminateFoundationBootstrapRegisterFacades::class,//外观注册 IlluminateFoundationBootstrapRegisterProviders::class,//服务提供者注册 IlluminateFoundationBootstrapBootProviders::class,//启动服务 ];
接下来讲讲 服务提供者注册和启动服务
IlluminateFoundationBootstrapRegisterProviders::class,//服务提供者注册
//服务提供者注册
public function bootstrap(Application $app)
{
$app->registerConfiguredProviders();
}
在Application中注册所有配置的服务提供者
public function registerConfiguredProviders() { $providers = Collection::make($this->config['app.providers']) ->partition(function ($provider) { return Str::startsWith($provider, 'Illuminate\'); }); $providers->splice(1, 0, [$this->make(PackageManifest::class)->providers()]); (new ProviderRepository($this, new Filesystem, $this->getCachedServicesPath())) ->load($providers->collapse()->toArray()); }
/** * //注册应用的服务提供者 * * @param array $providers * @return void */ public function load(array $providers) { //加载Laravelootstrapcacheservices.json文件 $manifest = $this->loadManifest(); //加载服务清单,这里包含程序所有的服务提供者并进行分类 if ($this->shouldRecompile($manifest, $providers)) { $manifest = $this->compileManifest($providers); } //服务提供者加载时间,当这个事件发生时才自动加载这个服务提供者 foreach ($manifest['when'] as $provider => $events) { $this->registerLoadEvents($provider, $events); } //提前注册那些必须加载的服务提供者,以此为应用提供服务 foreach ($manifest['eager'] as $provider) { $this->app->register($provider); } //在列表中江路延迟加载的服务提供者,需要时再加载 $this->app->addDeferredServices($manifest['deferred']); }
服务提供者都继承于服务提供者基类(IlluminateSupportServiceProvider),该类是一个抽象类,其中定义了一个抽象函数register(),所以每个服务提供者都需要实现该函数,而该函数中实现了服务注册的内容
启动服务
在这最后一步中:服务提供者必须要实现register()函数,还有一个boot()函数根据需要决定是否实现,主要用于启动服务;对于boot()函数的服务提供者,会通过下面类统一管理调用
IlluminateFoundationBootstrapBootProviders::class,//启动服务
public function bootstrap(Application $app) { $app->boot(); }
在服务容器vendorlaravelframeworksrcIlluminateFoundationApplication.php代码中
public function boot() { if ($this->booted) { return; } $this->fireAppCallbacks($this->bootingCallbacks); array_walk($this->serviceProviders, function ($p) { $this->bootProvider($p); }); $this->booted = true; $this->fireAppCallbacks($this->bootedCallbacks); } protected function bootProvider(ServiceProvider $provider) { if (method_exists($provider, 'boot')) { return $this->call([$provider, 'boot']); } }
从上面可以看到,在 Larave !应用程序的服务容器中保存了服务提供者的实例数组,即 $serviceProviders 属性:
这单包含了服务容器实例化过程中注册的两个基础服务提供者及在服务提供者注册过程中注册的 eagc :类服务提供者,
然后通过代码“ $this->caIl([$provider,'boot']); $SserviceProviders 属性中记录的每一个服务提供者实例的 boot()函数,
该函数主要是对相应的服务进行初始化,