知人者智,自知者明。胜人者有力,自胜者强。知足者富。强行者有志。——《老子·道经·第三十三章》
最近笔者正在理解Larvael服务容器、服务提供者、门面、契约的关系。
今天主要是记录自己借Laravel自带的Cache模块进行一个讲解。
代码和流程
首先我们打开config\app.php
中providers
下有一个服务提供者Illuminate\Cache\CacheServiceProvider::class
我们看看他的服务提供者是怎么写的:
public function register()
{
$this->app->singleton('cache', function ($app) {
return new CacheManager($app);
});
$this->app->singleton('cache.store', function ($app) {
return $app['cache']->driver();
});
$this->app->singleton('cache.psr6', function ($app) {
return new Psr16Adapter($app['cache.store']);
});
$this->app->singleton('memcached.connector', function () {
return new MemcachedConnector;
});
}
我们主要看cache
这个门面,它所单例绑定的是CacheManager
,我们继续深入
<?php
namespace Illuminate\Cache;
use Aws\DynamoDb\DynamoDbClient;
use Closure;
use Illuminate\Contracts\Cache\Factory as FactoryContract;
use Illuminate\Contracts\Cache\Store;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Support\Arr;
use InvalidArgumentException;
/**
* @mixin \Illuminate\Contracts\Cache\Repository
*/
class CacheManager implements FactoryContract
{
/**
* The application instance.
*
* @var \Illuminate\Contracts\Foundation\Application
*/
protected $app;
/**
* The array of resolved cache stores.
*
* @var array
*/
protected $stores = [];
/**
* The registered custom driver creators.
*
* @var array
*/
protected $customCreators = [];
/**
* Create a new Cache manager instance.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @return void
*/
public function __construct($app)
{
$this->app = $app;
}
/**
* Get a cache store instance by name, wrapped in a repository.
*
* @param string|null $name
* @return \Illuminate\Contracts\Cache\Repository
*/
public function store($name = null)
{
$name = $name ?: $this->getDefaultDriver();
return $this->stores[$name] = $this->get($name);
}
/**
* Get a cache driver instance.
*
* @param string|null $driver
* @return \Illuminate\Contracts\Cache\Repository
*/
public function driver($driver = null)
{
return $this->store($driver);
}
/**
* Attempt to get the store from the local cache.
*
* @param string $name
* @return \Illuminate\Contracts\Cache\Repository
*/
protected function get($name)
{
return $this->stores[$name] ?? $this->resolve($name);
}
/**
* Resolve the given store.
*
* @param string $name
* @return \Illuminate\Contracts\Cache\Repository
*
* @throws \InvalidArgumentException
*/
protected function resolve($name)
{
$config = $this->getConfig($name);
if (is_null($config)) {
throw new InvalidArgumentException("Cache store [{$name}] is not defined.");
}
if (isset($this->customCreators[$config['driver']])) {
return $this->callCustomCreator($config);
} else {
$driverMethod = 'create'.ucfirst($config['driver']).'Driver';
if (method_exists($this, $driverMethod)) {
return $this->{$driverMethod}($config);
} else {
throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported.");
}
}
}
/**
* Call a custom driver creator.
*
* @param array $config
* @return mixed
*/
protected function callCustomCreator(array $config)
{
return $this->customCreators[$config['driver']]($this->app, $config);
}
......
/**
* Create an instance of the file cache driver.
*
* @param array $config
* @return \Illuminate\Cache\Repository
*/
protected function createFileDriver(array $config)
{
return $this->repository(new FileStore($this->app['files'], $config['path']));
}
/**
* Create a new cache repository with the given implementation.
*
* @param \Illuminate\Contracts\Cache\Store $store
* @return \Illuminate\Cache\Repository
*/
public function repository(Store $store)
{
return tap(new Repository($store), function ($repository) {
$this->setEventDispatcher($repository);
});
}
/**
* Set the event dispatcher on the given repository instance.
*
* @param \Illuminate\Cache\Repository $repository
* @return void
*/
protected function setEventDispatcher(Repository $repository)
{
if (! $this->app->bound(DispatcherContract::class)) {
return;
}
$repository->setEventDispatcher(
$this->app[DispatcherContract::class]
);
}
......
/**
* Get the cache connection configuration.
*
* @param string $name
* @return array
*/
protected function getConfig($name)
{
return $this->app['config']["cache.stores.{$name}"];
}
......
/**
* Dynamically call the default driver instance.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
return $this->store()->$method(...$parameters);
}
}
我们以Laravel默认的File存储讲起:
- 当我们的控制器中使用
Cache::get()
,框架查找门面Illuminate\Support\Facades\Cache
,返回一个叫cache
的服务提供商 - 框架查找叫
cache
的服务提供商,实例化CacheManager()
- 实例化
CacheManager
调用get
方法,发现没有关于get的静态函数,于是触发了__call
魔术方法 __call
调用函数store
store
判断是否需要携带参数get
方法判断是否需要从配置中读取,并进行FileStore
的实例化,如果不需要直接返回store[]
中的实例化过的FileStore::class
FileStore
实现了契约Contracts\Cahce\Store
createXXXDriver
中发现返回的是Cache\Repository
,并发现使用了控制反转
,注入了不同的Store总结
这样的设计控制器不需要去管理实现的是哪一种Cache,是file、redis还是memcached,同时
Cache\Repository
也规定了必须注入满足契约的实现类,规范整体设计和方法,上层无感知切换。