-
Notifications
You must be signed in to change notification settings - Fork 4
/
LogEntry.php
317 lines (300 loc) · 9.48 KB
/
LogEntry.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
/**
* This file is part of PHPDebugConsole
*
* @package PHPDebugConsole
* @author Brad Kent <bkfake-github@yahoo.com>
* @license http://opensource.org/licenses/MIT MIT
* @copyright 2014-2024 Brad Kent
* @version v3.0
*/
namespace bdk\Debug;
use bdk\Debug;
use bdk\PubSub\Event;
use JsonSerializable;
/**
* Represents a log entry
*/
class LogEntry extends Event implements JsonSerializable
{
/**
* Regular expression for determining if argument contains "substitutions"
*
* @var non-empty-string
*/
public $subRegex = '/%
(?:
[coO]| # c: css, o: obj with max info, O: obj w generic info
[+-]? # sign specifier
(?:[ 0]|\'.{1})? # padding specifier
-? # alignment specifier
\d* # width specifier
(?:\.\d+)? # precision specifier
[difs]
)
/x';
/**
* Construct a log entry
*
* meta precedence low to high
* $meta
* $argsToMeta
* meta extracted from $args
*
* @param Debug $subject Debug instance
* @param string $method Debug method
* @param array $args arguments passed to method (may include meta args)
* @param array $meta default meta values
* @param array $defaultArgs default arguments (key/value array)
* @param array $argsToMeta move specified keys to meta
*/
public function __construct(Debug $subject, $method, $args = array(), $meta = array(), $defaultArgs = array(), $argsToMeta = array())
{
$this->subject = $subject;
$this->values = array(
'appendLog' => true,
'args' => $args,
'meta' => $meta,
'method' => $method,
'numArgs' => 0, // number of initial non-meta aargs passed (does not include added default values)
'return' => null,
);
$metaExtracted = $this->metaExtract($this->values['args']);
$this->mergeDefaultArgs($defaultArgs, $argsToMeta);
$this->values['meta'] = \array_merge($this->values['meta'], $metaExtracted);
$this->onSet($this->values);
}
/**
* Do the logEntry arguments appear to have string substitutions
*
* @return bool
*/
public function containsSubstitutions()
{
$args = $this->values['args'];
if (\count($args) < 2 || \is_string($args[0]) === false) {
return false;
}
return \preg_match($this->subRegex, $args[0]) === 1;
}
/**
* "crate" all logEntry arguments
*
* @return void
*/
public function crate()
{
$firstArgVal = \reset($this->values['args']);
if ($this->subject->php->isThrowable($firstArgVal)) {
$exception = $firstArgVal;
$this->values['args'][0] = $exception->getMessage();
$this->setMeta(array(
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'trace' => $this->subject->backtrace->get(null, 0, $exception),
));
}
$cfgRestore = array();
if (\in_array($this->values['method'], array('profileEnd', 'table', 'trace'), true)) {
$maxDepth = $this->subject->getCfg('maxDepth');
if ($maxDepth === 1) {
$this->subject->setCfg('maxDepth', 2, Debug::CONFIG_NO_RETURN);
$cfgRestore = array('maxDepth' => $maxDepth);
}
}
foreach ($this->values['args'] as $i => $val) {
$this->values['args'][$i] = $this->subject->abstracter->crate($val, $this->values['method']);
}
$this->subject->setCfg($cfgRestore, Debug::CONFIG_NO_RETURN);
}
/**
* Return an array containing method, args, & meta
*
* @return array
*/
public function export()
{
// phpcs:ignore SlevomatCodingStandard.Arrays.AlphabeticallySortedByKeys.IncorrectKeyOrder
$return = array(
'method' => $this->values['method'],
'args' => $this->values['args'],
'meta' => $this->values['meta'],
);
if ($this->subject->parentInstance) {
$return['meta']['channel'] = $this->getChannelName();
}
return $return;
}
/**
* Return channel name
*
* shortcut for getSubject()->getCfg('channelName')
*
* @return string
*/
public function getChannelName()
{
return $this->subject->getCfg('channelName', Debug::CONFIG_DEBUG);
}
/**
* Get meta value
*
* @param string $key key to get
* if not passed, return all meta values (no different than $logEntry['meta'])
* @param mixed $default (null) value to get
*
* @return mixed
*/
public function getMeta($key = null, $default = null)
{
if ($key === null) {
return $this->values['meta'];
}
return \array_key_exists($key, $this->values['meta'])
? $this->values['meta'][$key]
: $default;
}
/**
* Specify data which should be serialized to JSON
*
* @return array
*/
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return $this->export();
}
/**
* Set meta value(s)
* Value(s) get merged with existing values
*
* @param mixed $mixed (string) key or (array) key/value array
* @param mixed $val value if updating a single key
*
* @return void
*/
public function setMeta($mixed, $val = null)
{
if (\is_array($mixed) === false) {
if ($val === null) {
/** @psalm-suppress EmptyArrayAccess */
unset($this->values['meta'][$mixed]);
return;
}
$mixed = array($mixed => $val);
}
$this->setValue('meta', \array_merge($this->values['meta'], $mixed));
}
/**
* Merge default args with args
* Move args listed in argsToMeta to meta
*
* @param array $defaultArgs default arguments (key=>value array)
* @param array $argsToMeta move specified keys to meta
*
* @return void
*/
private function mergeDefaultArgs($defaultArgs, $argsToMeta)
{
if (!$defaultArgs) {
return;
}
$count = \count($defaultArgs);
$args = \array_slice($this->values['args'], 0, $count);
$argsMore = \array_slice($this->values['args'], $count);
$args = \array_combine(
\array_keys($defaultArgs),
\array_replace(\array_values($defaultArgs), $args)
);
foreach ($argsToMeta as $k) {
$this->values['meta'][$k] = $args[$k];
unset($args[$k]);
}
$this->values['args'] = \array_values($args + $argsMore);
}
/**
* Remove meta values from array
*
* @param array $array array such as an argument array
*
* @return array meta values
*/
private function metaExtract(&$array)
{
$meta = array();
foreach ($array as $i => $v) {
if (\is_array($v) && isset($v['debug']) && $v['debug'] === Debug::META) {
unset($v['debug']);
$meta = \array_merge($meta, $v);
unset($array[$i]);
}
}
$array = \array_values($array); // update $array passed by reference
$this->values['numArgs'] = \count($array);
return $meta;
}
/**
* Make sure attribs['class'] is an array
*
* @param array $values key => values being set
*
* @return void
*/
protected function onSet($values = array())
{
if (isset($values['meta']) === false) {
return;
}
if (isset($values['meta']['appendGroup'])) {
$this->values['meta']['appendGroup'] = $this->subject->html->sanitizeId($values['meta']['appendGroup']);
}
$this->onSetMetaAttribs();
if (\array_key_exists('channel', $values['meta'])) {
$this->onSetMetaChannel();
}
}
/**
* if we have 'attribs' make sure attribs['class'] is set and is an array
*
* @return void
*/
private function onSetMetaAttribs()
{
$meta = $this->values['meta'];
if (isset($meta['id'])) {
// move 'id' to attribs
$meta['attribs']['id'] = $meta['id'];
unset($meta['id']);
}
if (isset($meta['attribs']) === false) {
return;
}
if (!isset($meta['attribs']['class'])) {
$meta['attribs']['class'] = array();
} elseif (\is_string($meta['attribs']['class'])) {
$meta['attribs']['class'] = \explode(' ', $meta['attribs']['class']);
}
if (isset($meta['attribs']['id'])) {
$meta['attribs']['id'] = $this->subject->html->sanitizeId($meta['attribs']['id']);
}
$this->values['meta'] = $meta;
}
/**
* Handle meta['channel']
* Set this->subject from channel value
*
* @return void
*/
private function onSetMetaChannel()
{
$channel = $this->values['meta']['channel'];
unset($this->values['meta']['channel']);
if ($channel === null) {
$this->subject = $this->subject->rootInstance;
return;
}
$this->subject = $this->subject->parentInstance
? $this->subject->parentInstance->getChannel($channel)
: $this->subject->getChannel($channel);
}
}