-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathUnifiedContainer.class.php
More file actions
406 lines (322 loc) · 8.19 KB
/
UnifiedContainer.class.php
File metadata and controls
406 lines (322 loc) · 8.19 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<?php
/***************************************************************************
* Copyright (C) 2005-2009 by Konstantin V. Arkhipov *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of the *
* License, or (at your option) any later version. *
* *
***************************************************************************/
/*
UnifiedContainer:
child's and parent's field names:
abstract public function getChildIdField()
abstract public function getParentIdField()
all we need from outer world:
public function __construct(
Identifiable $parent, UnifiedContainer $dao, $lazy = true
)
if you want to apply Criteria's "filter":
public function setCriteria(Criteria $criteria)
first you should fetch whatever you want:
public function fetch()
then you can get it:
public function getList()
set you modified list:
public function setList($list)
finally, sync fetched data and stored one:
public function save()
OneToManyLinked <- UnifiedContainer:
indicates whether child can be free (parent_id nullable):
protected function isUnlinkable()
ManyToManyLinked <- UnifiedContainer:
helper's table name:
abstract public function getHelperTable()
id field name at parent's primary table:
protected function getParentTableIdField()
*/
/**
* IdentifiableObject childs collection handling.
*
* @see StorableContainer for alternative
*
* @ingroup Containers
**/
abstract class UnifiedContainer
{
protected $worker = null;
protected $parent = null;
protected $dao = null;
protected $lazy = true;
protected $fetched = false;
protected $list = array();
protected $clones = array();
// sleep state
protected $workerClass = null;
protected $daoClass = null;
protected $comparator = null;
abstract public function getParentIdField();
abstract public function getChildIdField();
public function __construct(
Identifiable $parent, GenericDAO $dao, $lazy = true
)
{
Assert::isBoolean($lazy);
$this->parent = $parent;
$this->lazy = $lazy;
$this->dao = $dao;
Assert::isInstance($dao->getObjectName(), 'Identifiable');
$this->comparator = SerializedObjectComparator::me();
}
public function __sleep()
{
$this->daoClass = get_class($this->dao);
$this->workerClass = get_class($this->worker);
return array('workerClass', 'daoClass', 'parent', 'lazy');
}
public function __wakeup()
{
$this->dao = Singleton::getInstance($this->daoClass);
$this->worker = new $this->workerClass($this);
}
public function __clone() {
$this->worker = clone $this->worker;
}
public function getParentObject()
{
return $this->parent;
}
/**
* @return GenericDAO
**/
public function getDao()
{
return $this->dao;
}
public function isLazy()
{
return $this->lazy;
}
public function isFetched()
{
return $this->fetched;
}
/**
* @throws WrongArgumentException
* @return UnifiedContainer
**/
public function setCriteria(Criteria $criteria)
{
Assert::isTrue(
$criteria->getDao() === null
|| (
$criteria->getDao() === $this->dao
),
"criteria's dao doesn't match container's one"
);
if (!$criteria->getDao())
$criteria->setDao($this->dao);
$this->worker->setCriteria($criteria);
return $this;
}
/**
* @return Criteria
**/
public function getCriteria()
{
return $this->worker->getCriteria();
}
public function setObjectComparator(Comparator $comparator)
{
$this->comparator = $comparator;
return $this;
}
/**
* @throws WrongArgumentException
* @return UnifiedContainer
**/
public function setList($list)
{
Assert::isArray($list);
$this->list = $list;
return $this;
}
/**
* @return UnifiedContainer
**/
public function mergeList(array $list)
{
if ($this->lazy) {
foreach ($list as $id)
$this->list[$id] = $id;
} else {
$this->list = array_merge($this->list, $list);
}
$this->fetched = true;
return $this;
}
public function getList()
{
if (!$this->list && !$this->isFetched())
$this->fetch();
return $this->list;
}
public function getCount()
{
if (!$this->isFetched() && $this->parent->getId()) {
$row = $this->dao->getCustom($this->worker->makeCountQuery());
return current($row);
}
return count($this->list);
}
/**
* @throws WrongStateException
* @return UnifiedContainer
**/
public function fetch()
{
if (!$this->parent->getId())
throw new WrongStateException(
'save parent object first'
);
try {
$this->fetchList();
} catch (ObjectNotFoundException $e) {
// yummy
}
$this->fetched = true;
return $this;
}
/**
* @throws WrongArgumentException
* @return UnifiedContainer
**/
public function save()
{
Assert::isArray(
$this->list,
"that's not an array :-/"
);
if (!$this->fetched)
throw new WrongStateException(
'do not want to save non-fetched collection'
);
$list = $this->list;
$clones = $this->clones;
$ids = $insert = $delete = $update = array();
if ($this->lazy) {
foreach ($list as $id) {
if (!isset($clones[$id]))
$insert[] = $ids[$id] = $id;
else
$ids[$id] = $id;
}
foreach ($clones as $id) {
if (!isset($ids[$id]))
$delete[] = $id;
}
} else {
foreach ($list as $object) {
$id = $object->getId();
if (null === $id) {
$insert[] = $object;
} elseif (
isset($clones[$id])
// there is no another way yet to compare objects without
// risk of falling into fatal error:
// "nesting level too deep?"
&& ($this->comparator->compare($object, $clones[$id]) <> 0)
) {
$update[] = $object;
} elseif (!isset($clones[$id])) {
$insert[] = $object;
}
if (null !== $id)
$ids[$id] = $object;
}
foreach ($clones as $id => $object) {
if (!isset($ids[$id]))
$delete[] = $object;
}
}
InnerTransactionWrapper::create()->
setDao($this->getDao())->
setFunction(array($this->worker, 'sync'))->
run($insert, $update, $delete);
$this->clones = array();
$this->syncClones();
$this->dao->uncacheLists();
return $this;
}
/**
* @return UnifiedContainer
**/
public function clean()
{
$this->list = $this->clones = array();
$this->fetched = false;
return $this;
}
/**
* @return UnifiedContainer
**/
public function dropList()
{
$this->worker->dropList();
$this->clean();
return $this;
}
/* void */ public static function destroy(UnifiedContainer $container)
{
unset($container->worker, $container);
}
protected function fetchList()
{
$query = $this->worker->makeFetchQuery();
if ($this->lazy) {
$list = $this->dao->getCustomRowList($query);
// special case for handling result from db
if (
isset($list[0])
&& is_array($list[0])
) {
$newList = array();
foreach ($list as $key => $value)
$newList[] = $value[$this->getChildIdField()];
$list = $newList;
}
} else
$list = $this->dao->getListByQuery($query);
$this->list = array();
return $this->importList($list);
}
/**
* @return UnifiedContainer
**/
private function importList(array $list)
{
$this->mergeList($list);
$this->syncClones();
return $this;
}
/**
* @return UnifiedContainer
**/
private function syncClones()
{
if ($this->lazy) {
foreach ($this->list as $id) {
$this->clones[$id] = $id;
}
} else {
foreach ($this->list as $object) {
// don't track unsaved objects
if ($id = $object->getId())
$this->clones[$id] = clone $object;
}
}
return $this;
}
}
?>