Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 65 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
common_cache_PartitionedCachable | |
0.00% |
0 / 65 |
|
0.00% |
0 / 7 |
1482 | |
0.00% |
0 / 1 |
getSerial | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
__sleep | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
182 | |||
__wakeup | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
_remove | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
getSuccessors | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
110 | |||
getPredecessors | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
30 | |||
buildSerial | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
getCache | n/a |
0 / 0 |
n/a |
0 / 0 |
0 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg |
19 | * (under the project TAO & TAO2); |
20 | * 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung |
21 | * (under the project TAO-TRANSFER); |
22 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
23 | * (under the project TAO-SUSTAIN & TAO-DEV); |
24 | * |
25 | */ |
26 | |
27 | /** |
28 | * Short description of class common_cache_PartitionedCachable |
29 | * |
30 | * @abstract |
31 | * @access public |
32 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
33 | * @package generis |
34 | |
35 | */ |
36 | abstract class common_cache_PartitionedCachable implements common_Serializable |
37 | { |
38 | // --- ASSOCIATIONS --- |
39 | |
40 | |
41 | // --- ATTRIBUTES --- |
42 | |
43 | /** |
44 | * Short description of attribute serial |
45 | * |
46 | * @access protected |
47 | * @var string |
48 | */ |
49 | protected $serial = ''; |
50 | |
51 | /** |
52 | * Short description of attribute serializedProperties |
53 | * |
54 | * @access protected |
55 | * @var array |
56 | */ |
57 | protected $serializedProperties = []; |
58 | |
59 | // --- OPERATIONS --- |
60 | |
61 | /** |
62 | * Obtain a serial for the instance of the class that implements the |
63 | * |
64 | * @access public |
65 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
66 | * @return string |
67 | */ |
68 | public function getSerial() |
69 | { |
70 | $returnValue = (string) ''; |
71 | |
72 | |
73 | if (empty($this->serial)) { |
74 | $this->serial = $this->buildSerial(); |
75 | } |
76 | $returnValue = $this->serial; |
77 | |
78 | |
79 | return (string) $returnValue; |
80 | } |
81 | |
82 | /** |
83 | * Short description of method __construct |
84 | * |
85 | * @access public |
86 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
87 | * @return mixed |
88 | */ |
89 | public function __construct() |
90 | { |
91 | |
92 | if (!is_null($this->getCache())) { |
93 | $this->getCache()->put($this); |
94 | } |
95 | } |
96 | |
97 | /** |
98 | * Gives the list of attributes to serialize by reflection. |
99 | * |
100 | * @access public |
101 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
102 | * @return array |
103 | */ |
104 | public function __sleep() |
105 | { |
106 | $returnValue = []; |
107 | |
108 | |
109 | $this->serializedProperties = []; |
110 | $reflection = new ReflectionClass($this); |
111 | foreach ($reflection->getProperties() as $property) { |
112 | //assuming that private properties don't contain serializables |
113 | if (!$property->isStatic() && !$property->isPrivate()) { |
114 | $propertyName = $property->getName(); |
115 | $containsSerializable = false; |
116 | $value = $this->$propertyName; |
117 | if (is_array($value)) { |
118 | $containsNonSerializable = false; |
119 | $serials = []; |
120 | foreach ($value as $key => $subvalue) { |
121 | if (is_object($subvalue) && $subvalue instanceof self) { |
122 | $containsSerializable = true; |
123 | $serials[$key] = $subvalue->getSerial(); |
124 | } else { |
125 | $containsNonSerializable = true; |
126 | } |
127 | } |
128 | if ($containsNonSerializable && $containsSerializable) { |
129 | throw new common_exception_Error( |
130 | 'Serializable ' . $this->getSerial() |
131 | . ' mixed serializable and non serializable values in property ' . $propertyName |
132 | ); |
133 | } |
134 | } else { |
135 | if (is_object($value) && $value instanceof self) { |
136 | $containsSerializable = true; |
137 | $serials = $value->getSerial(); |
138 | } |
139 | } |
140 | if ($containsSerializable) { |
141 | $this->serializedProperties[$property->getName()] = $serials; |
142 | } else { |
143 | $returnValue[] = $property->getName(); |
144 | } |
145 | } |
146 | } |
147 | |
148 | |
149 | return (array) $returnValue; |
150 | } |
151 | |
152 | /** |
153 | * Short description of method __wakeup |
154 | * |
155 | * @access public |
156 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
157 | * @return mixed |
158 | */ |
159 | public function __wakeup() |
160 | { |
161 | |
162 | foreach ($this->serializedProperties as $key => $value) { |
163 | if (is_array($value)) { |
164 | $restored = []; |
165 | foreach ($value as $arrayKey => $arrayValue) { |
166 | $restored[$arrayKey] = $this->getCache()->get($arrayValue); |
167 | } |
168 | } else { |
169 | $restored = $this->getCache()->get($value); |
170 | } |
171 | $this->$key = $restored; |
172 | } |
173 | $this->serializedProperties = []; |
174 | } |
175 | |
176 | /** |
177 | * Short description of method _remove |
178 | * |
179 | * @access public |
180 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
181 | * @return mixed |
182 | * |
183 | * phpcs:disable PSR2.Methods.MethodDeclaration |
184 | */ |
185 | public function _remove() |
186 | { |
187 | |
188 | //usefull only when persistance is enabled |
189 | if (!is_null($this->getCache())) { |
190 | //clean session |
191 | $this->getCache()->remove($this->getSerial()); |
192 | } |
193 | } |
194 | // phpcs:enable PSR2.Methods.MethodDeclaration |
195 | |
196 | /** |
197 | * Short description of method getSuccessors |
198 | * |
199 | * @access public |
200 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
201 | * @return array |
202 | */ |
203 | public function getSuccessors() |
204 | { |
205 | $returnValue = []; |
206 | |
207 | |
208 | $reflection = new ReflectionClass($this); |
209 | foreach ($reflection->getProperties() as $property) { |
210 | if (!$property->isStatic() && !$property->isPrivate()) { |
211 | $propertyName = $property->getName(); |
212 | $value = $this->$propertyName; |
213 | if (is_array($value)) { |
214 | foreach ($value as $key => $subvalue) { |
215 | if (is_object($subvalue) && $subvalue instanceof self) { |
216 | $returnValue[] = $subvalue; |
217 | } |
218 | } |
219 | } elseif (is_object($value) && $value instanceof self) { |
220 | $returnValue[] = $value; |
221 | } |
222 | } |
223 | } |
224 | |
225 | |
226 | return (array) $returnValue; |
227 | } |
228 | |
229 | /** |
230 | * Short description of method getPredecessors |
231 | * |
232 | * @access public |
233 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
234 | * @param string classFilter |
235 | * @return array |
236 | */ |
237 | public function getPredecessors($classFilter = null) |
238 | { |
239 | $returnValue = []; |
240 | |
241 | |
242 | foreach ($this->getCache()->getAll() as $serial => $instance) { |
243 | if ( |
244 | ($classFilter == null || $instance instanceof $classFilter) |
245 | && in_array($this, $instance->getSuccessors()) |
246 | ) { |
247 | $returnValue[] = $instance; |
248 | break; |
249 | } |
250 | } |
251 | |
252 | |
253 | return (array) $returnValue; |
254 | } |
255 | |
256 | /** |
257 | * Short description of method buildSerial |
258 | * |
259 | * @abstract |
260 | * @access protected |
261 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
262 | * @return string |
263 | */ |
264 | abstract protected function buildSerial(); |
265 | |
266 | /** |
267 | * Short description of method getCache |
268 | * |
269 | * @abstract |
270 | * @access public |
271 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
272 | * @return common_cache_Cache |
273 | */ |
274 | abstract public function getCache(); |
275 | } |