引言
在了解了laravle10大概的生命周期流程,从执行到回调整个步骤,接下来我们来对其中的流程进行详细剖解来看一下它究竟是怎样执行的。
在入口文件中有一个步骤是
$app = require_once __dir__.'/../bootstrap/app.php';
这样的,它加载了bootstrap/app.php文件,那么这个文件究竟做了写什么?我们一起往下看!
bootstrap/app.php都做了什么
创建应用实例
第一行它是这样的
/*
|--------------------------------------------------------------------------
| create the application
|--------------------------------------------------------------------------
|
| the first thing we will do is create a new laravel application instance
| which serves as the "glue" for all the components of laravel, and is
| the ioc container for the system binding all of the various parts.
|我们要做的第一件事是创建一个新的laravel应用程序实例,
|它作为laravel所有组件的“粘合剂”,
|使系统绑定所有不同部分的ioc(控制反转)容器。
*/
$app = new illuminate\foundation\application(
$_env['app_base_path'] ?? dirname(__dir__)
);意思就是说此处创建了应用实例,传入了项目的基础路径,那么这个application类里面又是怎样的逻辑?让我们往下看
application的构造方法: 在里面首先执行了setbasepath方法,在里面设置了一些基本路径
public function __construct($basepath = null)
{
if ($basepath) {
$this->setbasepath($basepath);
}
$this->registerbasebindings();
$this->registerbaseserviceproviders();
$this->registercorecontaineraliases();
}setbasepath设置了语言包、配置文件、静态资源、view页面、引导框架目录、数据库配置、日志缓存等基本路径。
/**
* set the base path for the application.
*
* @param string $basepath
* @return $this
*/
public function setbasepath($basepath)
{
$this->basepath = rtrim($basepath, '\/');
$this->bindpathsincontainer();
return $this;
}
/**
* bind all of the application paths in the container.
*
* @return void
*/
protected function bindpathsincontainer()
{
$this->instance('path', $this->path());
$this->instance('path.base', $this->basepath());
$this->instance('path.lang', $this->langpath());
$this->instance('path.config', $this->configpath());
$this->instance('path.public', $this->publicpath());
$this->instance('path.storage', $this->storagepath());
$this->instance('path.database', $this->databasepath());
$this->instance('path.resources', $this->resourcepath());
$this->instance('path.bootstrap', $this->bootstrappath());
}设置路径通过instance函数进行的,我们看一下instance函数。instance函数里面首先执行了removeabstractalias方法,对传入的变量进行清空,即(path.base、path、path.lang)等,如果这些变量都存在的话,就把它们释放删除了。
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;
}
/**
* remove an alias from the contextual binding alias cache.
*
* @param string $searched
* @return void
*/
protected function removeabstractalias($searched)
{
if (! isset($this->aliases[$searched])) {
return;
}
foreach ($this->abstractaliases as $abstract => $aliases) {
foreach ($aliases as $index => $alias) {
if ($alias == $searched) {
unset($this->abstractaliases[$abstract][$index]);
}
}
}
}在instance第二行里面,调用了bound函数,去确定给定的抽象类型是否已绑定,是否已被实例化,是否是别名。
$isbound = $this->bound($abstract);
/**
* determine if the given abstract type has been bound.
*
* @param string $abstract
* @return bool
*/
public function bound($abstract)
{
return isset($this->bindings[$abstract]) ||
isset($this->instances[$abstract]) ||
$this->isalias($abstract);
}接着就是删除别名
//删除别名 unset($this->aliases[$abstract]);
把实例放到instances数组中
$this->instances[$abstract] = $instance;
最后根据第二行返回的$isbound的值去执行rebound函数,执行到这里时,项目所需要的instances实例就已经加载完毕了,基础路径已经全部加载完成了。
if ($isbound) {
$this->rebound($abstract);
}
/**
* fire the "rebound" callbacks for the given abstract type.
*
* @param string $abstract
* @return void
*/
protected function rebound($abstract)
{
$instance = $this->make($abstract);
foreach ($this->getreboundcallbacks($abstract) as $callback) {
call_user_func($callback, $this, $instance);
}
}接着又增加了三个实例到instances数组内
注册基础绑定
$this->registerbasebindings();
把现有的实例注册为实例中的共享容器
/**
* register the basic bindings into the container.
*
* @return void
*/
protected function registerbasebindings()
{
static::setinstance($this);
//1,调用静态方法setinstance设置实例
$this->instance('app', $this);
//2,调用instance ,将现有的实例注册为实例中的共享容器,查看instance这个方法 (代码instance,上面已经说过)
//注册app,container
$this->instance(container::class, $this);
//3,在容器中注册mix获取版本化的mix文件路径
$this->singleton(mix::class);
$this->singleton(packagemanifest::class, function () {
return new packagemanifest(
new filesystem, $this->basepath(), $this->getcachedpackagespath()
);
});
}注册基础服务
$this->registerbaseserviceproviders();
查看registerbaseserviceproviders 注册所有基本服务提供商
/**
* register all of the base service providers.
*
* @return void
*/
protected function registerbaseserviceproviders()
{
//注册事件服务
$this->register(new eventserviceprovider($this));
//注册日志
$this->register(new logserviceprovider($this));
//注册路由
$this->register(new routingserviceprovider($this));
}注册容器别名
$this->registercorecontaineraliases();
registercorecontaineraliases 注册核心容器别名
将一些重要的接口绑定到容器中我们将能够在需要时解决它们
//http内核
$app->singleton(
illuminate\contracts\http\kernel::class,
app\http\kernel::class
);
//console内核
$app->singleton(
illuminate\contracts\console\kernel::class,
app\console\kernel::class
);
//laravel的异常处理
$app->singleton(
illuminate\contracts\debug\exceptionhandler::class,
app\exceptions\handler::class
);到此app.php的内容已经解析完了。
以上就是laravel生命周期启动(从创建应用实例到注册基础服务)过程解析的详细内容,更多关于laravel生命周期的资料请关注代码网其它相关文章!
发表评论