From 38e0b4d3095c5e21f3645dcbf445cba68433b6b9 Mon Sep 17 00:00:00 2001 From: Grzegorz Rola <grola@seostudio.pl> Date: Wed, 12 Jun 2019 12:38:41 +0000 Subject: [PATCH] Feature/cache resolvers --- composer.json | 2 +- src/Client/CachedClient.php | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 920fd25..82b0404 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "require": { "php": ">=5.5", "psr/log": "^1.0.1", - "wpdesk/wp-cache": "^1.0", + "wpdesk/wp-cache": "dev-feature/cache-resolver-creator", "wpdesk/wp-http-client": "^1.0", "psr/simple-cache": "^1.0" }, diff --git a/src/Client/CachedClient.php b/src/Client/CachedClient.php index 3c6ee45..0a9b26c 100644 --- a/src/Client/CachedClient.php +++ b/src/Client/CachedClient.php @@ -4,6 +4,7 @@ namespace WPDesk\ApiClient\Client; use Psr\SimpleCache\CacheInterface; use WPDesk\Cache\CacheDispatcher; +use WPDesk\Cache\CacheInfoResolverCreator; use WPDesk\Cache\CacheItemCreator; use WPDesk\Cache\CacheItemVerifier; use WPDesk\HttpClient\HttpClient; @@ -27,14 +28,29 @@ class CachedClient implements Client, CacheItemCreator, CacheItemVerifier /** * CachedClient constructor. + * * @param Client $decorated Decorated client * @param CacheInterface $cache */ public function __construct(Client $decorated, CacheInterface $cache) { $this->client = $decorated; - $this->cache = $cache; - $this->cacheDispatcher = new CacheDispatcher($cache, [new RequestCacheInfoResolver()]); + $this->cache = $cache; + $this->cacheDispatcher = new CacheDispatcher($cache, $this->getCacheInfoResolvers()); + } + + /** + * Get cache info resolvers. + * + * @return RequestCacheInfoResolver[] + */ + protected function getCacheInfoResolvers() + { + if ($this->client instanceof CacheInfoResolverCreator) { + return $this->client->createResolvers(); + } else { + return [new RequestCacheInfoResolver()]; + } } /** -- GitLab