看 Laravel 源代码了解 Container

自从上文《看 Laravel 源代码了解 ServiceProvider 的加载》,咱们知道 Application (or Container) 充当 Laravel 的容器,基本把全部 Laravel 核心的功能归入这个容器里了。php

咱们今天来看看这个 Application / Container 究竟是什么东西?html

了解 Container 以前,咱们须要先简单说说 Inversion of Control (控制反转) 的原理。laravel

Inversion of Control

要知道什么是 Inversion of Control 以前,咱们最好先了解一个原则:编程

依赖倒转原则 (Dependence Inversion Priciple, DIP)提倡:数组

  • 高层模块不该该依赖底层模块。两个都应该依赖抽象
  • 抽象不该该依赖细节,细节应该依赖抽象
  • 针对接口编程,不要针对实现编程

在编程时,咱们对代码进行模块化开发,它们之间避免不了有依赖,如模块 A 依赖模块 B,那么根据 DIP,模块 A 应该依赖模块 B 的接口,而不该该依赖模块 B 的实现。bash

下面咱们举个例子。闭包

咱们须要一个 Logger 功能,将系统 log 输出到文件中。咱们能够能够这么写:app

class LogToFile {
    public function execute($message) {
        info('log the message to a file :'.$message);
    }
}
复制代码

在须要的地方直接调用:ide

class UseLogger {
    protected $logger;

    public function __construct(LogToFile $logger) {
        $this->logger = $logger;
    }

    public function show() {
        $user = 'yemeishu';
        $this->logger->execute($user);
    }
}
复制代码

写个测试用例:模块化

$useLogger = new UseLogger(new LogToFile());

$useLogger->show();
复制代码

若是这时候咱们须要将 log 输出到钉钉上,咱们从新写一个 LogToDD 类:

class LogToDD {
    public function execute($message) {
        info('log the message to 钉钉 :'.$message);
    }
}
复制代码

这时候,咱们还须要修改使用端 (UseLogger) 代码,让它引入 LogToDD 类:

class UseLogger {
    protected $logger;

    public function __construct(LogToDD $logger) {
        $this->logger = $logger;
    }

    public function show() {
        $user = 'yemeishu';
        $this->logger->execute($user);
    }
}
复制代码

其实到这,你就能「嗅出」坏代码来了:

假如我使用端特别多,那就意味着每一个地方我都要作引入修改。

根据 DIP 原则,咱们应该面向接口开发。让使用端依赖接口,而不是实现。因此咱们建立一个接口:

interface Logger {
    public function execute($message);
}
复制代码

而后让 LogToFileLogToDD 做为 Logger 的实现:

class LogToFile implements Logger {
    public function execute($message) {
        info('log the message to a file :'.$message);
    }
}

class LogToDD implements Logger {
    public function execute($message) {
        info('log the message to 钉钉 :'.$message);
    }
}
复制代码

这样咱们在使用端时,直接引入 Logger 接口,让代码剥离具体实现。

class UseLogger {
    protected $logger;

    public function __construct(Logger $logger) {
        $this->logger = $logger;
    }

    public function show() {
        $user = 'yemeishu';
        $this->logger->execute($user);
    }
}
复制代码

这样就能够保证,不管是使用文件保存,仍是下发到钉钉上,均可以不用去改代码了,只须要再真正调用的时候,随着业务须要自行选择。如测试:

$useLogger1 = new UseLogger(new LogToFile());
$useLogger1->show();

$useLogger2 = new UseLogger(new LogToDD());
$useLogger2->show();
复制代码

结果:

但这里有个问题,最后在实例化使用时,仍是要「硬编码」的方式 new 咱们的实现类 (LogToDD or LogToFile)。

那有没有办法更进一步把最后的 new LogToDD() 也控制反转了呢?

绑定的实现

这里咱们把实现类绑定在 interface 或者标识 key 上,只要解析这个 interface 或者 key,就能够拿到咱们的实现类。

咱们来写一个简单的类来达到绑定和解析的功能:

class SimpleContainer {

    // 用于存储全部绑定 key-value
    protected static $container = [];

    public static function bind($name, Callable $resolver) {
        static::$container[$name] = $resolver;
    }

    public static function make($name) {
        if(isset(static::$container[$name])){
            $resolver = static::$container[$name] ;
            return $resolver();
        }
        throw new Exception("Binding does not exist in container");
    }
}
复制代码

咱们能够测试下:

