Php 拉维依赖注入
我正在创建一个框架无关的composer库,它采用DI的方法。我在尝试将laravel的缓存对象作为依赖项注入我的库类时遇到了一个问题。我的图书馆类签名如下:Php 拉维依赖注入,php,caching,laravel,dependency-injection,Php,Caching,Laravel,Dependency Injection,我正在创建一个框架无关的composer库,它采用DI的方法。我在尝试将laravel的缓存对象作为依赖项注入我的库类时遇到了一个问题。我的图书馆类签名如下: class Requester { const ACCESS_TOKEN_CACHE_KEY = 'access-token-cache-key'; const ACCESS_TOKEN_CACHE_TTL = 36000; protected $clientId; protected $clientS
class Requester {
const ACCESS_TOKEN_CACHE_KEY = 'access-token-cache-key';
const ACCESS_TOKEN_CACHE_TTL = 36000;
protected $clientId;
protected $clientSecret;
/** @var Client */
protected $guzzleClient;
/** @var CacheableInterface */
protected $cache;
/** @var string */
protected $accessToken;
public function __construct(
ClientFactory $clientFactory,
$clientId, $clientSecret,
CacheableInterface $cache = null,
$accessToken = null
)
{
$this->guzzleClient = $clientFactory->createClient();
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
$this->cache = $cache;
$this->accessToken = $accessToken;
}
// other methods
}
我想做的是为我正在使用的驱动程序(memcache)包装Laravels缓存,并简单地让它实现CacheableInterface
,而不必重新定义缓存驱动程序已经提供的所有功能。大概是这样的:
class CacheWrapper extends Laravel\Cache implements CacheableInterface {
}
我遇到的问题是,当我扩展
illighted\Cache\Repository
时,它希望我重新定义所有的缓存方法。有没有办法保留缓存方法并扩展对象,使其实现我的CacheableInterface
,或者有更好的方法来构建整个过程?我会创建类似的内容(当然,您必须调整参数):
class CacheWrapper implements CacheableInterface {
/**
* The laravel's driver you're using right now
*/
private $laravelDriver;
public function __construct(TypeHint $cacheDriver) {
$this->laravelDriver = $cacheDriver;
}
/**
* interface method
*/
public function cacheMethod1() {
$this->laravelDriver->analogousMethod1();
}
/**
* interface method
*/
public function cacheMethod2() {
$this->laravelDriver->analogousMethod2();
}
}