|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Flying\Struct\Storage; |
|
4
|
|
|
|
|
5
|
|
|
use Flying\Struct\ConfigurationManager; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Objects storage container |
|
9
|
|
|
*/ |
|
10
|
|
|
class Storage implements StorageInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* List of stored objects |
|
14
|
|
|
* |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
private $storage = []; |
|
18
|
|
|
/** |
|
19
|
|
|
* List of objects marked as "dirty" |
|
20
|
|
|
* |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
private $dirty = []; |
|
24
|
|
|
/** |
|
25
|
|
|
* Storage backend |
|
26
|
|
|
* |
|
27
|
|
|
* @var BackendInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $backend; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Register given object in storage |
|
33
|
|
|
* |
|
34
|
|
|
* @param StorableInterface $object |
|
35
|
|
|
* @return $this |
|
36
|
|
|
*/ |
|
37
|
84 |
|
public function register(StorableInterface $object) |
|
38
|
|
|
{ |
|
39
|
84 |
|
$hash = spl_object_hash($object); |
|
40
|
84 |
|
$this->storage[$hash] = $object; |
|
41
|
84 |
|
return $this; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Check if given object is registered into storage |
|
46
|
|
|
* |
|
47
|
|
|
* @param StorableInterface $object |
|
48
|
|
|
* @return boolean |
|
49
|
|
|
*/ |
|
50
|
4 |
|
public function has(StorableInterface $object) |
|
51
|
|
|
{ |
|
52
|
4 |
|
$hash = spl_object_hash($object); |
|
53
|
4 |
|
return array_key_exists($hash, $this->storage); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Remove given object from storage |
|
58
|
|
|
* |
|
59
|
|
|
* @param StorableInterface $object |
|
60
|
|
|
* @return $this |
|
61
|
|
|
*/ |
|
62
|
2 |
|
public function remove(StorableInterface $object) |
|
63
|
|
|
{ |
|
64
|
2 |
|
$hash = spl_object_hash($object); |
|
65
|
2 |
|
unset($this->storage[$hash]); |
|
66
|
2 |
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Load contents of given object from storage |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $key Storage key |
|
73
|
|
|
* @return mixed |
|
74
|
|
|
*/ |
|
75
|
78 |
|
public function load($key) |
|
76
|
|
|
{ |
|
77
|
78 |
|
$backend = $this->getBackend(); |
|
78
|
78 |
|
$result = null; |
|
79
|
78 |
|
if ($backend->has($key)) { |
|
80
|
3 |
|
$result = $backend->load($key); |
|
81
|
3 |
|
} |
|
82
|
78 |
|
return $result; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get storage backend |
|
87
|
|
|
* |
|
88
|
|
|
* @return BackendInterface |
|
89
|
|
|
*/ |
|
90
|
84 |
|
public function getBackend() |
|
91
|
|
|
{ |
|
92
|
84 |
|
if (!$this->backend) { |
|
93
|
81 |
|
$this->backend = ConfigurationManager::getConfiguration()->getStorageBackend(); |
|
94
|
81 |
|
} |
|
95
|
84 |
|
return $this->backend; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Set storage backend |
|
100
|
|
|
* |
|
101
|
|
|
* @param BackendInterface $backend |
|
102
|
|
|
* @return $this |
|
103
|
|
|
*/ |
|
104
|
3 |
|
public function setBackend(BackendInterface $backend) |
|
105
|
|
|
{ |
|
106
|
3 |
|
$this->backend = $backend; |
|
107
|
3 |
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Mark given object as "dirty" |
|
112
|
|
|
* |
|
113
|
|
|
* @param StorableInterface $object |
|
114
|
|
|
* @return $this |
|
115
|
|
|
*/ |
|
116
|
30 |
|
public function markAsDirty(StorableInterface $object) |
|
117
|
|
|
{ |
|
118
|
30 |
|
$hash = spl_object_hash($object); |
|
119
|
30 |
|
$this->dirty[$hash] = true; |
|
120
|
30 |
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Mark given object as "not dirty" |
|
125
|
|
|
* |
|
126
|
|
|
* @param StorableInterface $object |
|
127
|
|
|
* @return $this |
|
128
|
|
|
*/ |
|
129
|
3 |
|
public function markAsNotDirty(StorableInterface $object) |
|
130
|
|
|
{ |
|
131
|
3 |
|
$hash = spl_object_hash($object); |
|
132
|
3 |
|
unset($this->dirty[$hash]); |
|
133
|
3 |
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Check if given object is "dirty" |
|
138
|
|
|
* |
|
139
|
|
|
* @param StorableInterface $object |
|
140
|
|
|
* @return boolean |
|
141
|
|
|
*/ |
|
142
|
4 |
|
public function isDirty(StorableInterface $object) |
|
143
|
|
|
{ |
|
144
|
4 |
|
$hash = spl_object_hash($object); |
|
145
|
4 |
|
return array_key_exists($hash, $this->dirty); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Flush all changes in objects into storage |
|
150
|
|
|
* |
|
151
|
|
|
* @throws \RuntimeException |
|
152
|
|
|
* @return $this |
|
153
|
|
|
*/ |
|
154
|
7 |
|
public function flush() |
|
155
|
|
|
{ |
|
156
|
7 |
|
$backend = $this->getBackend(); |
|
157
|
7 |
|
$storedKeys = []; |
|
158
|
|
|
/** @var $object StorableInterface */ |
|
159
|
7 |
|
foreach ($this->storage as $hash => $object) { |
|
160
|
7 |
|
$key = $object->getStorageKey(); |
|
161
|
7 |
|
if ((!array_key_exists($hash, $this->dirty)) && $backend->has($key)) { |
|
162
|
3 |
|
continue; |
|
163
|
|
|
} |
|
164
|
6 |
|
if (in_array($key, $storedKeys, true)) { |
|
165
|
1 |
|
throw new \RuntimeException('Multiple objects with same storage key "' . $key |
|
166
|
1 |
|
. '" are requested to be stored'); |
|
167
|
|
|
} |
|
168
|
6 |
|
$backend->save($key, $object->toStorage()); |
|
169
|
6 |
|
$storedKeys[] = $key; |
|
170
|
7 |
|
} |
|
171
|
6 |
|
$this->dirty = []; |
|
172
|
6 |
|
return $this; |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|