|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* |
|
5
|
|
|
* This file is part of the Apix Project. |
|
6
|
|
|
* |
|
7
|
|
|
* (c) Franck Cassedanne <franck at ouarz.net> |
|
8
|
|
|
* |
|
9
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause New BSD License |
|
10
|
|
|
* |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Apix\SimpleCache\PsrSimpleCache; |
|
14
|
|
|
|
|
15
|
|
|
use Apix\Cache\Adapter as CacheAdapter; |
|
16
|
|
|
use Psr\Cache\CacheItemInterface as ItemInterface; |
|
17
|
|
|
use Psr\Cache\CacheItemPoolInterface as CacheItemPool; |
|
18
|
|
|
use Psr\Cache\InvalidArgumentException as CacheInvalidArgumentException; |
|
19
|
|
|
|
|
20
|
|
|
use Apix\Cache\PsrCache\Item as CacheItem; |
|
21
|
|
|
|
|
22
|
|
|
class TaggablePool extends Pool |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The tags associated with this pool. |
|
27
|
|
|
* @var array|null |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $tags = null; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var CacheAdapter |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $cache_adapter; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Constructor. |
|
38
|
|
|
*/ |
|
39
|
7 |
|
public function __construct(CacheItemPool $cache_item_pool) |
|
40
|
|
|
{ |
|
41
|
7 |
|
parent::__construct($cache_item_pool); |
|
42
|
|
|
|
|
43
|
7 |
|
$this->cache_adapter = $this->cache_item_pool->getCacheAdapter(); |
|
44
|
|
|
|
|
45
|
|
|
$options = array( |
|
46
|
|
|
'tag_enable' => true // wether to enable tagging |
|
47
|
7 |
|
); |
|
48
|
|
|
|
|
49
|
7 |
|
$this->cache_adapter->setOptions($options); |
|
50
|
7 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
*/ |
|
55
|
2 |
|
protected function setItemProperties( |
|
56
|
|
|
CacheItem $item, $value, $ttl = null |
|
57
|
|
|
) { |
|
58
|
2 |
|
return $item->set($value) |
|
|
|
|
|
|
59
|
2 |
|
->expiresAfter($ttl) |
|
60
|
2 |
|
->setTags($this->tags); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Retrieves the cache keys for the given tag. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $tag The cache tag to retrieve. |
|
67
|
|
|
* @return array Returns an array of cache keys. |
|
68
|
|
|
*/ |
|
69
|
2 |
|
public function getMultipleByTag($tag) |
|
70
|
|
|
{ |
|
71
|
2 |
|
$keys = $this->cache_adapter->loadTag($tag); |
|
72
|
2 |
|
$items = array(); |
|
73
|
2 |
|
if ($keys) { |
|
74
|
1 |
|
foreach ($keys as $key) { |
|
75
|
1 |
|
$k = $this->cache_adapter->removePrefixKey($key); |
|
76
|
1 |
|
$items[$k] = $this->get($k); |
|
77
|
1 |
|
} |
|
78
|
1 |
|
} |
|
79
|
|
|
|
|
80
|
2 |
|
return $items; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Removes all the cached entries associated with the given tag names. |
|
85
|
|
|
* |
|
86
|
|
|
* @param array $tags An array of tag names (string). |
|
87
|
|
|
* @return bool |
|
88
|
|
|
*/ |
|
89
|
2 |
|
public function clearByTags(array $tags) |
|
90
|
|
|
{ |
|
91
|
2 |
|
return $this->cache_adapter->clean($tags); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Sets this pool tags. |
|
96
|
|
|
* |
|
97
|
|
|
* @param array|null $tags |
|
98
|
|
|
* @return TaggableItem The invoked object. |
|
99
|
|
|
*/ |
|
100
|
5 |
|
public function setTags(array $tags=null) |
|
101
|
|
|
{ |
|
102
|
5 |
|
$this->tags = $tags; |
|
103
|
|
|
|
|
104
|
5 |
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Returns this pool tags. |
|
109
|
|
|
* |
|
110
|
|
|
* @return array|null |
|
111
|
|
|
*/ |
|
112
|
1 |
|
public function getTags() |
|
113
|
|
|
{ |
|
114
|
1 |
|
return $this->tags; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: