|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Webino (http://webino.sk) |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/webino/WebinoDraw for the canonical source repository |
|
6
|
|
|
* @copyright Copyright (c) 2012-2016 Webino, s. r. o. (http://webino.sk) |
|
7
|
|
|
* @author Peter Bačinský <[email protected]> |
|
8
|
|
|
* @license BSD-3-Clause |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace WebinoDraw\Cache; |
|
12
|
|
|
|
|
13
|
|
|
use ArrayObject; |
|
14
|
|
|
use DOMNode; |
|
15
|
|
|
use WebinoDraw\Dom\Element; |
|
16
|
|
|
use WebinoDraw\Dom\Text; |
|
17
|
|
|
use WebinoDraw\Event\DrawEvent; |
|
18
|
|
|
use Zend\Cache\Storage\StorageInterface; |
|
19
|
|
|
use Zend\EventManager\EventManagerAwareInterface; |
|
20
|
|
|
use Zend\EventManager\EventManagerAwareTrait; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class DrawCache |
|
24
|
|
|
*/ |
|
25
|
|
|
class DrawCache implements EventManagerAwareInterface |
|
26
|
|
|
{ |
|
27
|
|
|
use EventManagerAwareTrait; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Cache storage service name |
|
31
|
|
|
*/ |
|
32
|
|
|
const STORAGE = 'WebinoDrawCache'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var StorageInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $cache; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $eventIdentifier = 'WebinoDraw'; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var Element[]|Text[] |
|
46
|
|
|
*/ |
|
47
|
|
|
private $nodes; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param StorageInterface|object $cache |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct(StorageInterface $cache) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->cache = $cache; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Save nodes XHTML to the cache |
|
59
|
|
|
* |
|
60
|
|
|
* @todo refactor |
|
61
|
|
|
* @param DrawEvent $event |
|
62
|
|
|
* @return self |
|
63
|
|
|
*/ |
|
64
|
|
|
public function save(DrawEvent $event) |
|
65
|
|
|
{ |
|
66
|
|
|
$spec = $event->getSpec(); |
|
67
|
|
|
if (empty($spec['cache'])) { |
|
68
|
|
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
foreach ($this->nodes as $cacheKey => $node) { |
|
72
|
|
|
if (empty($node->ownerDocument)) { |
|
73
|
|
|
continue; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$doc = $node->getOwnerDocument(); |
|
77
|
|
|
$cachedNode = $doc->getXpath()->query('//*[@__cacheKey="' . $cacheKey . '"]')->item(0); |
|
78
|
|
|
|
|
79
|
|
|
if (empty($cachedNode) || !($cachedNode instanceof Element)) { |
|
80
|
|
|
// TODO logger should be saved to cache |
|
81
|
|
|
//echo 'SHOULD SAVE: ' . print_r($spec['locator'], true);echo '<br />'; |
|
82
|
|
|
continue; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$cachedNode->removeAttribute('__cacheKey'); |
|
86
|
|
|
|
|
87
|
|
|
// TODO redesign |
|
88
|
|
|
if ($node instanceof Text || 'text' === $cachedNode->getAttribute('__cache')) { |
|
89
|
|
|
$cachedNode->removeAttribute('__cache'); |
|
90
|
|
|
$xhtml = $doc->saveXML($cachedNode->firstChild); |
|
91
|
|
|
} else { |
|
92
|
|
|
$xhtml = $doc->saveXML($cachedNode); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
// TODO logger saving to cache |
|
96
|
|
|
//echo '<br />SAVE: ' . print_r($spec['cache'], true) . '<br />' . htmlspecialchars($xhtml) . '<br />'; |
|
97
|
|
|
$this->cache->setItem($cacheKey, $xhtml); |
|
98
|
|
|
$this->cache->setTags($cacheKey, (array) $spec['cache']); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$this->nodes = []; |
|
102
|
|
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Load nodes XHTML from the cache |
|
107
|
|
|
* |
|
108
|
|
|
* @todo refactor |
|
109
|
|
|
* @param DrawEvent $event |
|
110
|
|
|
* @return bool true = loaded |
|
111
|
|
|
*/ |
|
112
|
|
|
public function load(DrawEvent $event) |
|
113
|
|
|
{ |
|
114
|
|
|
$spec = $event->getSpec(); |
|
115
|
|
|
if (empty($spec['cache'])) { |
|
116
|
|
|
return false; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$cached = true; |
|
120
|
|
|
$nodes = $event->getNodes(); |
|
121
|
|
|
$newNodes = []; |
|
122
|
|
|
|
|
123
|
|
|
foreach ($nodes->toArray() as $_node) { |
|
124
|
|
|
$node = $this->nodeToCache($spec, $_node); |
|
125
|
|
|
$cacheKey = $this->createCacheKey($node, $event); |
|
126
|
|
|
$xhtml = $this->cache->getItem($cacheKey); |
|
127
|
|
|
|
|
128
|
|
|
if (empty($xhtml)) { |
|
129
|
|
|
$cached = false; |
|
130
|
|
|
$newNodes[] = $_node; |
|
131
|
|
|
|
|
132
|
|
|
// TODO logger queued to cache |
|
133
|
|
|
//echo 'CANNOT LOAD: ' . print_r($spec['locator'], true);echo '<br />'; |
|
134
|
|
|
$this->nodes[$cacheKey] = $node; |
|
135
|
|
|
|
|
136
|
|
|
if ($node instanceof Text) { |
|
137
|
|
|
$node->getParentNode()->setAttribute('__cacheKey', $cacheKey); |
|
138
|
|
|
$onReplace = function ($newNode) use ($cacheKey) { |
|
139
|
|
|
$newNode->parentNode->setAttribute('__cacheKey', $cacheKey); |
|
140
|
|
|
}; |
|
141
|
|
|
|
|
|
|
|
|
|
142
|
|
|
} elseif ($node instanceof Element) { |
|
143
|
|
|
$node->setAttribute('__cacheKey', $cacheKey); |
|
144
|
|
|
$onReplace = function (Element $newNode) use ($cacheKey) { |
|
145
|
|
|
$newNode->setAttribute('__cacheKey', $cacheKey); |
|
146
|
|
|
}; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
if (isset($onReplace)) { |
|
150
|
|
|
$node->setOnReplace($this->createOnReplaceHandler()); |
|
151
|
|
|
$node->setOnReplace($onReplace); |
|
152
|
|
|
} |
|
153
|
|
|
continue; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
// TODO logger loading cached |
|
157
|
|
|
//echo '<br />LOAD: ' . print_r($spec['cache'], true) . '<br />' . htmlspecialchars($xhtml) . '<br />'; |
|
158
|
|
|
$frag = $node->ownerDocument->createDocumentFragment(); |
|
159
|
|
|
$frag->appendXml($xhtml); |
|
160
|
|
|
$node->parentNode->replaceChild($frag, $node); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
$nodes->setNodes($newNodes); |
|
164
|
|
|
return $cached; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Select node to cache |
|
169
|
|
|
* |
|
170
|
|
|
* @param ArrayObject $spec |
|
171
|
|
|
* @param Element|Text $node |
|
172
|
|
|
* @return DomNode |
|
173
|
|
|
*/ |
|
174
|
|
|
private function nodeToCache(ArrayObject $spec, $node) |
|
175
|
|
|
{ |
|
176
|
|
|
if (isset($spec['cache_node_xpath'])) { |
|
177
|
|
|
// TODO cache node not found exception |
|
178
|
|
|
return $node->getOwnerDocument()->getXpath()->query($spec['cache_node_xpath'], $node)->item(0); |
|
179
|
|
|
} |
|
180
|
|
|
return $node; |
|
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @param DOMNode $node |
|
185
|
|
|
* @param DrawEvent $event |
|
186
|
|
|
* @return string |
|
187
|
|
|
*/ |
|
188
|
|
|
protected function createCacheKey(DOMNode $node, DrawEvent $event) |
|
189
|
|
|
{ |
|
190
|
|
|
$spec = $event->getSpec(); |
|
191
|
|
|
$cacheKey = $node->getNodePath(); |
|
192
|
|
|
|
|
193
|
|
|
if (!empty($spec['cache_key'])) { |
|
194
|
|
|
// replace vars in the cache key settings |
|
195
|
|
|
$specCacheKey = $spec['cache_key']; |
|
196
|
|
|
$helper = $event->getHelper(); |
|
197
|
|
|
|
|
198
|
|
|
/** @var \WebinoDraw\VarTranslator\Translation $translation */ |
|
199
|
|
|
$translation = clone $helper->getVarTranslator()->getTranslation(); |
|
200
|
|
|
$translation->merge($helper->getVars()); |
|
201
|
|
|
$translation->getVarTranslation()->translate($specCacheKey); |
|
202
|
|
|
|
|
203
|
|
|
$cacheKey .= join('', $specCacheKey); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
empty($spec['cache_key_trigger']) |
|
207
|
|
|
or $cacheKey .= $this->cacheKeyTrigger((array) $spec['cache_key_trigger'], $event); |
|
208
|
|
|
|
|
209
|
|
|
return md5($cacheKey); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @param array $triggers |
|
214
|
|
|
* @param DrawEvent $event |
|
215
|
|
|
* @return string |
|
216
|
|
|
*/ |
|
217
|
|
|
protected function cacheKeyTrigger(array $triggers, DrawEvent $event) |
|
218
|
|
|
{ |
|
219
|
|
|
$spec = $event->getSpec(); |
|
220
|
|
|
$helper = $event->getHelper(); |
|
221
|
|
|
$cacheKey = ''; |
|
222
|
|
|
|
|
223
|
|
|
foreach ($triggers as $eventName) { |
|
224
|
|
|
$results = $this->getEventManager()->trigger($eventName, $helper, ['spec' => $spec]); |
|
225
|
|
|
foreach ($results as $result) { |
|
226
|
|
|
$cacheKey .= $result; |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
return $cacheKey; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @return \Closure |
|
235
|
|
|
*/ |
|
236
|
|
|
private function createOnReplaceHandler() |
|
237
|
|
|
{ |
|
238
|
|
|
return function ($newNode, $oldNode, $frag) { |
|
239
|
|
|
$self = ($oldNode instanceof Text) ? $oldNode->getParentNode() : $oldNode; |
|
240
|
|
|
if ($self->hasAttribute('__cacheKey')) { |
|
241
|
|
|
$cacheKey = $self->getAttributeNode('__cacheKey'); |
|
242
|
|
|
|
|
243
|
|
|
if ($newNode instanceof Text) { |
|
244
|
|
|
$newParentNode = $newNode->getParentNode(); |
|
245
|
|
|
$newParentNode->setAttributeNode($cacheKey); |
|
246
|
|
|
$newParentNode->setAttribute('__cache', 'text'); |
|
247
|
|
|
|
|
|
|
|
|
|
248
|
|
|
} elseif ($newNode instanceof Element) { |
|
249
|
|
|
if ($frag->childNodes->length <= 1) { |
|
250
|
|
|
// has wrapper |
|
251
|
|
|
$newNode->setAttributeNode($cacheKey); |
|
252
|
|
|
} else { |
|
253
|
|
|
$newNode->parentNode->setAttributeNode($cacheKey); |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
$self->removeAttribute('__cacheKey'); |
|
257
|
|
|
} |
|
258
|
|
|
}; |
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
|
|
|
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 implementation 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 interface: