Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

04. ServiceProvider Boot 解析 #4

Open
xiaohuilam opened this issue Sep 25, 2018 · 0 comments
Open

04. ServiceProvider Boot 解析 #4

xiaohuilam opened this issue Sep 25, 2018 · 0 comments
Labels
book The digital book for laravel learning service provider The book which talk about service providers

Comments

@xiaohuilam
Copy link
Owner

xiaohuilam commented Sep 25, 2018

随着前面 《02. Kernel Handle解析》 和 《03. ServiceProvider Register 解析》 的结束,我们接下来要分析的便是 启动服务提供者 这个步骤。

代码

<?php
namespace Illuminate\Foundation\Bootstrap;
use Illuminate\Contracts\Foundation\Application;
class BootProviders
{
/**
* Bootstrap the given application.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @return void
*/
public function bootstrap(Application $app)
{
$app->boot();
}
}

ServiceProvider Register 一样的,在 BootProviders 中也是调用了 Illuminate\Foundation\Application::boot()

/**
* Boot the application's service providers.
*
* @return void
*/
public function boot()
{
if ($this->booted) {
return;
}
// Once the application has booted we will also fire some "booted" callbacks
// for any listeners that need to do work after this initial booting gets
// finished. This is useful when ordering the boot-up processes we run.
$this->fireAppCallbacks($this->bootingCallbacks);
array_walk($this->serviceProviders, function ($p) {
$this->bootProvider($p);
});
$this->booted = true;
$this->fireAppCallbacks($this->bootedCallbacks);
}

首先判断是否 boot 过。

然后通过触发 $bootingCallbacks 钩子

/**
* Call the booting callbacks for the application.
*
* @param array $callbacks
* @return void
*/
protected function fireAppCallbacks(array $callbacks)
{
foreach ($callbacks as $callback) {
call_user_func($callback, $this);
}
}

$bootingCallbacks 是来自 02. HTTP Kernel Handle解析 登记的闭包,主要是服务于声明了 $defer = true 的服务提供者。

然后依次遍历 $this->serviceProviders 执行 Illuminate\Foundation\Application::bootProvider()

/**
* Boot the given service provider.
*
* @param \Illuminate\Support\ServiceProvider $provider
* @return mixed
*/
protected function bootProvider(ServiceProvider $provider)
{
if (method_exists($provider, 'boot')) {
return $this->call([$provider, 'boot']);
}
}

其实就是运行一遍 ServiceProvider 的 boot 方法

接着将 Illuminate\Foundation\Application::$boot 设置为 true

最后触发 bootedCallbacks 钩子

@xiaohuilam xiaohuilam added the book The digital book for laravel learning label Sep 25, 2018
@xiaohuilam xiaohuilam added the service provider The book which talk about service providers label Oct 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
book The digital book for laravel learning service provider The book which talk about service providers
Projects
None yet
Development

No branches or pull requests

1 participant