[laravel源码] Container/resolve 解析
说明
本章说明容器中 resolve
的流程。
源码
vendor\laravel\framework\src\Illuminate\Container\Container.php
protected function resolve($abstract, $parameters = [])
{
// 1. aliases获取别名
$abstract = $this->getAlias($abstract);
// 2. 判断是否需要匿名函数
$needsContextualBuild = ! empty($parameters) || ! is_null(
$this->getContextualConcrete($abstract)
);
// 3. instances已存在且已有匿名函数,直接返回
if (isset($this->instances[$abstract]) && ! $needsContextualBuild) {
return $this->instances[$abstract];
}
$this->with[] = $parameters;
// 4. 获取匿名函数
$concrete = $this->getConcrete($abstract);
// 5.条件判断,进入不同流程
if ($this->isBuildable($concrete, $abstract)) {
$object = $this->build($concrete);
} else {
$object = $this->make($concrete);
}
// 6. 获取扩展回调,有,则执行
foreach ($this->getExtenders($abstract) as $extender) {
$object = $extender($object, $this);
}
// 7.判断share并赋值
if ($this->isShared($abstract) && ! $needsContextualBuild) {
$this->instances[$abstract] = $object;
}
// 8. 执行解析到的回调函数
$this->fireResolvingCallbacks($abstract, $object);
// 9. 赋值
$this->resolved[$abstract] = true;
array_pop($this->with);
return $object;
}
分析
本作品采用《CC 协议》,转载必须注明作者和本文链接