草稿 8 Laravel Facade 门面类加载原理

Illuminte\Foundation\Bootstrap\RegisterFacades.php

public function bootstrap(Application $app)
{
        Facade::clearResovedInstances();
        Facade::setFacadeApplication($app);
        AliasLoader::getInstance($app->make('config')->get('app.aliases'))->register();
}

Illuminate\Foundation\AliasLoader.php

// 外观别名类数组
protected $aliases;

public static function getInstance( array $aliases = [])
{
        if(is_null(static::$instance)){
            return static::$instance = new static($aliases);
        }
        $aliases = array_merge(static::$instance->getAliases(), $aliases);
        static::$instance->setAliases($aliases);
        return static::$instance;
}

public function register()
{
        if(!$this->registered){
            $this->prependToLoaderStack();
            $this->registered = true;
        }
}

public function prependToLoaderStack()
{
        spl_autoload_register([$this, 'load'], true, true);
}

public function load($alias)
{
        if(isset($this->aliases[$alias])){
            class_alias($this->aliases[$alias], $alias);
        }       
}

class_alias (string $original , string $alias [, bool $autoload = TRUE] ) : bool。为一个类创建别名。比如给 Illuminate\Support\Facades\Route 类起个 Route 的别名,class_alias ('Illuminate\Support\Facades\Route', 'Route') 这样就行了。这么做的好处是根域名中的 Route 就表示命名空间为 Illuminate\Support\Facades 的 Route 类。

外观注册是通过 RegisterFacades 类的 bootstrap () 函数实现的。实现门面类其实分为两个步骤。1. 是实例话外观自动加载类 Illuminate\Foundation\Aliasloader::class 并把外观别名数组添加到该实例中,。2. 完成自动加载类中的自动加载函数的添加。用 getInstance () 单例模式实例话外观自动加载类。再用 $app->make ('config')->get ('app.aliases') 来获取外观类数组。这句代码就用到了配置加载的内容,即获取加载的 app.php 的配置文件,返回数组的键为”aliases” 的数组值。
在自动加载类实例话以后,会调用 register () 函数向自动加载栈的开始处加入一个新的加载函数,即通过代码 spl_autoload_register ([$this, 'load'], true, true) 实现。所有类的自动加载都先经过这个函数。这个函数就是 AliasLoader 类实例的 load () 函数。此函数的作用是为外观类设置一个别名。即外观别名,当用户通过外观别名访问类时,实际上访问的是对应的外观类。下面我们就看看通过外观别名怎实现类似 Route::get (' 路径 ',' 响应函数 ') 这样的函数调用。

首先程序会调用 Route 类,由于注册了外观别名。那么会自动加载第一个函数是 AliasLoader 类的 load () 函数。该函数会查找别名对应类。于是便找到 Route 的外观别名是 Illuminate\Support\Facades\Route 类,并加载这个类接着调用该类的 get () 静态函数。可是这个类没有对应的函数。不过它继承了 Illuminate\Support\Facades\Facade 这个类。该类也没有对应的静态函数,不过该类有__callstatic () 函数。这个函数是在没有查询到对应的静态函数时候会被调用。__callstatic () 函数里面有个 $instance = static::getFacadeRoot () 来获取对应类的实例,其中 getFacadeRoot () 静态函数是根据 Illuminate\Support\Facades\Route 类的 getFacadeAccessor () 函数来实例话对应的 Route 类实例。最后调用 Route 实例的 get 函数,流程完毕。下面是相应的代码。

Illuminate\Support\Facades\Facade.php

public function getFacadeRoot()
{
        return static::resolveFacadeInstance(static::getFacadeAccessor());
}

public function resolveFacadeInstance($name)
{
        // 这里不用看
        if(is_object($name)){
            return $name;
        }

        if(isset(static::resolvedInstance[$name])){
            return static::resolvedInstance[$name];
        }       
        // 主要看这里,static::$app[$name]这个是以数组的形式实例话类。等同于,$app->make($name);
        return static::$resolvedInstance[$name] = static::$app[$name]
}

public static function __callStatic($method, $args)
{
        $instance = static::getFacadeRoot();
        if (! $instance) {
            throw new RuntimeException('A facade root has not been set.');
      }
//    return call_user_func_array([$instance, $method], $args);
        return $instance->$method(...$args);
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。