在开始以前,欢迎关注我本身的博客:www.leoyang90.cn
这篇文章咱们开始讲 laravel 框架中的门面 Facade,什么是门面呢?官方文档:php
Facades(读音:/fəˈsäd/ )为应用程序的服务容器中可用的类提供了一个「静态」接口。Laravel 自带了不少 facades ,几乎能够用来访问到 Laravel 中全部的服务。Laravel facades 其实是服务容器中那些底层类的「静态代理」,相比于传统的静态方法, facades 在提供了简洁且丰富的语法同时,还带来了更好的可测试性和扩展性。laravel
什么意思呢?首先,咱们要知道 laravel 框架的核心就是个 Ioc 容器即 服务容器,功能相似于一个工厂模式,是个高级版的工厂。laravel 的其余功能例如路由、缓存、日志、数据库其实都是相似于插件或者零件同样,叫作 服务。Ioc 容器主要的做用就是生产各类零件,就是提供各个服务。在 laravel 中,若是咱们想要用某个服务,该怎么办呢?最简单的办法就是调用服务容器的 make 函数,或者利用依赖注入,或者就是今天要讲的门面 Facade。门面相对于其余方法来讲,最大的特色就是简洁,例如咱们常用的 Router,若是利用服务容器的 make:数据库
App::make('router')->get('/', function () { return view('welcome'); });
若是利用门面:bootstrap
Route::get('/', function () { return view('welcome'); });
能够看出代码更加简洁。其实,下面咱们就会介绍门面最后调用的函数也是服务容器的 make 函数。segmentfault
咱们以 Route 为例,来说解一下门面 Facade 的原理与实现。咱们先来看 Route 的门面类:数组
class Route extends Facade { protected static function getFacadeAccessor() { return 'router'; } }
很简单吧?其实每一个门面类也就是重定义一下 getFacadeAccessor 函数就好了,这个函数返回服务的惟一名称:router。须要注意的是要确保这个名称能够用服务容器的 make 函数建立成功(App::make('router')),缘由咱们立刻就会讲到。
那么当咱们写出 Route::get() 这样的语句时,到底发生了什么呢?奥秘就在基类 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); }
当运行 Route::get() 时,发现门面 Route 没有静态 get() 函数,PHP 就会调用这个魔术函数 __callStatic。咱们看到这个魔术函数作了两件事:得到对象实例,利用对象调用 get() 函数。首先先看看如何得到对象实例的:app
public static function getFacadeRoot() { return static::resolveFacadeInstance(static::getFacadeAccessor()); } protected static function getFacadeAccessor() { throw new RuntimeException('Facade does not implement getFacadeAccessor method.'); } protected static function resolveFacadeInstance($name) { if (is_object($name)) { return $name; } if (isset(static::$resolvedInstance[$name])) { return static::$resolvedInstance[$name]; } return static::$resolvedInstance[$name] = static::$app[$name]; }
咱们看到基类 getFacadeRoot() 调用了 getFacadeAccessor(),也就是咱们的服务重载的函数,若是调用了基类的 getFacadeAccessor,就会抛出异常。在咱们的例子里 getFacadeAccessor() 返回了 “router”,接下来 getFacadeRoot() 又调用了 resolveFacadeInstance()。在这个函数里重点就是composer
return static::$resolvedInstance[$name] = static::$app[$name];
咱们看到,在这里利用了 app 也就是服务容器建立了 “router”,建立成功后放入 resolvedInstance做为缓存,以便之后快速加载。
好了,Facade 的原理到这里就讲完了,可是到这里咱们有个疑惑,为何代码中写 Route 就能够调用 IlluminateSupportFacadesRoute 呢?这个就是别名的用途了,不少门面都有本身的别名,这样咱们就没必要在代码里面写 use IlluminateSupportFacadesRoute,而是能够直接用 Route 了。框架
为何咱们能够在 larval 中全局用 Route,而不须要使用 use IlluminateSupportFacadesRoute?其实奥秘在于一个 PHP 函数:class_alias,它能够为任何类建立别名。larval 在启动的时候为各个门面类调用了 class_alias 函数,所以没必要直接用类名,直接用别名便可。在 config 文件夹的 app 文件里面存放着门面与类名的映射:
'aliases' => [ 'App' => Illuminate\Support\Facades\App::class, 'Artisan' => Illuminate\Support\Facades\Artisan::class, 'Auth' => Illuminate\Support\Facades\Auth::class, ... ]
下面咱们来看看 laravel 是如何为门面类建立别名的。
说到 larval 的启动,咱们离不开 index.php:
require __DIR__.'/../bootstrap/autoload.php'; $app = require_once __DIR__.'/../bootstrap/app.php'; $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class); $response = $kernel->handle( $request = Illuminate\Http\Request::capture() ); ...
第一句就是咱们前面博客说的 composer 的自动加载,接下来第二句获取 laravel 核心的 Ioc 容器,第三句“制造”出 Http 请求的内核,第四句是咱们这里的关键,这句牵扯很大,laravel 里面全部功能服务的注册加载,乃至 Http 请求的构造与传递都是这一句的功劳。
$request = Illuminate\Http\Request::capture()
这句是 laravel 经过全局 _SERVER 数组构造一个 Http 请求的语句,接下来会调用 Http 的内核函数 handle:
public function handle($request) { try { $request->enableHttpMethodParameterOverride(); $response = $this->sendRequestThroughRouter($request); } catch (Exception $e) { $this->reportException($e); $response = $this->renderException($request, $e); } catch (Throwable $e) { $this->reportException($e = new FatalThrowableError($e)); $response = $this->renderException($request, $e); } event(new Events\RequestHandled($request, $response)); return $response; }
在 handle 函数方法中 enableHttpMethodParameterOverride 函数是容许在表单中使用 delete、put 等类型的请求。咱们接着看 sendRequestThroughRouter:
protected function sendRequestThroughRouter($request) { $this->app->instance('request', $request); Facade::clearResolvedInstance('request'); $this->bootstrap(); return (new Pipeline($this->app)) ->send($request) ->through($this->app->shouldSkipMiddleware() ? [] : $this->middleware) ->then($this->dispatchToRouter()); }
前两句是在 larval 的 Ioc 容器设置 request 请求的对象实例,Facade 中清楚 request 的缓存实例。bootstrap:
public function bootstrap() { if (! $this->app->hasBeenBootstrapped()) { $this->app->bootstrapWith($this->bootstrappers()); } } protected $bootstrappers = [ \Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class, \Illuminate\Foundation\Bootstrap\LoadConfiguration::class, \Illuminate\Foundation\Bootstrap\HandleExceptions::class, \Illuminate\Foundation\Bootstrap\RegisterFacades::class, \Illuminate\Foundation\Bootstrap\RegisterProviders::class, \Illuminate\Foundation\Bootstrap\BootProviders::class, ];
$bootstrappers 是 Http 内核里专门用于启动的组件,bootstrap 函数中调用 Ioc 容器的 bootstrapWith 函数来建立这些组件并利用组件进行启动服务。app->bootstrapWith:
public function bootstrapWith(array $bootstrappers) { $this->hasBeenBootstrapped = true; foreach ($bootstrappers as $bootstrapper) { $this['events']->fire('bootstrapping: '.$bootstrapper, [$this]); $this->make($bootstrapper)->bootstrap($this); $this['events']->fire('bootstrapped: '.$bootstrapper, [$this]); } }
能够看到 bootstrapWith 函数也就是利用 Ioc 容器建立各个启动服务的实例后,回调启动本身的函数 bootstrap,在这里咱们只看咱们 Facade 的启动组件
\Illuminate\Foundation\Bootstrap\RegisterFacades::class
RegisterFacades 的 bootstrap 函数:
class RegisterFacades { public function bootstrap(Application $app) { Facade::clearResolvedInstances(); Facade::setFacadeApplication($app); AliasLoader::getInstance($app->make('config')->get('app.aliases', []))->register(); } }
能够看出来,bootstrap 作了一下几件事:
清除了 Facade 中的缓存
设置 Facade 的 Ioc 容器
得到咱们前面讲的 config 文件夹里面 app 文件 aliases 别名映射数组
使用 aliases 实例化初始化 AliasLoader
调用 AliasLoader->register()
public function register() { if (! $this->registered) { $this->prependToLoaderStack(); $this->registered = true; } } protected function prependToLoaderStack() { spl_autoload_register([$this, 'load'], true, true); }
咱们能够看出,别名服务的启动关键就是这个 spl_autoload_register,这个函数咱们应该很熟悉了,在自动加载中这个函数用于解析命名空间,在这里用于解析别名的真正类名。
咱们首先来看看被注册到 spl_autoload_register 的函数,load:
public function load($alias) { if (static::$facadeNamespace && strpos($alias, static::$facadeNamespace) === 0) { $this->loadFacade($alias); return true; } if (isset($this->aliases[$alias])) { return class_alias($this->aliases[$alias], $alias); } }
这个函数的下面很好理解,就是 class_alias 利用别名映射数组将别名映射到真正的门面类中去,可是上面这个是什么呢?实际上,这个是 laravel5.4 版本新出的功能叫作实时门面服务。
其实门面功能已经很简单了,咱们只须要定义一个类继承 Facade 便可,可是 laravel5.4 打算更近一步——自动生成门面子类,这就是实时门面。
实时门面怎么用?看下面的例子:
namespace App\Services; class PaymentGateway { protected $tax; public function __construct(TaxCalculator $tax) { $this->tax = $tax; } }
这是一个自定义的类,若是咱们想要为这个类定义一个门面,在 laravel5.4 咱们能够这么作:
use Facades\ { App\Services\PaymentGateway }; Route::get('/pay/{amount}', function ($amount) { PaymentGateway::pay($amount); });
那么这么作的原理是什么呢?咱们接着看源码:
protected static $facadeNamespace = 'Facades\\'; if (static::$facadeNamespace && strpos($alias, static::$facadeNamespace) === 0) { $this->loadFacade($alias); return true; }
若是命名空间是以 Facades\ 开头的,那么就会调用实时门面的功能,调用 loadFacade 函数:
protected function loadFacade($alias) { tap($this->ensureFacadeExists($alias), function ($path) { require $path; }); }
tap 是 laravel 的全局帮助函数,ensureFacadeExists 函数负责自动生成门面类,loadFacade 负责加载门面类:
protected function ensureFacadeExists($alias) { if (file_exists($path = storage_path('framework/cache/facade-'.sha1($alias).'.php'))) { return $path; } file_put_contents($path, $this->formatFacadeStub( $alias, file_get_contents(__DIR__.'/stubs/facade.stub') )); return $path; }
能够看出来,laravel 框架生成的门面类会放到 stroge/framework/cache/ 文件夹下,名字以 facade 开头,以命名空间的哈希结尾。若是存在这个文件就会返回,不然就要利用 file_put_contents 生成这个文件,formatFacadeStub:
protected function formatFacadeStub($alias, $stub) { $replacements = [ str_replace('/', '\\', dirname(str_replace('\\', '/', $alias))), class_basename($alias), substr($alias, strlen(static::$facadeNamespace)), ]; return str_replace( ['DummyNamespace', 'DummyClass', 'DummyTarget'], $replacements, $stub ); }
简单的说,对于 Facades\App\Services\PaymentGateway
,replacements
第一项是门面命名空间,将 Facades\App\Services\PaymentGateway
转为 Facades/App/Services/PaymentGateway
,取前面 Facades/App/Services/
,再转为命名空间 Facades\App\Services\
;第二项是门面类名,PaymentGateway
;第三项是门面类的服务对象,App\Services\PaymentGateway
,用这些来替换门面的模板文件:
<?php namespace DummyNamespace; use Illuminate\Support\Facades\Facade; /** * @see \DummyTarget */ class DummyClass extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'DummyTarget'; } }
替换后的文件是:
<?php namespace Facades\App\Services\; use Illuminate\Support\Facades\Facade; /** * @see \DummyTarget */ class PaymentGateway extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'App\Services\PaymentGateway'; } }
就是这么简单!!!
门面的原理就是这些,相对来讲门面服务的原理比较简单,和自动加载相互配合使得代码更加简洁,但愿你们能够更好的使用这些门面!