前言
本文主要给大家介绍了关于Laravel中Facade加载过程与原理的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。
简介
Facades(读音:/fəˈsäd/ )为应用程序的 服务容器 中可用的类提供了一个「静态」接口。你不必 use 一大串的命名空间,也不用实例化对象,就能访问对象的具体方法。
1
2
3
4
5
6
7
8
9
|
use Config;
class Test
{
public function index()
{
return Config::get( 'app.name' );
}
}
|
Facade 的启动与注册
Facade 的启动引导是在 Illuminate\\Foundation\\Bootstrap\\RegisterFacades 中注册的。
1
2
3
4
5
6
7
8
9
10
|
public function bootstrap(Application $app )
{
Facade::clearResolvedInstances();
Facade::setFacadeApplication( $app );
AliasLoader::getInstance( array_merge (
$app ->make( 'config' )->get( 'app.aliases' , []),
$app ->make(PackageManifest:: class )->aliases()
))->register();
}
|
默认的别名配置是从 app 配置文件下的 aliases 读取的,PackageManifest 是 laravel 5.5 新增的 包自动发现 规则,这里我们暂时不考虑 PackageManifest 包提供的别名。
其中,array_merge 返回如下格式的数组:
1
2
3
4
5
|
"App" => "Illuminate\\Support\\Facades\\App"
"Artisan" => "Illuminate\\Support\\Facades\\Artisan"
"Auth" => "Illuminate\\Support\\Facades\\Auth"
"Blade" => "Illuminate\\Support\\Facades\\Blade"
...
|
上面代码将通过 AliasLoader 把所有的 facade 注册进自动加载。其核心就是 php 的 spl_autoload_register。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/**
* Prepend the load method to the auto-loader stack.
*
* @return void
*/
protected function register()
{
if (! $this ->registered) {
spl_autoload_register([ $this , 'load' ], true, true);
$this ->registered = true;
}
}
|
注册完成后,后续所有 use 的类都将通过 load 函数来完成类的自动加载。
注意:这里在定义 spl_autoload_register 时,最后面的参数传的是 true。当该参数是 true 时,spl_autoload_register() 会添加函数到队列之首,而不是队列尾部。(优先通过该函数来完成自动加载)
也就是说,
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?php
use Config;
use App\\User;
class Test
{
public function index()
{
Config::get( 'app.name' );
new User();
}
}
|
不管我们 use 的是具体存在的类(App\\User)还是别名 (Config),都将最先通过 load 函数来完成自动加载,当该函数返回 false 时,再由其他自动加载函数来完成自动加载(如 composer psr-4)。
在 AliasLoader 的 load 方法中,主要是用了 class_alias 函数来实现的别名自动加载。
1
2
3
4
5
6
|
public function load( $alias )
{
if (isset( $this ->aliases[ $alias ])) {
return class_alias( $this ->aliases[ $alias ], $alias );
}
}
|
关于 class_alias 这里帖一个官方的列子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
class foo { }
class_alias( 'foo' , 'bar' );
$a = new foo;
$b = new bar;
// the objects are the same
var_dump( $a == $b , $a === $b ); //true
var_dump( $a instanceof $b ); //false
// the classes are the same
var_dump( $a instanceof foo); //true
var_dump( $a instanceof bar); //true
var_dump( $b instanceof foo); //true
var_dump( $b instanceof bar); //true
|
Facade 的加载
当我们在使用 Facade 时,如:
1
2
3
4
5
6
7
8
9
10
11
|
<?php
use Config;
class Test
{
public function index()
{
Config::get( 'app.name' );
}
}
|
实际上加载的是 Illuminate\\Support\\Facades\\Config 类(因为我们已经注册了 class_alias),相当于:
1
2
3
4
5
6
7
8
9
10
11
|
<?php
use Illuminate\\Support\\Facades\\Config;
class Test
{
public function index()
{
Config::get( 'app.name' );
}
}
|
而所有的 Facade 都继承自 Illuminate\\Support\\Facades\\Facade 类,在该基类中定义了一个 __callStatic 方法,已至于我们能够轻松地使用 Facade(不用实列化)。
1
2
3
4
5
6
7
8
9
10
11
12
|
<?php
public static function __callStatic( $method , $args )
{
$instance = static ::getFacadeRoot();
if (! $instance ) {
throw new RuntimeException( 'A facade root has not been set.' );
}
return $instance -> $method (... $args );
}
|
getFacadeRoot 方法用于获取别名类的具体实列,我们知道,所有的 Facade 类都需要定义一个 getFacadeAccessor 方法。该方法可能的返回值有:
- String 类型的字符串(如 config, db)
- String 类型的类字符串 (如 App\\Service\\SomeService)
- Object 具体的实列化对象
- Closure 闭包
如 Config Facade 的 getFacadeAccessor 方法如下:
1
2
3
4
|
protected static function getFacadeAccessor()
{
return 'config' ;
}
|
getFacadeRoot 方法将根据 getFacadeAccessor()
的返回值,从容器从取出对应的实列对象。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public static function getFacadeRoot()
{
$name = static ::getFacadeAccessor();
if ( is_object ( $name )) {
return $name ;
}
if (isset( static :: $resolvedInstance [ $name ])) {
return static :: $resolvedInstance [ $name ];
}
return static :: $resolvedInstance [ $name ] = static :: $app [ $name ];
}
|
由于 APP 容器中已经注册过 config 的实列
1
2
3
4
|
<?php
//Illuminate\\Foundation\\Bootstrap/LoadConfiguration
$app ->instance( 'config' , $config = new Repository( $items ));
|
所以 \\Config::get('app.name', 'dafault)
实际访问的是 Repository 实列的 get('app.name', 'default')
方法。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对快网idc的支持。
原文链接:https://segmentfault.com/a/1190000011274118
相关文章
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-29 94
-
centos 5.1下的安全设置(适合所有的linux版本)
2025-05-27 50 -
2025-06-04 59
-
2025-05-25 96
-
CentOS 6.X如何更改网卡名称?CentOS 6.X更改网卡名称的方法
2025-05-27 86