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
|
1176 |
|
public function __construct( |
21
|
|
|
public array $info = [], |
22
|
|
|
) { |
23
|
1176 |
|
$this->alias = $info['alias'] ?? null; |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
731 |
|
public function __toString(): string |
27
|
|
|
{ |
28
|
731 |
|
$info = []; |
29
|
731 |
|
foreach ($this->info as $key => $item) { |
30
|
731 |
|
$info[] = "$key: {$this->stringifyValue($item)}"; |
31
|
|
|
} |
32
|
731 |
|
return \implode("\n", $info); |
33
|
|
|
} |
34
|
|
|
|
35
|
731 |
|
private function stringifyValue(mixed $item): string |
36
|
|
|
{ |
37
|
|
|
return match (true) { |
38
|
731 |
|
\is_string($item) => "'$item'", |
39
|
731 |
|
\is_scalar($item) => \var_export($item, true), |
40
|
731 |
|
$item instanceof \Closure => $this->renderClosureSignature(new \ReflectionFunction($item)), |
41
|
731 |
|
$item instanceof \ReflectionFunctionAbstract => $this->renderClosureSignature($item), |
42
|
731 |
|
$item instanceof \UnitEnum => $item::class . "::$item->name", |
43
|
731 |
|
\is_object($item) => $item instanceof \Stringable ? (string) $item : 'instance of ' . $item::class, |
44
|
724 |
|
\is_array($item) => $this->renderArray($item), |
45
|
731 |
|
default => \get_debug_type($item), |
46
|
|
|
}; |
47
|
|
|
} |
48
|
|
|
|
49
|
8 |
|
private function renderArray(array $array, int $level = 0): string |
50
|
|
|
{ |
51
|
8 |
|
if ($array === []) { |
52
|
|
|
return '[]'; |
53
|
|
|
} |
54
|
8 |
|
if ($level >= self::ARRAY_MAX_LEVEL) { |
55
|
|
|
return 'array'; |
56
|
|
|
} |
57
|
8 |
|
$result = []; |
58
|
8 |
|
foreach ($array as $key => $value) { |
59
|
8 |
|
$result[] = \sprintf( |
60
|
8 |
|
'%s: %s', |
61
|
8 |
|
$key, |
62
|
8 |
|
\is_array($value) |
63
|
|
|
? $this->renderArray($value, $level + 1) |
64
|
8 |
|
: $this->stringifyValue($value), |
65
|
8 |
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
8 |
|
$pad = \str_repeat(' ', $level); |
69
|
8 |
|
return "[\n $pad" . \implode(",\n $pad", $result) . "\n$pad]"; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|