|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MatthiasMullie\Scrapbook\Psr6; |
|
4
|
|
|
|
|
5
|
|
|
use MatthiasMullie\Scrapbook\KeyValueStore; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Helper object to serve as glue between pool & item. |
|
9
|
|
|
* |
|
10
|
|
|
* New items are created by first get()-ing them from the pool. It would be |
|
11
|
|
|
* inefficient to let such a get() immediately query the real cache (because it |
|
12
|
|
|
* may not be meant to retrieve real data, but just to set a new value) |
|
13
|
|
|
* |
|
14
|
|
|
* Instead, every Item returned by get() will be a "placeholder", and once the |
|
15
|
|
|
* values are actually needed, this object will be called to go do that (along |
|
16
|
|
|
* with every other value that has not yet been resolved, while we're at it) |
|
17
|
|
|
* |
|
18
|
|
|
* @author Matthias Mullie <[email protected]> |
|
19
|
|
|
* @copyright Copyright (c) 2014, Matthias Mullie. All rights reserved |
|
20
|
|
|
* @license LICENSE MIT |
|
21
|
|
|
*/ |
|
22
|
|
|
class Repository |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var KeyValueStore |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $store; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Array of resolved items. |
|
31
|
|
|
* |
|
32
|
|
|
* @var mixed[] [unique => value] |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $resolved = array(); |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Array of unresolved items. |
|
38
|
|
|
* |
|
39
|
|
|
* @var string[] [unique => key] |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $unresolved = array(); |
|
42
|
|
|
|
|
43
|
|
|
public function __construct(KeyValueStore $store) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->store = $store; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Add a to-be-resolved cache key. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $unique |
|
52
|
|
|
* @param string $key |
|
53
|
|
|
*/ |
|
54
|
|
|
public function add($unique, $key) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->unresolved[$unique] = $key; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* This repository holds the real values for all Item objects. However, if |
|
61
|
|
|
* such an item gets garbage collected, there is no point in wasting any |
|
62
|
|
|
* more memory storing that value. |
|
63
|
|
|
* In that case, this method can be called to remove those values. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $unique |
|
66
|
|
|
*/ |
|
67
|
|
|
public function remove($unique) |
|
68
|
|
|
{ |
|
69
|
|
|
unset($this->unresolved[$unique], $this->resolved[$unique]); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $unique |
|
74
|
|
|
* |
|
75
|
|
|
* @return mixed|null Value, of null if non-existent |
|
76
|
|
|
*/ |
|
77
|
|
|
public function get($unique) |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->exists($unique) ? $this->resolved[$unique] : null; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param string $unique |
|
84
|
|
|
* |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
|
|
public function exists($unique) |
|
88
|
|
|
{ |
|
89
|
|
|
if (array_key_exists($unique, $this->unresolved)) { |
|
90
|
|
|
$this->resolve(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return array_key_exists($unique, $this->resolved); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Resolve all unresolved keys at once. |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function resolve() |
|
100
|
|
|
{ |
|
101
|
|
|
$keys = array_unique(array_values($this->unresolved)); |
|
102
|
|
|
$values = $this->store->getMulti($keys); |
|
103
|
|
|
|
|
104
|
|
|
foreach ($this->unresolved as $unique => $key) { |
|
105
|
|
|
if (!array_key_exists($key, $values)) { |
|
106
|
|
|
// key doesn't exist in cache |
|
107
|
|
|
continue; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/* |
|
111
|
|
|
* In theory, there could've been multiple unresolved requests for |
|
112
|
|
|
* the same cache key. In the case of objects, we'll clone them |
|
113
|
|
|
* to make sure that when the value for 1 item is manipulated, it |
|
114
|
|
|
* doesn't affect the value of the other item (because those objects |
|
115
|
|
|
* would be passed by-ref without the cloning) |
|
116
|
|
|
*/ |
|
117
|
|
|
$value = $values[$key]; |
|
118
|
|
|
$value = is_object($value) ? clone $value : $value; |
|
119
|
|
|
|
|
120
|
|
|
$this->resolved[$unique] = $value; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$this->unresolved = array(); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|