Facades(讀音:/fəˈsäd/ )爲應用程序的
服務容器
中可用的類提供了一個「靜態」接口。你沒必要use
一大串的命名空間,也不用實例化對象,就能訪問對象的具體方法。php
use Config; class Test { public function index() { return Config::get('app.name'); } }
Facade 的啓動引導是在 Illuminate\Foundation\Bootstrap\RegisterFacades
中註冊的。laravel
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
包提供的別名。bootstrap
其中,array_merge
返回以下格式的數組:數組
"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
。bash
/** * 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()
會添加函數到隊列之首,而不是隊列尾部。(優先經過該函數來完成自動加載)app
也就是說,composer
<?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
函數來實現的別名自動加載。this
public function load($alias) { if (isset($this->aliases[$alias])) { return class_alias($this->aliases[$alias], $alias); } }
關於 class_alias
這裏帖一個官方的列子:
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
時,如:
<?php use Config; class Test { public function index() { Config::get('app.name'); } }
實際上加載的是 Illuminate\Support\Facades\Config
類(由於咱們已經註冊了 class_alias
),至關於:
<?php use Illuminate\Support\Facades\Config; class Test { public function index() { Config::get('app.name'); } }
而全部的 Facade
都繼承自 Illuminate\Support\Facades\Facade
類,在該基類中定義了一個 __callStatic
方法,已至於咱們可以輕鬆地使用 Facade
(不用實列化)。
<?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
方法。該方法可能的返回值有:
config
, db
)App\Service\SomeService
)如 Config Facade
的 getFacadeAccessor
方法以下:
protected static function getFacadeAccessor() { return 'config'; }
getFacadeRoot
方法將根據 getFacadeAccessor()
的返回值,從容器從取出對應的實列對象。
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
的實列
<?php //Illuminate\Foundation\Bootstrap/LoadConfiguration $app->instance('config', $config = new Repository($items));
因此 \Config::get('app.name', 'dafault)
實際訪問的是 Repository
實列的 get('app.name', 'default')
方法。