版本5.1
namespace App\Http\Controllers;
use Cache;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
/**
* Show the profile for the given user.
*
* @param int $id
* @return Response
*/
public function showProfile($id)
{
$user = Cache::get('user:'.$id);
return view('profile', ['user' => $user]);
}
}
代碼中的 Cache 能夠被這樣直接使用因為使用了 Facade, 效果是通過 Cache 類使用靜態方法調用容器中綁定的相應的類解析出的實例的對應方法。
有點繞額。
首先,Cache 實際是 Illuminate\Support\Facades\Cache 的別名,通過 class_alias 函數實現的, 這個步驟稍后再看。
Illuminate\Support\Facades\Cache 里繼承 Illuminate\Support\Facades\Facade 并且實現了一個getFacadeAccessor方法。
protected static function getFacadeAccessor()
{
return 'cache';
}
回到 Cache::get('user:'.$id)
這個語句,當這個類沒有找到get靜態方法的時候,父類里的魔術方法會被調用。
public static function __callStatic($method, $args)
{
$instance = static::getFacadeRoot();
if (! $instance) {
throw new RuntimeException('A facade root has not been set.');
}
switch (count($args)) {
case 0:
return $instance->$method();
case 1:
return $instance->$method($args[0]);
case 2:
return $instance->$method($args[0], $args[1]);
case 3:
return $instance->$method($args[0], $args[1], $args[2]);
case 4:
return $instance->$method($args[0], $args[1], $args[2], $args[3]);
default:
return call_user_func_array([$instance, $method], $args);
}
}
getFacadeRoot 方法應該是要返回一個對象。
public static function getFacadeRoot()
{
return static::resolveFacadeInstance(static::getFacadeAccessor());
}
getFacadeAccessor()方法在子類里定義了,只是返回一個字符串cache。
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];
}
$app會賦值為容器,也就是 $app['cache'], $app的父類繼承了 ArrayAccess,于是 $app['cache'] 實際是調用了 $app->offsetGet('cache'), 實現 $app->make('cache')
在回到最初,Illuminate\Support\Facades\Cache 在哪里把 Cache 設置為別名,需要過一下 Laravel 的啟動過程??聪氯肟谖募?public/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()
);
$response->send();
$kernel->terminate($request, $response);
容器通過make Illuminate\Contracts\Http\Kernel::class 之后,得到是 App\Http\Kernel 的實例,下一句執行App\Http\Kernel 父類 Illuminate\Foundation\Http 中的handle方法,handle方法中調用了 sendRequestThroughRouter 方法, 這個方法里又執行 bootstrap 方法,這個方法是把需要啟動的類名數組傳遞給容器的 bootstrapWith 方法來執行。
看下數組
protected $bootstrappers = [
'Illuminate\Foundation\Bootstrap\DetectEnvironment',
'Illuminate\Foundation\Bootstrap\LoadConfiguration',
'Illuminate\Foundation\Bootstrap\ConfigureLogging',
'Illuminate\Foundation\Bootstrap\HandleExceptions',
'Illuminate\Foundation\Bootstrap\RegisterFacades',
'Illuminate\Foundation\Bootstrap\RegisterProviders',
'Illuminate\Foundation\Bootstrap\BootProviders',
];
里面的 Illuminate\Foundation\Bootstrap\RegisterFacades
,就是注冊Facades應該就是要找的類了。那傳入容器 Illuminate\Foundation\Application 中的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]);
}
}
$this->make('Illuminate\Foundation\Bootstrap\RegisterFacades')->bootstrap($this);
也就是解析得到 Illuminate\Foundation\Bootstrap\RegisterFacades 的實例,執行里面的 bootstrap 方法。
public function bootstrap(Application $app)
{
Facade::clearResolvedInstances();
Facade::setFacadeApplication($app);
AliasLoader::getInstance($app->make('config')->get('app.aliases'))->register();
}
看 setFacadeApplication($app) ,就是容器傳入facade類,賦值給里面的 $app。
$app->make('config')->get('app.aliases')
可以猜到是取 config/app.php 中的aliases,也就是別名和對應Facade類。
$app->make('config')返回的是一個 Illuminate\Config\Repository 對象,這個設置是在 bootstrapWith 調用 Illuminate\Foundation\Bootstrap\LoadConfiguration 的時候,暫且不說。
'aliases' => [
...
'Cache' => Illuminate\Support\Facades\Cache::class,
...
]
把這個數組傳入 Illuminate\Foundation\AliasLoader 的getInstance方法。
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;
}
就是把aliases賦值給了
public function register()
{
if (! $this->registered) {
$this->prependToLoaderStack();
$this->registered = true;
}
}
protected function prependToLoaderStack()
{
spl_autoload_register([$this, 'load'], true, true);
}
public function load($alias)
{
if (isset($this->aliases[$alias])) {
return class_alias($this->aliases[$alias], $alias);
}
}
就是用 spl_autoload_register 把 load 函數,放到了autoload的處理過程中,這個不管,看下 load 函數,就是那個數組里的Facade類都設置一個別名。