SimpleContainer::bind(Logger::class, function () {
    return new LogToFile();
});

$useLogger3 = new UseLogger(SimpleContainer::make(Logger::class));

$useLogger3->show();
复制代码

只要有一处绑定了 LoggerLogToFile 的关系,就能够在任何须要调用的地方直接解析引用了。

也就意味着,经过这种方法,在全部编码的地方都是引入「interface」,而不是实现类。完全实现 DPI 原则。

当咱们把全部这种绑定汇集在一块儿,就构成了咱们今天的主题内容:「Container」—— illuminate / container。

Laravel Container

在研究 Laravel Container 以前,咱们根据上面的例子,使用 Container,看怎么方便实现。

$container = new Container();
$container->bind(Logger::class, LogToFile::class);

$useLogger4 = new UseLogger($container->make(Logger::class));
$useLogger4->show();
复制代码

达到同样的效果

[2018-05-19 15:36:30] testing.INFO: log the message to a file :yemeishu
复制代码

注:在 Laravel 开发时,咱们会把这个绑定写在 APPServiceProvider 的 boot 或者 register 中,或者其余的 ServiceProvider 上也行。

结合上一篇文章《看 Laravel 源代码了解 ServiceProvider 的加载》和以上的原理的讲解。咱们对 Container 的使用,已经不陌生了。

接下来就能够看看 Container 的源代码了。

Container 源码解析

从上文能够知道,Container 的做用主要有两个,一个是绑定,另个一个是解析。

绑定

咱们看看主要有哪些绑定类型:

  1. 绑定一个单例
  2. 绑定实例
  3. 绑定接口到实现
  4. 绑定初始数据
  5. 情境绑定
  6. tag 标记绑定

下面咱们根据这些类型进行分析:

1. 绑定一个单例

public function singleton($abstract, $concrete = null) {
        $this->bind($abstract, $concrete, true);
    }
复制代码

主要利用参数 $share = true 来标记此时绑定为一个单例。

2. 绑定实例

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;
}
复制代码

绑定实例,主要是将 [$abstract, $instance]存储进数组 $instances 中。

3. tag 标记绑定

/** * Assign a set of tags to a given binding. * * @param array|string $abstracts * @param array|mixed ...$tags * @return void */
public function tag($abstracts, $tags) {
    $tags = is_array($tags) ? $tags : array_slice(func_get_args(), 1);

    foreach ($tags as $tag) {
        if (! isset($this->tags[$tag])) {
            $this->tags[$tag] = [];
        }

        foreach ((array) $abstracts as $abstract) {
            $this->tags[$tag][] = $abstract;
        }
    }
}
复制代码

这个挺好理解,主要是将 $abstracts 数组放在同一组标签下,最后能够经过 tag,解析这一组 $abstracts

4. 绑定

public function bind($abstract, $concrete = null, $shared = false) {
    // If no concrete type was given, we will simply set the concrete type to the
    // abstract type. After that, the concrete type to be registered as shared
    // without being forced to state their classes in both of the parameters.
    $this->dropStaleInstances($abstract);

    // 若是传入的实现为空,则绑定 $concrete 本身
    if (is_null($concrete)) {
        $concrete = $abstract;
    }

    // If the factory is not a Closure, it means it is just a class name which is
    // bound into this container to the abstract type and we will just wrap it
    // up inside its own Closure to give us more convenience when extending.
    // 目的是将 $concrete 转成闭包函数
    if (! $concrete instanceof Closure) {
        $concrete = $this->getClosure($abstract, $concrete);
    }

    // 存储到 $bindings 数组中,若是 $shared = true, 则表示绑定单例
    $this->bindings[$abstract] = compact('concrete', 'shared');

    // If the abstract type was already resolved in this container we'll fire the
    // rebound listener so that any objects which have already gotten resolved
    // can have their copy of the object updated via the listener callbacks.
    if ($this->resolved($abstract)) {
        $this->rebound($abstract);
    }
}
复制代码

解析

/** * Resolve the given type from the container. * * @param string $abstract * @param array $parameters * @return mixed */
public function make($abstract, array $parameters = []) {
    return $this->resolve($abstract, $parameters);
}

