|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Util |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) Gjero Krsteski (http://krsteski.de) |
|
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Pimf\Util; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Due to PHP Bug #39736 - serialize() consumes insane amount of RAM. |
|
13
|
|
|
* |
|
14
|
|
|
* Now we can put objects, strings, integers or arrays. Even instances of SimpleXMLElement can be put too! |
|
15
|
|
|
* |
|
16
|
|
|
* @package Util |
|
17
|
|
|
* @link https://bugs.php.net/bug.php?id=39736 |
|
18
|
|
|
* @author Gjero Krsteski <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class Serializer |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Serialize things. |
|
24
|
|
|
* |
|
25
|
|
|
* @param mixed $object Item you want - string, array, integer, object |
|
26
|
|
|
* |
|
27
|
|
|
* @return string Containing a byte-stream representation. |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function serialize($object) |
|
30
|
|
|
{ |
|
31
|
|
|
$masked = false; |
|
32
|
|
|
|
|
33
|
|
|
if (false === is_object($object)) { |
|
34
|
|
|
$object = self::mask($object); |
|
35
|
|
|
$masked = true; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$capsule = new \stdClass(); |
|
39
|
|
|
$capsule->type = get_class($object); |
|
40
|
|
|
$capsule->object = $object; |
|
41
|
|
|
$capsule->fake = $masked; |
|
42
|
|
|
|
|
43
|
|
|
if ($object instanceof \SimpleXMLElement) { |
|
44
|
|
|
$capsule->object = $object->asXml(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return '' . self::serializeNative($capsule); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Unserialize things. |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $object Serialized object. |
|
54
|
|
|
* |
|
55
|
|
|
* @return mixed |
|
56
|
|
|
*/ |
|
57
|
|
|
public static function unserialize($object) |
|
58
|
|
|
{ |
|
59
|
|
|
$capsule = self::unserializeNative($object); |
|
60
|
|
|
|
|
61
|
|
|
if (true === $capsule->fake) { |
|
62
|
|
|
$capsule->object = self::unmask($capsule->object); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if ($capsule->type == 'SimpleXMLElement') { |
|
66
|
|
|
$capsule->object = simplexml_load_string($capsule->object); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $capsule->object; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param \stdClass $value Item value. |
|
74
|
|
|
* |
|
75
|
|
|
* @throws \RuntimeException If error during serialize. |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function serializeNative($value) |
|
79
|
|
|
{ |
|
80
|
|
|
return (extension_loaded('igbinary') && function_exists('igbinary_serialize')) |
|
81
|
|
|
? igbinary_serialize($value) |
|
82
|
|
|
: serialize($value); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param string $serialized The serialized item-string. |
|
87
|
|
|
* |
|
88
|
|
|
* @throws \RuntimeException If error during unserialize. |
|
89
|
|
|
* @return mixed |
|
90
|
|
|
*/ |
|
91
|
|
|
public static function unserializeNative($serialized) |
|
92
|
|
|
{ |
|
93
|
|
|
return (extension_loaded('igbinary') && function_exists('igbinary_unserialize')) |
|
94
|
|
|
? igbinary_unserialize($serialized) |
|
95
|
|
|
: unserialize($serialized); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param mixed $item Item |
|
100
|
|
|
* |
|
101
|
|
|
* @return \stdClass |
|
102
|
|
|
*/ |
|
103
|
|
|
private static function mask($item) |
|
104
|
|
|
{ |
|
105
|
|
|
return (object)$item; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param mixed $item Item |
|
110
|
|
|
* |
|
111
|
|
|
* @return array |
|
112
|
|
|
*/ |
|
113
|
|
|
private static function unmask($item) |
|
114
|
|
|
{ |
|
115
|
|
|
if (is_object($item) && property_exists($item, 'scalar')) { |
|
116
|
|
|
return $item->scalar; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return (array)$item; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|