场景
- laravel在处理http request 之前启动了几个核心的bootstrap class, 下面简单聊聊\Illuminate\Foundation\Bootstrap\BootProviders::class 做了哪些功能
源码分析
public function bootstrap(Application $app){$app->boot();}
本质上执行的是service container boot function
$this->fireAppCallbacks($this->bootingCallbacks);
执行boot之前的回调函数
array_walk($this->serviceProviders, function ($p) {$this->bootProvider($p);});
调用所有eager provider 的boot function
if (method_exists($provider, 'boot')) {return $this->call([$provider, 'boot']);}
$this->fireAppCallbacks($this->bootedCallbacks);
执行 booted之后回调函数
小结
\Illuminate\Foundation\Bootstrap\BootProviders::class 做的事情比较简单, 执行boot之前之后的两个回调函数, 以及调用eager provider 的boot function
源码
class BootProviders
{
public function bootstrap(Application $app)
{
$app->boot();
}
}
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']);
}
}