1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by Gorlum 21.04.2018 16:13 |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Core; |
7
|
|
|
|
8
|
|
|
use Common\Interfaces\IContainer; |
9
|
|
|
use Exception; |
10
|
|
|
use Planet\Planet; |
11
|
|
|
use SN; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Entity repository |
15
|
|
|
* |
16
|
|
|
* |
17
|
|
|
* |
18
|
|
|
* |
19
|
|
|
* @package Core |
20
|
|
|
*/ |
21
|
|
|
class RepoV2 implements IContainer { |
22
|
|
|
|
23
|
|
|
// /** |
24
|
|
|
// * @var GlobalContainer $gc |
25
|
|
|
// */ |
26
|
|
|
// protected $gc; |
27
|
|
|
// |
28
|
|
|
// /** |
29
|
|
|
// * @var StorageV2 $storage |
30
|
|
|
// */ |
31
|
|
|
// protected $storage; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* List of synonyms |
35
|
|
|
* |
36
|
|
|
* @var string[] $synonyms |
37
|
|
|
*/ |
38
|
|
|
protected $synonyms = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var EntityDb[][] $repo |
42
|
|
|
*/ |
43
|
|
|
protected $repo = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var int[][] $version |
47
|
|
|
*/ |
48
|
|
|
protected $version = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Core\Repository constructor. |
52
|
|
|
* |
53
|
|
|
* @param GlobalContainer $gc |
54
|
|
|
*/ |
55
|
|
|
public function __construct(GlobalContainer $gc) { |
|
|
|
|
56
|
|
|
// $this->gc = $gc; |
|
|
|
|
57
|
|
|
// $this->storage = $gc->storageV2; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $className |
62
|
|
|
* @param string $synonym |
63
|
|
|
* |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
public function addSynonym($className, $synonym) { |
67
|
|
|
$this->synonyms[$className] = $synonym; |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param mixed $name |
74
|
|
|
* |
75
|
|
|
* @return EntityDb|null |
76
|
|
|
*/ |
77
|
|
|
public function __get($name) { |
78
|
|
|
return $this->__isset($name) ? $this->repo[reset($name)][end($name)] : null; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param mixed $name |
83
|
|
|
* |
84
|
|
|
* @return EntityDb|null |
85
|
|
|
* |
86
|
|
|
* @throws Exception |
87
|
|
|
*/ |
88
|
|
|
public function getOrLoad($name) { |
89
|
|
|
if ($this->__isset($name)) { |
90
|
|
|
return $this->__get($name); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$className = $this->getClassName($name); |
94
|
|
|
$dbId = $this->getDbId($name); |
95
|
|
|
/** |
96
|
|
|
* @var EntityDb $entity |
97
|
|
|
*/ |
98
|
|
|
$entity = new $className(); |
99
|
|
|
$entity->dbLoadRecord($dbId); |
100
|
|
|
|
101
|
|
|
if ($entity->isNew()) { |
102
|
|
|
unset($entity); |
103
|
|
|
$entity = null; |
104
|
|
|
} else { |
105
|
|
|
$this->__set($name, $entity); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $entity; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param int|string $planetId |
113
|
|
|
* |
114
|
|
|
* @return Planet|EntityDb|null |
115
|
|
|
* |
116
|
|
|
* @throws Exception |
117
|
|
|
*/ |
118
|
|
|
public function getPlanet($planetId) { |
119
|
|
|
return $this->getOrLoad([Planet::class, $planetId]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Writes entity to repository |
124
|
|
|
* |
125
|
|
|
* Entity should not be already set - otherwise exception raised |
126
|
|
|
* |
127
|
|
|
* @param mixed $name |
128
|
|
|
* @param EntityDb $value |
129
|
|
|
* |
130
|
|
|
* @return void |
131
|
|
|
* @throws Exception |
132
|
|
|
*/ |
133
|
|
|
public function __set($name, $value) { |
134
|
|
|
if (!($className = $this->getClassName($name))) { |
135
|
|
|
throw new Exception("Mallformed name " . var_export($name, true) . " in RepoV2::__set(). "); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if ($this->__isset($name) && $this->__get($name) !== $value) { |
139
|
|
|
throw new Exception("Trying to overwrite entity [" . implode(',', $name) . "] which already set. Unset it first!"); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$dbId = $this->getDbId($name); |
143
|
|
|
$this->repo[$className][$dbId] = $value; |
144
|
|
|
$this->version[$className][$dbId] = SN::$transaction_id; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param array $name |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
protected function getClassName($name) { |
153
|
|
|
if (!$this->isNameValid($name)) { |
154
|
|
|
return false; |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$className = reset($name); |
158
|
|
|
if (!empty($this->synonyms[$className])) { |
159
|
|
|
$className = $this->synonyms[$className]; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $className; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param array $name |
167
|
|
|
* |
168
|
|
|
* @return mixed |
169
|
|
|
*/ |
170
|
|
|
protected function getDbId($name) { |
171
|
|
|
$dbId = end($name); |
172
|
|
|
|
173
|
|
|
return $dbId; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param array $name - [entityClassName, dbId] |
178
|
|
|
* |
179
|
|
|
* @return bool |
180
|
|
|
*/ |
181
|
|
|
protected function isNameValid($name) { |
182
|
|
|
return is_array($name) && count($name) == 2; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Checks if entity with specified DB ID registered in repository |
187
|
|
|
* |
188
|
|
|
* Repository also holds entity current version (for transaction support) |
189
|
|
|
* Repository stores ONLY EXISTING OBJECTS - so it's not caches unsuccessful data retrieves |
190
|
|
|
* |
191
|
|
|
* @param array $name - [entityClassName, dbId] |
192
|
|
|
* |
193
|
|
|
* @return bool |
194
|
|
|
*/ |
195
|
|
|
public function __isset($name) { |
196
|
|
|
if (!($className = $this->getClassName($name))) { |
197
|
|
|
return false; |
198
|
|
|
} |
199
|
|
|
$dbId = $this->getDbId($name); |
200
|
|
|
|
201
|
|
|
return is_array($this->repo[$className]) && isset($this->repo[$className][$dbId]); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param array $name |
206
|
|
|
* |
207
|
|
|
* @return void |
208
|
|
|
*/ |
209
|
|
|
public function __unset($name) { |
210
|
|
|
if (!($className = $this->getClassName($name))) { |
211
|
|
|
return; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
$dbId = $this->getDbId($name); |
215
|
|
|
unset($this->repo[$className][$dbId]); |
216
|
|
|
unset($this->version[$className][$dbId]); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Is container contains no data |
221
|
|
|
* |
222
|
|
|
* @return bool |
223
|
|
|
*/ |
224
|
|
|
public function isEmpty() { |
225
|
|
|
return empty($this->repo) && empty($this->version); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Clears container contents |
230
|
|
|
*/ |
231
|
|
|
public function clear() { |
232
|
|
|
$this->repo = []; |
233
|
|
|
$this->version = []; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
} |
237
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.