/** * Resolve the given type from the container. * * @param string $abstract * @param array $parameters * @return mixed */
protected function resolve($abstract, $parameters = []) {
    $abstract = $this->getAlias($abstract);

    $needsContextualBuild = ! empty($parameters) || ! is_null(
        $this->getContextualConcrete($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]) && ! $needsContextualBuild) {
        return $this->instances[$abstract];
    }

    $this->with[] = $parameters;

    $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);
    } else {
        $object = $this->make($concrete);
    }

    // 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) && ! $needsContextualBuild) {
        $this->instances[$abstract] = $object;
    }

    $this->fireResolvingCallbacks($abstract, $object);

    // Before returning, we will also set the resolved flag to "true" and pop off
    // the parameter overrides for this build. After those two things are done
    // we will be ready to return back the fully constructed class instance.
    $this->resolved[$abstract] = true;

    array_pop($this->with);

    return $object;
}
复制代码

咱们一步步来分析该「解析」函数:

$needsContextualBuild = ! empty($parameters) || ! is_null(
    $this->getContextualConcrete($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]) && ! $needsContextualBuild) {
    return $this->instances[$abstract];
}
复制代码

若是是绑定的单例,而且不须要上面的参数依赖。咱们就能够直接返回 $this->instances[$abstract]

$concrete = $this->getConcrete($abstract);

...

/** * 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])) {
        return $this->bindings[$abstract]['concrete'];
    }

    return $abstract;
}
复制代码

这一步主要是先从绑定的上下文找,是否是能够找到绑定类;若是没有,则再从 $bindings[] 中找关联的实现类;最后尚未找到的话,就直接返回 $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);
} else {
    $object = $this->make($concrete);
}

...

/** * Determine if the given concrete is buildable. * * @param mixed $concrete * @param string $abstract * @return bool */
protected function isBuildable($concrete, $abstract) {
    return $concrete === $abstract || $concrete instanceof Closure;
}
复制代码

这个比较好理解,若是以前找到的 $concrete 返回的是 $abstract 值,或者 $concrete 是个闭包,则执行 $this->build($concrete),不然,表示存在嵌套依赖的状况,则采用递归的方法执行 $this->make($concrete),直到全部的都解析完为止。

$this->build($concrete)

/** * Instantiate a concrete instance of the given type. * * @param string $concrete * @return mixed * * @throws \Illuminate\Contracts\Container\BindingResolutionException */
public function build($concrete) {
    // 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, $this->getLastParameterOverride());
    }

    // 利用反射机制,解析该类。
    $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()) {
        return $this->notInstantiable($concrete);
    }

    $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)) {
        // 将 build 过程的内容 pop,而后直接构造对象输出。
        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.
    // 解析出全部上下文依赖对象,带入函数,构造对象输出
    $instances = $this->resolveDependencies(
        $dependencies
    );

    array_pop($this->buildStack);

    return $reflector->newInstanceArgs($instances);
}
复制代码

此方法分红两个分支:若是 $concrete instanceof Closure,则直接调用闭包函数,返回结果:$concrete();另外一种分支就是,传入的就是一个 $concrete === $abstract === 类名,经过反射方法,解析并 new 该类。

具体解释看上面的注释。 ReflectionClass 类的使用,具体参考:php.golaravel.com/class.refle…

代码往下看:

// 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) && ! $needsContextualBuild) {
    $this->instances[$abstract] = $object;
}

$this->fireResolvingCallbacks($abstract, $object);

// Before returning, we will also set the resolved flag to "true" and pop off
// the parameter overrides for this build. After those two things are done
// we will be ready to return back the fully constructed class instance.
$this->resolved[$abstract] = true;

array_pop($this->with);

return $object;
复制代码

这些就比较好理解了。主要判断是否存在扩展,则相应扩展功能;若是是绑定单例,则将解析的结果存到 $this->instances 数组中;最后作一些解析的「善后工做」。

最后咱们再看看 tag 标签的解析:

/** * Resolve all of the bindings for a given tag. * * @param string $tag * @return array */
public function tagged($tag) {
    $results = [];

    if (isset($this->tags[$tag])) {
        foreach ($this->tags[$tag] as $abstract) {
            $results[] = $this->make($abstract);
        }
    }

    return $results;
}
复制代码

如今看这个就更好理解了,若是传入的 tag 标签值存在 tags 数组中,则遍历全部 $abstract, 一一解析,将结果保存数组输出。

总结

虽然 Container 核心的内容咱们了解了,但还有不少细节值得咱们接着研究,如:$alias 相关的,事件相关的,扩展相关的。

最后收尾,推荐你们看看这个 Container:silexphp/Pimple

A small PHP 5.3 dependency injection container pimple.symfony.com

未完待续

相关文章
相关标签/搜索