|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spiral\Core\Internal\Tracer; |
|
6
|
|
|
|
|
7
|
|
|
use Spiral\Core\Exception\Traits\ClosureRendererTrait; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @internal |
|
11
|
|
|
*/ |
|
12
|
|
|
final class Trace implements \Stringable |
|
13
|
|
|
{ |
|
14
|
|
|
use ClosureRendererTrait; |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
private const ARRAY_MAX_LEVEL = 3; |
|
17
|
|
|
|
|
18
|
|
|
public readonly ?string $alias; |
|
19
|
|
|
|
|
20
|
944 |
|
public function __construct( |
|
21
|
|
|
public array $info = [], |
|
22
|
|
|
) { |
|
23
|
944 |
|
$this->alias = $info['alias'] ?? null; |
|
|
|
|
|
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
508 |
|
public function __toString(): string |
|
27
|
|
|
{ |
|
28
|
508 |
|
$info = []; |
|
29
|
508 |
|
foreach ($this->info as $key => $item) { |
|
30
|
508 |
|
$info[] = "$key: {$this->stringifyValue($item)}"; |
|
31
|
|
|
} |
|
32
|
508 |
|
return \implode("\n", $info); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
508 |
|
private function stringifyValue(mixed $item): string |
|
36
|
|
|
{ |
|
37
|
508 |
|
return match (true) { |
|
38
|
508 |
|
\is_string($item) => "'$item'", |
|
39
|
508 |
|
\is_scalar($item) => \var_export($item, true), |
|
40
|
508 |
|
$item instanceof \Closure => $this->renderClosureSignature(new \ReflectionFunction($item)), |
|
41
|
508 |
|
$item instanceof \ReflectionFunctionAbstract => $this->renderClosureSignature($item), |
|
42
|
508 |
|
$item instanceof \UnitEnum => $item::class . "::$item->name", |
|
43
|
508 |
|
\is_object($item) => $item instanceof \Stringable ? (string) $item : 'instance of ' . $item::class, |
|
44
|
508 |
|
\is_array($item) => $this->renderArray($item), |
|
45
|
508 |
|
default => \get_debug_type($item), |
|
46
|
508 |
|
}; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
19 |
|
private function renderArray(array $array, int $level = 0): string |
|
50
|
|
|
{ |
|
51
|
19 |
|
if ($array === []) { |
|
52
|
|
|
return '[]'; |
|
53
|
|
|
} |
|
54
|
19 |
|
if ($level >= self::ARRAY_MAX_LEVEL) { |
|
55
|
|
|
return 'array'; |
|
56
|
|
|
} |
|
57
|
19 |
|
$result = []; |
|
58
|
19 |
|
foreach ($array as $key => $value) { |
|
59
|
19 |
|
$result[] = \sprintf( |
|
60
|
19 |
|
'%s: %s', |
|
61
|
19 |
|
$key, |
|
62
|
19 |
|
\is_array($value) |
|
63
|
|
|
? $this->renderArray($value, $level + 1) |
|
64
|
19 |
|
: $this->stringifyValue($value), |
|
65
|
19 |
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
19 |
|
$pad = \str_repeat(' ', $level); |
|
69
|
19 |
|
return "[\n $pad" . \implode(",\n $pad", $result) . "\n$pad]"; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|