/
Registry.php
327 lines (278 loc) · 9.92 KB
/
Registry.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
namespace persistr {
use qtil;
class Registry extends qtil\Registry {
/**
* List of persisted objects
* @var array
*/
private static $persisted = [];
/**
* List of persistence models
* @var array
*/
private static $models = [];
/**
* List of persistors
* @var array
*/
private static $persistors = [];
/**
* Main persistence method, all new persisted objects must execute this
* in the constructor
* @param mixed $object
*/
public static function persist($object) {
self::setup($object);
}
/**
* Retrieves persistor by class
* @param mixed $className
* @return mixed
* @throws \InvalidArgumentException
*/
public static function persistor($className) {
if($className instanceof Interfaces\Model) {
$className = $className->getClassName();
} elseif(is_object($className)) {
$className = get_class($className);
} elseif(!is_string($className)) {
throw new \InvalidArgumentException();
}
return self::$persisted[$className];
}
/**
* Checks if class is persisted
* @param string $className
* @return boolean
*/
public static function persisted($className) {
return array_key_exists($className,self::$persisted);
}
/**
* Factory method to add persistence layer to class, interface, or trait
* @param string $sourceAsset
* @return \persistr\Persistor
*/
public static function persistClass($sourceAsset,Persistor $persistor=null) {
foreach(self::$persistors as $p) {
if($p->isTrait() && $p->using($sourceAsset)) {
$persistor = $p;
} elseif($p->isClass() && $p->extending($sourceAsset)) {
$persistor = $p;
} elseif($p->isInterface() && $p->implementing($sourceAsset)) {
$persistor = $p;
}
}
if($persistor instanceof Persistor) {
self::$persisted[$sourceAsset] = $persistor;
return $persistor;
}
}
/**
* Helper method
* Retrieves persistor or builds default filter
* @param string $className
*/
public static function getPersistorByName($className) {
if(self::persisted($className)) {
$persistor = self::persistor($className);
} else {
$persistor = self::persistClass($className);
}
if(is_null($persistor)) {
$persistor = self::createBlankPersistor($className);
}
return $persistor;
}
/**
* Helper function
* Retrieves persistence model by class name
* @param mixed $className
* @return persistr\Interfaces\Model
* @throws Exception
*/
public static function getModelByName($className) {
$persistor = self::getPersistorByName($className);
if($persistor instanceof Persistor) {
$datasource = $persistor->getDataSource();
$model = $datasource[$className];
} else {
throw new Exception('Persistor invalid ("'.$className.'")');
}
if(empty($model)) {
if(@class_exists($className)) {
$parents = class_parents($className);
if(!empty($parents)) {
return self::getModelByName($parents[array_keys($parents)[0]]);
} else {
throw new Exception('Model not found for object type("'.$className.'")');
}
}
}
return $model;
}
/**
* Helper method to create blank model if none found
* @param string $className
* @return persistr\Model\Blank
*/
private static function createBlankPersistor($className) {
$datasource = new qtil\Collection();
$persistenceModel = new PersistenceModel($datasource);
$persistor = new Persistor($className, $persistenceModel);
self::register($persistor);
$model = new Model\Blank($className);
$datasource->insert($className,$model);
return $persistor;
}
/**
* Helper function
* Retrieves data filters by model
* @param persistr\Interfaces\Model $model
* @param persistr\Object\Registry $registry
* @return array
*/
private static function getDataFiltersByModel(Interfaces\Model $model, persistr\Object\Registry $registry = null) {
$className = $model->getClassName();
if(self::persisted($className)) {
$persistor = self::persistor($className);
} else {
$persistor = self::persistClass($className);
}
if(is_null($registry)) {
$registry = $model->getRegistry();
}
$filter = null;
if(!$registry->hasDataFilters() && $persistor->hasDataFilters()) {
$filters = $persistor->getDataFilters();
foreach($filters as $filter) {
if(is_object($filter)) {
$registry->addDataFilter(clone $filter);
}
}
} else {
$filter = $registry->getDataFilters();
}
return $filter;
}
/**
* Performs setup method to prepare object for persistence
* @param mixed $object
* @throws Exception
*/
public static function setup($object) {
$fullClassName = get_class($object);
$model = self::getModelByName($fullClassName);
if(!is_null($model)) {
$objectRegistry = $model->getRegistry();
$objectRegistry->addInstance($object);
if($objectRegistry->hasDataFilters()) {
$filters = self::getDataFiltersByModel($model);
self::filter($filters,$object);
}
} else {
throw new Exception('Model not found : object ('.$fullClassName.').');
}
}
/**
* Helper method
* Performs initialization filtering
* Defaults to blank object
* @param array $filters
* @param mixed $object
*/
protected static function filter(array $filters,$object) {
if(!empty($filters)) {
foreach($filters as $filter) {
if(\is_callable($filter)) {
$filter([],$object);
}
}
}
}
/**
* Adds persistor to registry
* @param \persistr\Persistor $persistor
*/
public static function register(Persistor $persistor) {
$sourceAsset = $persistor->getSourceAsset();
self::$persistors[$sourceAsset] = $persistor;
}
/**
* Removes all persistors from registry
*/
public static function clearPersistors() {
self::$persistors = [];
self::$models = [];
self::$persisted = [];
}
/**
* Removes all references in registery to object
* @param mixed $object
*/
public static function free($object) {
$filter = self::modelRegistry($object)->filter();
if(!is_null($filter)) {
$filter->free($object);
}
}
/**
* Helper function
* Retrieves object registry by model
* @param mixed $object
* @return persistr\Object\Registry
*/
private static function modelRegistry($object) {
$model = self::getModel($object);
return $model->getRegistry();
}
/**
* Checks if object has data filters
* @param mixed $object
* @return boolean
*/
public static function filtered($object) {
$model = self::getModel($object);
if(is_null($model)) {
return false;
}
$registry = $model->getRegistry();
if(is_null($registry)) {
return false;
}
return ($registry->hasDataFilters());
}
/**
* Helper method
* Retrieves persistence model by object
* @param mixed $object
* @return persistr\Interfaces\Model
* @throws Exception
*/
public static function getModel($object) {
$className = get_class($object);
if(isset(self::$models[$className])) {
return self::$models[$className];
}
$model = self::getModelByName($className);
if(!is_null($model)) {
self::setModel($object,$model);
return $model;
}
}
/**
* Sets persistence model for object
* newModel may be model itself or the models absolute name
* @param mixed $object
* @param mixed $newModel
*/
public static function setModel($object, $newModel) {
$className = get_class($object);
if(is_string($newModel)) {
$newModel = self::getModelByName($newModel);
}
self::$models[$className] = $newModel;
}
}
}