Facade of principle and call process
Creating a class CacheManager.php (by default all components are in the same directory)
Creating a provider CacheServiceProvider
class CacheServiceProvider extends ServiceProvider { public function register() { $this->app->singleton('cache',function($app){ return new CacheManager($app); }); } }
Service container ($ this-> app) Binding services (CacheManager) in the service provider (ServiceProvider)
The singleton class binding to the vessel
Provider in config / app.php Register Service
'providers' => [ /* * Laravel Framework Service Providers... */ 'Illuminate\Cache\CacheServiceProvider', ]
Creating facades, call the method container
class Cache extends Facade { protected static function getFacadeAccessor() { return 'cache'; } }
Facade base class
All Menlian classes are inherited from the Facade class, then the base class defines a method __callStatic that we can easily use 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); }
public static function getFacadeRoot() { return static::resolveFacadeInstance(static::getFacadeAccessor()); }
A method according to getFacadeRoot getFacadeAccessor () Returns the value of the corresponding instance of an object taken from the container.
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]; }