laravel中路由、缓存等常常有Rote::get()、Cache::get()这样的写法,引入对应门面类就可以使用所定义方法,然而打开所引用门面类,并没有这些方法呀,那么它是怎样实现的呢?以及为何要这么写呢?
涉及知识点:重载、延迟静态绑定、外观模式。
我以非常常用的Cache门面为例追一下代码。
打开Cache门面类,发现只有一个方法,返回了一个名字。
class Cache extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'cache';
}
}
不知所云吗?别急,它继承了Facade,打开看看,翻一下发现有魔术方法
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);
}
当我们调用Cache::get(),很显然Cache门面和Facade都不存在静态的get方法,由此此魔术方法被触发,这是非常经典的使用场景,static::又是延迟静态绑定的写法,是要用调用域的getFacadeRoot()方法,追一下。
public static function getFacadeRoot()
{
return static::resolveFacadeInstance(static::getFacadeAccessor());
}
发现它用到了static::getFacadeAccessor(),也是延迟静态绑定的用法,咱们现在是Cache门面,返回的是cache,可以查看一下路由门面返回的是router。
protected static function resolveFacadeInstance($name)
{
if (is_object($name)) {
return $name;
}
if (isset(static::$resolvedInstance[$name])) {
return static::$resolvedInstance[$name];
}
if (static::$app) {
return static::$resolvedInstance[$name] = static::$app[$name];
}
}
Facade类定义的属性包括:
/**
* The application instance being facaded.
*
* @var \Illuminate\Contracts\Foundation\Application
*/
protected static $app;
/**
* The resolved object instances.
*
* @var array
*/
protected static $resol