|
3 | 3 |
|
4 | 4 | use Psr\Cache\CacheItemPoolInterface; |
5 | 5 | use ScriptFUSION\Porter\Cache\CacheKeyGenerator; |
6 | | -use ScriptFUSION\Porter\Cache\CacheToggle; |
7 | 6 | use ScriptFUSION\Porter\Cache\InvalidCacheKeyException; |
8 | 7 | use ScriptFUSION\Porter\Cache\JsonCacheKeyGenerator; |
9 | 8 | use ScriptFUSION\Porter\Cache\MemoryCache; |
10 | | -use ScriptFUSION\Porter\Options\EncapsulatedOptions; |
11 | 9 |
|
12 | 10 | /** |
13 | | - * Caches remote data using PSR-6-compliant objects. |
| 11 | + * Wraps a connector to cache fetched data using PSR-6-compliant objects. |
14 | 12 | */ |
15 | | -abstract class CachingConnector implements Connector, CacheToggle |
| 13 | +class CachingConnector implements Connector, ConnectorWrapper |
16 | 14 | { |
17 | | - const RESERVED_CHARACTERS = '{}()/\@:'; |
18 | | - |
19 | 15 | /** |
20 | | - * @var CacheItemPoolInterface |
| 16 | + * @var Connector |
21 | 17 | */ |
22 | | - private $cache; |
| 18 | + private $connector; |
23 | 19 |
|
24 | 20 | /** |
25 | | - * @var bool |
| 21 | + * @var CacheItemPoolInterface |
26 | 22 | */ |
27 | | - private $cacheEnabled = true; |
| 23 | + private $cache; |
28 | 24 |
|
29 | 25 | /** |
30 | 26 | * @var CacheKeyGenerator |
31 | 27 | */ |
32 | 28 | private $cacheKeyGenerator; |
33 | 29 |
|
34 | | - public function __construct(CacheItemPoolInterface $cache = null, CacheKeyGenerator $cacheKeyGenerator = null) |
35 | | - { |
| 30 | + public function __construct( |
| 31 | + Connector $connector, |
| 32 | + CacheItemPoolInterface $cache = null, |
| 33 | + CacheKeyGenerator $cacheKeyGenerator = null |
| 34 | + ) { |
| 35 | + $this->connector = $connector; |
36 | 36 | $this->cache = $cache ?: new MemoryCache; |
37 | 37 | $this->cacheKeyGenerator = $cacheKeyGenerator ?: new JsonCacheKeyGenerator; |
38 | 38 | } |
39 | 39 |
|
| 40 | + public function __clone() |
| 41 | + { |
| 42 | + $this->connector = clone $this->connector; |
| 43 | + |
| 44 | + /* It doesn't make sense to clone the cache because we want cache state to be shared between imports. |
| 45 | + We're also not cloning the CacheKeyGenerator because they're expected to be stateless algorithms. */ |
| 46 | + } |
| 47 | + |
40 | 48 | /** |
| 49 | + * @param ConnectionContext $context |
41 | 50 | * @param string $source |
42 | | - * @param EncapsulatedOptions|null $options |
43 | 51 | * |
44 | 52 | * @return mixed |
45 | 53 | * |
46 | 54 | * @throws InvalidCacheKeyException |
47 | 55 | */ |
48 | | - public function fetch($source, EncapsulatedOptions $options = null) |
| 56 | + public function fetch(ConnectionContext $context, $source) |
49 | 57 | { |
50 | | - if ($this->isCacheEnabled()) { |
51 | | - $optionsCopy = $options ? $options->copy() : []; |
52 | | - |
53 | | - ksort($optionsCopy); |
| 58 | + if ($context->mustCache()) { |
| 59 | + $options = $this->connector instanceof ConnectorOptions ? $this->connector->getOptions()->copy() : []; |
| 60 | + ksort($options); |
54 | 61 |
|
55 | | - $key = $this->validateCacheKey($this->getCacheKeyGenerator()->generateCacheKey($source, $optionsCopy)); |
| 62 | + $this->validateCacheKey($key = $this->cacheKeyGenerator->generateCacheKey($source, $options)); |
56 | 63 |
|
57 | 64 | if ($this->cache->hasItem($key)) { |
58 | 65 | return $this->cache->getItem($key)->get(); |
59 | 66 | } |
60 | 67 | } |
61 | 68 |
|
62 | | - $data = $this->fetchFreshData($source, $options); |
| 69 | + $data = $this->connector->fetch($context, $source); |
63 | 70 |
|
64 | 71 | isset($key) && $this->cache->save($this->cache->getItem($key)->set($data)); |
65 | 72 |
|
66 | 73 | return $data; |
67 | 74 | } |
68 | 75 |
|
69 | | - abstract public function fetchFreshData($source, EncapsulatedOptions $options = null); |
70 | | - |
71 | | - public function getCache() |
72 | | - { |
73 | | - return $this->cache; |
74 | | - } |
75 | | - |
76 | | - public function setCache(CacheItemPoolInterface $cache) |
77 | | - { |
78 | | - $this->cache = $cache; |
79 | | - } |
80 | | - |
81 | | - public function enableCache() |
82 | | - { |
83 | | - $this->cacheEnabled = true; |
84 | | - } |
85 | | - |
86 | | - public function disableCache() |
87 | | - { |
88 | | - $this->cacheEnabled = false; |
89 | | - } |
90 | | - |
91 | | - public function isCacheEnabled() |
92 | | - { |
93 | | - return $this->cacheEnabled; |
94 | | - } |
95 | | - |
96 | | - public function getCacheKeyGenerator() |
97 | | - { |
98 | | - return $this->cacheKeyGenerator; |
99 | | - } |
100 | | - |
101 | | - public function setCacheKeyGenerator(CacheKeyGenerator $cacheKeyGenerator) |
102 | | - { |
103 | | - $this->cacheKeyGenerator = $cacheKeyGenerator; |
104 | | - } |
105 | | - |
106 | 76 | /** |
107 | 77 | * @param mixed $key |
108 | 78 | * |
109 | | - * @return string |
| 79 | + * @return void |
110 | 80 | * |
111 | 81 | * @throws InvalidCacheKeyException Cache key contains invalid data. |
112 | 82 | */ |
113 | 83 | private function validateCacheKey($key) |
114 | 84 | { |
| 85 | + // TODO: Remove when PHP 5 support dropped and replace with string hint. |
115 | 86 | if (!is_string($key)) { |
116 | 87 | throw new InvalidCacheKeyException('Cache key must be a string.'); |
117 | 88 | } |
118 | 89 |
|
119 | | - if (strpbrk($key, self::RESERVED_CHARACTERS) !== false) { |
| 90 | + if (strpbrk($key, CacheKeyGenerator::RESERVED_CHARACTERS) !== false) { |
120 | 91 | throw new InvalidCacheKeyException(sprintf( |
121 | 92 | 'Cache key "%s" contains one or more reserved characters: "%s".', |
122 | 93 | $key, |
123 | | - self::RESERVED_CHARACTERS |
| 94 | + CacheKeyGenerator::RESERVED_CHARACTERS |
124 | 95 | )); |
125 | 96 | } |
| 97 | + } |
126 | 98 |
|
127 | | - return $key; |
| 99 | + public function getWrappedConnector() |
| 100 | + { |
| 101 | + return $this->connector; |
128 | 102 | } |
129 | 103 | } |
0 commit comments