1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
Copyright (C) 2006-2014 David Négrier - THE CODING MACHINE |
5
|
|
|
|
6
|
|
|
This program is free software; you can redistribute it and/or modify |
7
|
|
|
it under the terms of the GNU General Public License as published by |
8
|
|
|
the Free Software Foundation; either version 2 of the License, or |
9
|
|
|
(at your option) any later version. |
10
|
|
|
|
11
|
|
|
This program is distributed in the hope that it will be useful, |
12
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
GNU General Public License for more details. |
15
|
|
|
|
16
|
|
|
You should have received a copy of the GNU General Public License |
17
|
|
|
along with this program; if not, write to the Free Software |
18
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace Mouf\Database\TDBM; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The StandardObjectStorage class is used to reference all beans that have been fetched from the database. |
25
|
|
|
* If a bean is requested twice from TDBM, the StandardObjectStorage is used to "cache" the bean. |
26
|
|
|
* The StandardObjectStorage acts like a big array. It is used if the "weakref" extension is not available. |
27
|
|
|
* Otherwise, the WeakrefObjectStorage is used instead. |
28
|
|
|
* |
29
|
|
|
* @author David Negrier |
30
|
|
|
*/ |
31
|
|
|
class StandardObjectStorage |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* An array of fetched object, accessible via table name and primary key. |
35
|
|
|
* If the primary key is split on several columns, access is done by an array of columns, serialized. |
36
|
|
|
* |
37
|
|
|
* @var array<string, array<string, TDBMObject>> |
38
|
|
|
*/ |
39
|
|
|
private $objects = array(); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Sets an object in the storage. |
43
|
|
|
* |
44
|
|
|
* @param string $tableName |
45
|
|
|
* @param string $id |
46
|
|
|
* @param TDBMObject $dbRow |
47
|
|
|
*/ |
48
|
|
|
public function set($tableName, $id, DbRow $dbRow) |
49
|
|
|
{ |
50
|
|
|
$this->objects[$tableName][$id] = $dbRow; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Checks if an object is in the storage. |
55
|
|
|
* |
56
|
|
|
* @param string $tableName |
57
|
|
|
* @param string $id |
58
|
|
|
* |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
public function has($tableName, $id) |
62
|
|
|
{ |
63
|
|
|
return isset($this->objects[$tableName][$id]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns an object from the storage (or null if no object is set). |
68
|
|
|
* |
69
|
|
|
* @param string $tableName |
70
|
|
|
* @param string $id |
71
|
|
|
* |
72
|
|
|
* @return DbRow |
73
|
|
|
*/ |
74
|
|
|
public function get($tableName, $id) |
75
|
|
|
{ |
76
|
|
|
if (isset($this->objects[$tableName][$id])) { |
77
|
|
|
return $this->objects[$tableName][$id]; |
78
|
|
|
} else { |
79
|
|
|
return; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Removes an object from the storage. |
85
|
|
|
* |
86
|
|
|
* @param string $tableName |
87
|
|
|
* @param string $id |
88
|
|
|
*/ |
89
|
|
|
public function remove($tableName, $id) |
90
|
|
|
{ |
91
|
|
|
unset($this->objects[$tableName][$id]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Applies the callback to all objects. |
96
|
|
|
* |
97
|
|
|
* @param callable $callback |
98
|
|
|
*/ |
99
|
|
|
public function apply(callable $callback) |
100
|
|
|
{ |
101
|
|
|
foreach ($this->objects as $tableName => $table) { |
102
|
|
|
foreach ($table as $id => $obj) { |
103
|
|
|
$callback($obj, $tableName, $id); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|