|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the O2System Framework package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Steeve Andrian Salim |
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
|
13
|
|
|
|
|
14
|
|
|
namespace O2System\Reactor\Containers; |
|
15
|
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
|
17
|
|
|
|
|
18
|
|
|
use Psr\Container\ContainerExceptionInterface; |
|
19
|
|
|
use Psr\Container\ContainerInterface; |
|
20
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
|
21
|
|
|
use Traversable; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class Globals |
|
25
|
|
|
* |
|
26
|
|
|
* @package O2System\Reactor\Container |
|
27
|
|
|
*/ |
|
28
|
|
|
class Globals implements |
|
29
|
|
|
\ArrayAccess, |
|
30
|
|
|
\IteratorAggregate, |
|
31
|
|
|
\Countable, |
|
32
|
|
|
\Serializable, |
|
33
|
|
|
\JsonSerializable, |
|
34
|
|
|
ContainerInterface |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* Globals::getIterator |
|
38
|
|
|
* |
|
39
|
|
|
* Retrieve an external iterator |
|
40
|
|
|
* |
|
41
|
|
|
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
|
42
|
|
|
* @return Traversable An instance of an object implementing <b>Iterator</b> or |
|
43
|
|
|
* <b>Traversable</b> |
|
44
|
|
|
* @since 5.0.0 |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getIterator() |
|
47
|
|
|
{ |
|
48
|
|
|
return new \ArrayIterator($GLOBALS); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// ------------------------------------------------------------------------ |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Globals::exists |
|
55
|
|
|
* |
|
56
|
|
|
* Checks if the data exists on the storage. |
|
57
|
|
|
* An alias of Globals::__isset method. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $offset The object offset key. |
|
60
|
|
|
* |
|
61
|
|
|
* @return bool Returns TRUE on success or FALSE on failure. |
|
62
|
|
|
*/ |
|
63
|
|
|
public function exists($offset) |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->__isset($offset); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// ------------------------------------------------------------------------ |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Globals::__isset |
|
72
|
|
|
* |
|
73
|
|
|
* Implementing magic method __isset to simplify when checks if offset exists on PHP native session variable, |
|
74
|
|
|
* just simply calling isset( $globals[ 'offset' ] ). |
|
75
|
|
|
* |
|
76
|
|
|
* @param mixed $offset PHP native GLOBALS offset. |
|
77
|
|
|
* |
|
78
|
|
|
* @return bool |
|
79
|
|
|
*/ |
|
80
|
|
|
public function __isset($offset) |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->offsetExists($offset); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
// ------------------------------------------------------------------------ |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Globals::offsetExists |
|
89
|
|
|
* |
|
90
|
|
|
* Whether a offset exists |
|
91
|
|
|
* |
|
92
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
93
|
|
|
* |
|
94
|
|
|
* @param mixed $offset <p> |
|
95
|
|
|
* An offset to check for. |
|
96
|
|
|
* </p> |
|
97
|
|
|
* |
|
98
|
|
|
* @return boolean true on success or false on failure. |
|
99
|
|
|
* </p> |
|
100
|
|
|
* <p> |
|
101
|
|
|
* The return value will be casted to boolean if non-boolean was returned. |
|
102
|
|
|
* @since 5.0.0 |
|
103
|
|
|
*/ |
|
104
|
|
|
public function offsetExists($offset) |
|
105
|
|
|
{ |
|
106
|
|
|
return isset($GLOBALS[ $offset ]); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
// ------------------------------------------------------------------------ |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Globals::__get |
|
113
|
|
|
* |
|
114
|
|
|
* Implementing magic method __get to simplify gets PHP native session variable by requested offset, |
|
115
|
|
|
* just simply calling isset( $session[ 'offset' ] ). |
|
116
|
|
|
* |
|
117
|
|
|
* @param $offset |
|
118
|
|
|
* |
|
119
|
|
|
* @return mixed |
|
120
|
|
|
*/ |
|
121
|
|
|
public function &__get($offset) |
|
122
|
|
|
{ |
|
123
|
|
|
return $GLOBALS[ $offset ]; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
// ------------------------------------------------------------------------ |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Globals::__set |
|
130
|
|
|
* |
|
131
|
|
|
* Implementing magic method __set to simplify set PHP native GLOBALS variable, |
|
132
|
|
|
* just simply calling $globals->offset = 'foo'. |
|
133
|
|
|
* |
|
134
|
|
|
* @param mixed $offset PHP native GLOBALS offset. |
|
135
|
|
|
* @param mixed $value PHP native GLOBALS offset value to set. |
|
136
|
|
|
*/ |
|
137
|
|
|
public function __set($offset, $value) |
|
138
|
|
|
{ |
|
139
|
|
|
$this->offsetSet($offset, $value); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
// ------------------------------------------------------------------------ |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Globals::store |
|
146
|
|
|
* |
|
147
|
|
|
* Store the data into the storage. |
|
148
|
|
|
* An alias of Globals::__set method. |
|
149
|
|
|
* |
|
150
|
|
|
* @param string $offset The data offset key. |
|
151
|
|
|
* @param mixed $value The data to be stored. |
|
152
|
|
|
* |
|
153
|
|
|
* @return void |
|
154
|
|
|
*/ |
|
155
|
|
|
public function store($offset, $value) |
|
156
|
|
|
{ |
|
157
|
|
|
$this->__set($offset, $value); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
// ------------------------------------------------------------------------ |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Globals::offsetSet |
|
164
|
|
|
* |
|
165
|
|
|
* Offset to set |
|
166
|
|
|
* |
|
167
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
|
168
|
|
|
* |
|
169
|
|
|
* @param mixed $offset <p> |
|
170
|
|
|
* The offset to assign the value to. |
|
171
|
|
|
* </p> |
|
172
|
|
|
* @param mixed $value <p> |
|
173
|
|
|
* The value to set. |
|
174
|
|
|
* </p> |
|
175
|
|
|
* |
|
176
|
|
|
* @return void |
|
177
|
|
|
* @since 5.0.0 |
|
178
|
|
|
*/ |
|
179
|
|
|
public function offsetSet($offset, $value) |
|
180
|
|
|
{ |
|
181
|
|
|
$GLOBALS[ $offset ] = $value; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
// ------------------------------------------------------------------------ |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Globals::remove |
|
188
|
|
|
* |
|
189
|
|
|
* Removes a data from the storage. |
|
190
|
|
|
* An alias of Globals::__unset method. |
|
191
|
|
|
* |
|
192
|
|
|
* @param string $offset The object offset key. |
|
193
|
|
|
* |
|
194
|
|
|
* @return void |
|
195
|
|
|
*/ |
|
196
|
|
|
public function remove($offset) |
|
197
|
|
|
{ |
|
198
|
|
|
$this->__unset($offset); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
// ------------------------------------------------------------------------ |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Globals::__unset |
|
205
|
|
|
* |
|
206
|
|
|
* Implementing magic method __unset to simplify unset method, just simply calling |
|
207
|
|
|
* unset( $globals[ 'offset' ] ). |
|
208
|
|
|
* |
|
209
|
|
|
* @param mixed $offset PHP Native GLOBALS offset |
|
210
|
|
|
* |
|
211
|
|
|
* @return void |
|
212
|
|
|
*/ |
|
213
|
|
|
public function __unset($offset) |
|
214
|
|
|
{ |
|
215
|
|
|
$this->offsetUnset($offset); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
// ------------------------------------------------------------------------ |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Globals::offsetUnset |
|
222
|
|
|
* |
|
223
|
|
|
* Offset to unset |
|
224
|
|
|
* |
|
225
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
|
226
|
|
|
* |
|
227
|
|
|
* @param mixed $offset <p> |
|
228
|
|
|
* The offset to unset. |
|
229
|
|
|
* </p> |
|
230
|
|
|
* |
|
231
|
|
|
* @return void |
|
232
|
|
|
* @since 5.0.0 |
|
233
|
|
|
*/ |
|
234
|
|
|
public function offsetUnset($offset) |
|
235
|
|
|
{ |
|
236
|
|
|
if (isset($GLOBALS[ $offset ])) { |
|
237
|
|
|
unset($GLOBALS[ $offset ]); |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
// ------------------------------------------------------------------------ |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Globals::merge |
|
245
|
|
|
* |
|
246
|
|
|
* Merge new array of data into the data storage. |
|
247
|
|
|
* |
|
248
|
|
|
* @param array $data New array of data. |
|
249
|
|
|
* |
|
250
|
|
|
* @return array The old array of data storage. |
|
251
|
|
|
*/ |
|
252
|
|
|
public function merge(array $data) |
|
253
|
|
|
{ |
|
254
|
|
|
$oldData = $GLOBALS; |
|
255
|
|
|
$GLOBALS = array_merge($GLOBALS, $data); |
|
256
|
|
|
|
|
257
|
|
|
return $oldData; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
// ------------------------------------------------------------------------ |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* Globals::exchange |
|
264
|
|
|
* |
|
265
|
|
|
* Exchange the array of data storage into the new array of data. |
|
266
|
|
|
* |
|
267
|
|
|
* @param array $data New array of data. |
|
268
|
|
|
* |
|
269
|
|
|
* @return array The old array of data storage. |
|
270
|
|
|
*/ |
|
271
|
|
|
public function exchange(array $data) |
|
272
|
|
|
{ |
|
273
|
|
|
$oldData = $GLOBALS; |
|
274
|
|
|
$GLOBALS = $data; |
|
275
|
|
|
|
|
276
|
|
|
return $oldData; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
// ------------------------------------------------------------------------ |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* Globals::destroy |
|
283
|
|
|
* |
|
284
|
|
|
* Removes all object from the container and perform each object destruction. |
|
285
|
|
|
* |
|
286
|
|
|
* @return array Array of old storage items. |
|
287
|
|
|
*/ |
|
288
|
|
|
public function destroy() |
|
289
|
|
|
{ |
|
290
|
|
|
$storage = $GLOBALS; |
|
291
|
|
|
|
|
292
|
|
|
$GLOBALS = []; |
|
293
|
|
|
|
|
294
|
|
|
return $storage; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
// ------------------------------------------------------------------------ |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* Globals::count |
|
301
|
|
|
* |
|
302
|
|
|
* Application of Countable::count method to count the numbers of contained objects. |
|
303
|
|
|
* |
|
304
|
|
|
* @see http://php.net/manual/en/countable.count.php |
|
305
|
|
|
* @return int The numbers of data on the storage. |
|
306
|
|
|
*/ |
|
307
|
|
|
public function count() |
|
308
|
|
|
{ |
|
309
|
|
|
return (int)count($GLOBALS); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
// ------------------------------------------------------------------------ |
|
313
|
|
|
|
|
314
|
|
|
/** |
|
315
|
|
|
* Globals::serialize |
|
316
|
|
|
* |
|
317
|
|
|
* Application of Serializable::serialize method to serialize the data storage. |
|
318
|
|
|
* |
|
319
|
|
|
* @see http://php.net/manual/en/serializable.serialize.php |
|
320
|
|
|
* |
|
321
|
|
|
* @return string The string representation of the serialized data storage. |
|
322
|
|
|
*/ |
|
323
|
|
|
public function serialize() |
|
324
|
|
|
{ |
|
325
|
|
|
return serialize($GLOBALS); |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
// ------------------------------------------------------------------------ |
|
329
|
|
|
|
|
330
|
|
|
/** |
|
331
|
|
|
* Globals::unserialize |
|
332
|
|
|
* |
|
333
|
|
|
* Application of Serializable::unserialize method to unserialize and construct the data storage. |
|
334
|
|
|
* |
|
335
|
|
|
* @see http://php.net/manual/en/serializable.unserialize.php |
|
336
|
|
|
* |
|
337
|
|
|
* @param string $serialized The string representation of the serialized data storage. |
|
338
|
|
|
* |
|
339
|
|
|
* @return void |
|
340
|
|
|
*/ |
|
341
|
|
|
public function unserialize($serialized) |
|
342
|
|
|
{ |
|
343
|
|
|
$GLOBALS = unserialize($serialized); |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
// ------------------------------------------------------------------------ |
|
347
|
|
|
|
|
348
|
|
|
/** |
|
349
|
|
|
* Globals::jsonSerialize |
|
350
|
|
|
* |
|
351
|
|
|
* Specify data which should be serialized to JSON |
|
352
|
|
|
* |
|
353
|
|
|
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
354
|
|
|
* @return mixed data which can be serialized by <b>json_encode</b>, |
|
355
|
|
|
* which is a value of any type other than a resource. |
|
356
|
|
|
* @since 5.4.0 |
|
357
|
|
|
*/ |
|
358
|
|
|
public function jsonSerialize() |
|
359
|
|
|
{ |
|
360
|
|
|
return $GLOBALS; |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
// ------------------------------------------------------------------------ |
|
364
|
|
|
|
|
365
|
|
|
/** |
|
366
|
|
|
* Globals::getArrayCopy |
|
367
|
|
|
* |
|
368
|
|
|
* Gets a copy of the data storage. |
|
369
|
|
|
* |
|
370
|
|
|
* @return array Returns a copy of the data storage. |
|
371
|
|
|
*/ |
|
372
|
|
|
public function getArrayCopy() |
|
373
|
|
|
{ |
|
374
|
|
|
return $GLOBALS; |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
|
|
// ------------------------------------------------------------------------ |
|
378
|
|
|
|
|
379
|
|
|
/** |
|
380
|
|
|
* Globals::get |
|
381
|
|
|
* |
|
382
|
|
|
* Finds an entry of the container by its identifier and returns it. |
|
383
|
|
|
* |
|
384
|
|
|
* @param string $id Identifier of the entry to look for. |
|
385
|
|
|
* |
|
386
|
|
|
* @throws NotFoundExceptionInterface No entry was found for **this** identifier. |
|
387
|
|
|
* @throws ContainerExceptionInterface Error while retrieving the entry. |
|
388
|
|
|
* |
|
389
|
|
|
* @return mixed Entry. |
|
390
|
|
|
*/ |
|
391
|
|
|
public function get($id) |
|
392
|
|
|
{ |
|
393
|
|
|
if ($this->has($id)) { |
|
394
|
|
|
return $this->offsetGet($id); |
|
395
|
|
|
} |
|
396
|
|
|
|
|
397
|
|
|
// @todo throw exception |
|
398
|
|
|
} |
|
399
|
|
|
|
|
400
|
|
|
// ------------------------------------------------------------------------ |
|
401
|
|
|
|
|
402
|
|
|
/** |
|
403
|
|
|
* Globals::has |
|
404
|
|
|
* |
|
405
|
|
|
* Returns true if the container can return an entry for the given identifier. |
|
406
|
|
|
* Returns false otherwise. |
|
407
|
|
|
* |
|
408
|
|
|
* `has($id)` returning true does not mean that `get($id)` will not throw an exception. |
|
409
|
|
|
* It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. |
|
410
|
|
|
* |
|
411
|
|
|
* @param string $id Identifier of the entry to look for. |
|
412
|
|
|
* |
|
413
|
|
|
* @return bool |
|
414
|
|
|
*/ |
|
415
|
|
|
public function has($id) |
|
416
|
|
|
{ |
|
417
|
|
|
return (bool)$this->offsetExists($id); |
|
418
|
|
|
} |
|
419
|
|
|
|
|
420
|
|
|
// ------------------------------------------------------------------------ |
|
421
|
|
|
|
|
422
|
|
|
/** |
|
423
|
|
|
* Globals::offsetGet |
|
424
|
|
|
* |
|
425
|
|
|
* Offset to retrieve |
|
426
|
|
|
* |
|
427
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
428
|
|
|
* |
|
429
|
|
|
* @param mixed $offset <p> |
|
430
|
|
|
* The offset to retrieve. |
|
431
|
|
|
* </p> |
|
432
|
|
|
* |
|
433
|
|
|
* @return mixed Can return all value types. |
|
434
|
|
|
* @since 5.0.0 |
|
435
|
|
|
*/ |
|
436
|
|
|
public function offsetGet($offset) |
|
437
|
|
|
{ |
|
438
|
|
|
return (isset($GLOBALS[ $offset ])) ? $GLOBALS[ $offset ] : false; |
|
439
|
|
|
} |
|
440
|
|
|
} |