1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Core\Internal; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @internal |
9
|
|
|
*/ |
10
|
|
|
final class Tracer implements \Stringable |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Trace blocks |
14
|
|
|
* |
15
|
|
|
* @var Trace[][] |
16
|
|
|
*/ |
17
|
|
|
private array $traces = []; |
18
|
|
|
|
19
|
421 |
|
public function __toString(): string |
20
|
|
|
{ |
21
|
421 |
|
return $this->traces === [] ? '' : "Container trace list:\n" . $this->renderTraceList($this->traces); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $header Message before stack list |
26
|
|
|
* @param bool $lastBlock Generate trace list only for last block |
27
|
|
|
* @param bool $clear Remove touched trace list |
28
|
|
|
*/ |
29
|
421 |
|
public function combineTraceMessage(string $header, bool $lastBlock = false, bool $clear = false): string |
|
|
|
|
30
|
|
|
{ |
31
|
421 |
|
return "$header\n$this"; |
32
|
|
|
} |
33
|
|
|
|
34
|
742 |
|
public function push(bool $nextLevel, mixed ...$details): void |
35
|
|
|
{ |
36
|
742 |
|
$trace = $details === [] ? null : new Trace($details); |
37
|
742 |
|
if ($nextLevel || $this->traces === []) { |
38
|
742 |
|
$this->traces[] = $trace === null ? [] : [$trace]; |
39
|
683 |
|
} elseif ($trace !== null) { |
40
|
683 |
|
$this->traces[\array_key_last($this->traces)][] = $trace; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
742 |
|
public function pop(bool $previousLevel = false): void |
45
|
|
|
{ |
46
|
742 |
|
if ($this->traces === []) { |
47
|
|
|
return; |
48
|
|
|
} |
49
|
742 |
|
if ($previousLevel) { |
50
|
709 |
|
\array_pop($this->traces); |
51
|
709 |
|
return; |
52
|
|
|
} |
53
|
742 |
|
$key = \array_key_last($this->traces); |
54
|
742 |
|
$list = &$this->traces[$key]; |
55
|
742 |
|
\array_pop($list); |
56
|
|
|
} |
57
|
|
|
|
58
|
415 |
|
public function getRootAlias(): string |
59
|
|
|
{ |
60
|
415 |
|
return $this->traces[0][0]->alias ?? ''; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Trace[][] $blocks |
65
|
|
|
*/ |
66
|
421 |
|
private function renderTraceList(array $blocks): string |
67
|
|
|
{ |
68
|
421 |
|
$result = []; |
69
|
421 |
|
$i = 0; |
70
|
421 |
|
foreach ($blocks as $block) { |
71
|
421 |
|
\array_push($result, ...$this->blockToStringList($block, $i++)); |
72
|
|
|
} |
73
|
421 |
|
return \implode("\n", $result); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param Trace[] $items |
78
|
|
|
* @param int<0, max> $level |
79
|
|
|
* |
80
|
|
|
* @return string[] |
81
|
|
|
*/ |
82
|
421 |
|
private function blockToStringList(array $items, int $level = 0): array |
83
|
|
|
{ |
84
|
421 |
|
$result = []; |
85
|
421 |
|
$padding = \str_repeat(' ', $level); |
86
|
421 |
|
$firstPrefix = "$padding- "; |
87
|
|
|
// Separator |
88
|
421 |
|
$s = "\n"; |
89
|
421 |
|
$nexPrefix = "$s$padding "; |
90
|
421 |
|
foreach ($items as $item) { |
91
|
421 |
|
$result[] = $firstPrefix . \str_replace($s, $nexPrefix, (string)$item); |
92
|
|
|
} |
93
|
421 |
|
return $result; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.