| 199 |
lars |
1 |
<?php namespace Clockwork\Helpers;
|
|
|
2 |
|
|
|
3 |
// Prepares various types of data for serialization
|
|
|
4 |
class Serializer
|
|
|
5 |
{
|
|
|
6 |
// Serialized objects cache by object hash
|
|
|
7 |
protected $cache = [];
|
|
|
8 |
|
|
|
9 |
// Options for the current instance
|
|
|
10 |
protected $options = [];
|
|
|
11 |
|
|
|
12 |
// Default options for new instances
|
|
|
13 |
protected static $defaults = [
|
|
|
14 |
'blackbox' => [
|
|
|
15 |
\Illuminate\Container\Container::class,
|
|
|
16 |
\Illuminate\Foundation\Application::class,
|
|
|
17 |
\Laravel\Lumen\Application::class
|
|
|
18 |
],
|
|
|
19 |
'limit' => 10,
|
|
|
20 |
'toArray' => false,
|
|
|
21 |
'toString' => false,
|
|
|
22 |
'debugInfo' => true,
|
|
|
23 |
'jsonSerialize' => false,
|
|
|
24 |
'traces' => true,
|
|
|
25 |
'tracesFilter' => null,
|
|
|
26 |
'tracesSkip' => null,
|
|
|
27 |
'tracesLimit' => null
|
|
|
28 |
];
|
|
|
29 |
|
|
|
30 |
// Create a new instance optionally with options overriding defaults
|
|
|
31 |
public function __construct(array $options = [])
|
|
|
32 |
{
|
|
|
33 |
$this->options = $options + static::$defaults;
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
// Set default options for all new instances
|
|
|
37 |
public static function defaults(array $defaults)
|
|
|
38 |
{
|
|
|
39 |
static::$defaults = $defaults + static::$defaults;
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
// Prepares the passed data to be ready for serialization, takes any kind of data to normalize as the first
|
|
|
43 |
// argument, other arguments are used internally in recursion
|
|
|
44 |
public function normalize($data, $context = null, $limit = null)
|
|
|
45 |
{
|
|
|
46 |
if ($context === null) $context = [ 'references' => [] ];
|
|
|
47 |
if ($limit === null) $limit = $this->options['limit'];
|
|
|
48 |
|
|
|
49 |
if (is_array($data)) {
|
|
|
50 |
if ($limit === 0) return [ '__type__' => 'array', '__omitted__' => 'limit' ];
|
|
|
51 |
|
|
|
52 |
return [ '__type__' => 'array' ] + $this->normalizeEach($data, $context, $limit - 1);
|
|
|
53 |
} elseif (is_object($data)) {
|
|
|
54 |
if ($data instanceof \Closure) return [ '__type__' => 'anonymous function' ];
|
|
|
55 |
|
|
|
56 |
$className = get_class($data);
|
|
|
57 |
$objectHash = spl_object_hash($data);
|
|
|
58 |
|
|
|
59 |
if ($limit === 0) return [ '__class__' => $className, '__omitted__' => 'limit' ];
|
|
|
60 |
|
|
|
61 |
if (isset($context['references'][$objectHash])) return [ '__type__' => 'recursion' ];
|
|
|
62 |
|
|
|
63 |
$context['references'][$objectHash] = true;
|
|
|
64 |
|
|
|
65 |
if (isset($this->cache[$objectHash])) return $this->cache[$objectHash];
|
|
|
66 |
|
|
|
67 |
if ($this->options['blackbox'] && in_array($className, $this->options['blackbox'])) {
|
|
|
68 |
return $this->cache[$objectHash] = [ '__class__' => $className, '__omitted__' => 'blackbox' ];
|
|
|
69 |
} elseif ($this->options['toString'] && method_exists($data, '__toString')) {
|
|
|
70 |
return $this->cache[$objectHash] = (string) $data;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
if ($this->options['debugInfo'] && method_exists($data, '__debugInfo')) {
|
|
|
74 |
$data = (array) $data->__debugInfo();
|
|
|
75 |
} elseif ($this->options['jsonSerialize'] && method_exists($data, 'jsonSerialize')) {
|
|
|
76 |
$data = (array) $data->jsonSerialize();
|
|
|
77 |
} elseif ($this->options['toArray'] && method_exists($data, 'toArray')) {
|
|
|
78 |
$data = (array) $data->toArray();
|
|
|
79 |
} else {
|
|
|
80 |
$data = (array) $data;
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
$data = array_combine(
|
|
|
84 |
array_map(function ($key) {
|
|
|
85 |
// replace null-byte prefixes of protected and private properties used by php with * (protected)
|
|
|
86 |
// and ~ (private)
|
|
|
87 |
return preg_replace('/^\0.+?\0/', '~', str_replace("\0*\0", '*', $key));
|
|
|
88 |
}, array_keys($data)),
|
|
|
89 |
$this->normalizeEach($data, $context, $limit - 1)
|
|
|
90 |
);
|
|
|
91 |
|
|
|
92 |
return $this->cache[$objectHash] = [ '__class__' => $className ] + $data;
|
|
|
93 |
} elseif (is_resource($data)) {
|
|
|
94 |
return [ '__type__' => 'resource' ];
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
return $data;
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
// Normalize each member of an array (doesn't add metadata for top level)
|
|
|
101 |
public function normalizeEach($data, $context = null, $limit = null) {
|
|
|
102 |
return array_map(function ($item) use ($context, $limit) {
|
|
|
103 |
return $this->normalize($item, $context, $limit);
|
|
|
104 |
}, $data);
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
// Normalize a stack trace instance
|
|
|
108 |
public function trace(StackTrace $trace)
|
|
|
109 |
{
|
|
|
110 |
if (! $this->options['traces']) return null;
|
|
|
111 |
|
|
|
112 |
if ($this->options['tracesFilter']) $trace = $trace->filter($this->options['tracesFilter']);
|
|
|
113 |
if ($this->options['tracesSkip']) $trace = $trace->skip($this->options['tracesSkip']);
|
|
|
114 |
if ($this->options['tracesLimit']) $trace = $trace->limit($this->options['tracesLimit']);
|
|
|
115 |
|
|
|
116 |
return array_map(function ($frame) {
|
|
|
117 |
return [
|
|
|
118 |
'call' => $frame->call,
|
|
|
119 |
'file' => $frame->file,
|
|
|
120 |
'line' => $frame->line,
|
|
|
121 |
'isVendor' => (bool) $frame->vendor
|
|
|
122 |
];
|
|
|
123 |
}, $trace->frames());
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
// Normalize an exception instance
|
|
|
127 |
public function exception(/* Throwable */ $exception)
|
|
|
128 |
{
|
|
|
129 |
return [
|
|
|
130 |
'type' => get_class($exception),
|
|
|
131 |
'message' => $exception->getMessage(),
|
|
|
132 |
'code' => $exception->getCode(),
|
|
|
133 |
'file' => $exception->getFile(),
|
|
|
134 |
'line' => $exception->getLine(),
|
|
|
135 |
'trace' => (new Serializer([ 'tracesLimit' => false ]))->trace(StackTrace::from($exception->getTrace())),
|
|
|
136 |
'previous' => $exception->getPrevious() ? $this->exception($exception->getPrevious()) : null
|
|
|
137 |
];
|
|
|
138 |
}
|
|
|
139 |
}
|