[英]Laravel dependency injection
I am creating a framework agnostic composer library that employs the methodologies of DI. 我正在创建一个使用DI方法的框架不可知的作曲家库。 I am running into an issue when trying to inject laravel's Cache object as a dependency into my library class. 尝试将laravel的Cache对象作为依赖项注入到我的库类中时遇到问题。 My library class signature is as follows: 我的库类签名如下:
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
}
What I would like to do is wrap Laravels cache for the driver I am using (memcache) and simply have it implement CacheableInterface
without having to redefine all the functions that are already available through the cache driver. 我想做的是为正在使用的驱动程序包装Laravels缓存(memcache),并简单地使其实现CacheableInterface
而不必重新定义通过缓存驱动程序已经可用的所有功能。 Something like this: 像这样:
class CacheWrapper extends Laravel\Cache implements CacheableInterface {
}
The problem I run into is when I extend Illuminate\\Cache\\Repository
it wants me to redefine all of the cache methods. 我遇到的问题是,当我扩展Illuminate\\Cache\\Repository
它希望我重新定义所有缓存方法。 Is there a way to retain the cache methods and extend the object so it implements my CacheableInterface
or is there a better way to architect the whole thing? 有没有办法保留缓存方法并扩展对象,以便它实现我的CacheableInterface
还是有更好的方法来构建整个对象?
I would create something like this (of course you'd have to adjust the parameters): 我会创建类似这样的东西(当然,您必须调整参数):
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.