照猫画虎 实现 min-laravel 框架系列之服务提供者的注册和启动
- laravel
- 2020-06-15
- 3332
- 0
HTTP Kernel 服务提供者的注册和启动
注册
Illuminate\Foundation\Bootstrap\RegisterProviders;
// 调用 application 类的 registerConfiguredProviders 方法
public function bootstrap(Application $app)
{
$app->registerConfiguredProviders();
}
....
....
** Illuminate\Foundation\Application;
public function registerConfiguredProviders()
{
$providers = $this['config']->get('app.providers');
(new ProviderRepository($this, new Filesystem, $this->getCachedServicesPath()))->load($providers);
}
Filesystem 类
该类主要是负责生产缓存文件,有关文件操作的方法都在这个类中
ProviderRepository 类
load 方法
public function load(array $providers)
{
// 加载缓存文件 @bootstrap/cache/services.php
$manifest = $this->loadManifest();
// 通过 $providers 判断是否需要重新生成缓存文件
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']);
}
manifest 数据格式
启动
** Illuminate\Foundation\Application;
public function boot()
{
if ($this->isBooted()) {
return;
}
// 调用注册的回调函数
$this->fireAppCallbacks($this->bootingCallbacks);
array_walk($this->serviceProviders, function ($p) {
$this->bootProvider($p);
});
$this->booted = true;
$this->fireAppCallbacks($this->bootedCallbacks);
}