laravel容器类make方法解释
2024-09-01 09:38:18
容器类调用make方法时,如果没有已注册的key,那么会自动通过反射类实例化具体类
make
/**
* Resolve the given type from the container.
*
* @param string $abstract
* @param array $parameters
* @return mixed
*/
public function make($abstract, array $parameters = [])
{
$abstract = $this->getAlias($abstract); // If an instance of the type is currently being managed as a singleton we'll
// just return an existing instance instead of instantiating new instances
// so the developer can keep using the same objects instance every time.
if (isset($this->instances[$abstract])) {
return $this->instances[$abstract];
} $concrete = $this->getConcrete($abstract); // We're ready to instantiate an instance of the concrete type registered for
// the binding. This will instantiate the types, as well as resolve any of
// its "nested" dependencies recursively until all have gotten resolved.
if ($this->isBuildable($concrete, $abstract)) {
$object = $this->build($concrete, $parameters);
} else {
$object = $this->make($concrete, $parameters);
} // If we defined any extenders for this type, we'll need to spin through them
// and apply them to the object being built. This allows for the extension
// of services, such as changing configuration or decorating the object.
foreach ($this->getExtenders($abstract) as $extender) {
$object = $extender($object, $this);
} // If the requested type is registered as a singleton we'll want to cache off
// the instances in "memory" so we can return it later without creating an
// entirely new instance of an object on each subsequent request for it.
if ($this->isShared($abstract)) {
$this->instances[$abstract] = $object;
} $this->fireResolvingCallbacks($abstract, $object); $this->resolved[$abstract] = true; return $object;
}
getConcrete
/**
* Get the concrete type for a given abstract.
*
* @param string $abstract
* @return mixed $concrete
*/
protected function getConcrete($abstract)
{
if (! is_null($concrete = $this->getContextualConcrete($abstract))) {
return $concrete;
} // If we don't have a registered resolver or concrete for the type, we'll just
// assume each type is a concrete name and will attempt to resolve it as is
// since the container should be able to resolve concretes automatically.
if (! isset($this->bindings[$abstract])) {
if ($this->missingLeadingSlash($abstract) &&
isset($this->bindings['\\'.$abstract])) {
$abstract = '\\'.$abstract;
} return $abstract;
} return $this->bindings[$abstract]['concrete'];
}
build
/**
* Instantiate a concrete instance of the given type.
*
* @param string $concrete
* @param array $parameters
* @return mixed
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function build($concrete, array $parameters = [])
{
// If the concrete type is actually a Closure, we will just execute it and
// hand back the results of the functions, which allows functions to be
// used as resolvers for more fine-tuned resolution of these objects.
if ($concrete instanceof Closure) {
return $concrete($this, $parameters);
} $reflector = new ReflectionClass($concrete); // If the type is not instantiable, the developer is attempting to resolve
// an abstract type such as an Interface of Abstract Class and there is
// no binding registered for the abstractions so we need to bail out.
if (! $reflector->isInstantiable()) {
$message = "Target [$concrete] is not instantiable."; throw new BindingResolutionContractException($message);
} $this->buildStack[] = $concrete; $constructor = $reflector->getConstructor(); // If there are no constructors, that means there are no dependencies then
// we can just resolve the instances of the objects right away, without
// resolving any other types or dependencies out of these containers.
if (is_null($constructor)) {
array_pop($this->buildStack); return new $concrete;
} $dependencies = $constructor->getParameters(); // Once we have all the constructor's parameters we can create each of the
// dependency instances and then use the reflection instances to make a
// new instance of this class, injecting the created dependencies in.
$parameters = $this->keyParametersByArgument(
$dependencies, $parameters
); $instances = $this->getDependencies(
$dependencies, $parameters
); array_pop($this->buildStack); return $reflector->newInstanceArgs($instances);
}
最新文章
- 在table中进行内容搜索
- 排序陷阱 List.Sort Linq.OrderBy
- 大话设计模式C++版——装饰模式
- OSG中的示例程序简介
- candence 知识积累4
- const实现
- Uploadify 上传文件插件详解
- Java中Properties类的使用
- Debian安装Apache2+MySQL5+PHP5(zz)
- zookeeer client 通信协议
- 响应式移动端去除css的hover和jq的hover还有input在苹果下的默认样式
- centos7上安装ELK
- Swift中String和NSString的一个不同之处
- ASP.NET项目开发
- “指定的参数已超出有效值的范围”在【 parameterUpdate.Add(new OracleParameter(";STATUS";, 0));】报错
- .NetCore中EFCore的使用整理
- [转][C#]Web.config 相关配置
- js检查字符串的包含关系
- 浅谈System.gc()
- Centos7 搭建lnmp环境 (centos7+nginx+MySQL5.7.9+PHP7)
热门文章
- vue 数组更新检测注意事项
- 关于LinkedList for OpenJDK
- javaScript基础及初始面向对象
- Beyond compare4密钥
- Invalid JDK version in profile 'doclint-java8-disable': Unbounded range: [1.8, for project com.google.code.gson:gson 解决办法
- hadoop 2.x HA 出现ssh不能解析问题记录。
- Lucene核心数据结构——FST存词典,跳表存倒排或者roarning bitmap 见另外一个文章
- CSAW Quals CTF 2017-scv
- O缺缺缺缺氧 O缺缺氧
- openwrt环境中某个运行在host端的软件如何安装到openwrt的$(STAGING_DIR_HOST)/bin下