|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace TYPO3Fluid\Fluid\Core\NewParser; |
|
5
|
|
|
|
|
6
|
|
|
use TYPO3Fluid\Fluid\Core\Parser\Context; |
|
7
|
|
|
|
|
8
|
|
|
class Debugger |
|
9
|
|
|
{ |
|
10
|
|
|
private $fp; |
|
11
|
|
|
|
|
12
|
|
|
public function __construct(string $logFile) |
|
13
|
|
|
{ |
|
14
|
|
|
$this->fp = fopen($logFile, 'w+'); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function __destruct() |
|
18
|
|
|
{ |
|
19
|
|
|
fwrite($this->fp, PHP_EOL . PHP_EOL); |
|
20
|
|
|
fclose($this->fp); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function writeLogContent(string $content): void |
|
24
|
|
|
{ |
|
25
|
|
|
fwrite($this->fp, $content); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function writeLogLine(string $line, int $color = 0): void |
|
29
|
|
|
{ |
|
30
|
|
|
$this->writeLogContent(($color === 0 ? $line : "\033[01;" . (string) $color . 'm' . $line . "\033[01;0m") . PHP_EOL); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function debugSequence(\Iterator $sequence): void |
|
34
|
|
|
{ |
|
35
|
|
|
$colors = [ |
|
36
|
|
|
"\033[01;0m", |
|
37
|
|
|
"\033[01;31m", |
|
38
|
|
|
"\033[01;32m", |
|
39
|
|
|
"\033[01;34m", |
|
40
|
|
|
"\033[01;35m", |
|
41
|
|
|
"\033[01;36m", |
|
42
|
|
|
]; |
|
43
|
|
|
|
|
44
|
|
|
$reflect = new \ReflectionClass(Context::class); |
|
45
|
|
|
$constants = array_flip($reflect->getConstants()); |
|
46
|
|
|
$legend = ''; |
|
47
|
|
|
foreach ($constants as $index => $constant) { |
|
48
|
|
|
$legend .= $colors[$index] ?? "\033[01;0m"; |
|
49
|
|
|
$legend .= substr($constant, 8); |
|
50
|
|
|
$legend .= "\033[0m"; |
|
51
|
|
|
$legend .= ' '; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$captures = []; |
|
55
|
|
|
$symbols = []; |
|
56
|
|
|
$contexts = []; |
|
57
|
|
|
$nesting = []; |
|
58
|
|
|
|
|
59
|
|
|
$spacing = ''; |
|
60
|
|
|
|
|
61
|
|
|
try { |
|
62
|
|
|
foreach ($sequence as $symbol => $capture) { |
|
63
|
|
|
$captures[] = $capture->captured === null ? null : str_replace(PHP_EOL, '', $capture->captured); |
|
64
|
|
|
$symbols[] = $symbol; |
|
65
|
|
|
$contexts[] = $sequencer->position->context->context; |
|
|
|
|
|
|
66
|
|
|
$nesting[] = count($sequencer->position->stack); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
} catch (\RuntimeException $exception) { |
|
69
|
|
|
$this->writeLogLine($exception->getMessage(), 31); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
$this->writeLogLine(PHP_EOL); |
|
74
|
|
|
$this->writeLogLine($legend); |
|
75
|
|
|
$this->writeLogLine(str_repeat('—', strlen(str_replace(PHP_EOL, '', $sequencer->source->source)))); |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
$symbolLine = ''; |
|
78
|
|
|
foreach ($symbols as $index => $symbol) { |
|
79
|
|
|
$char = $symbol === Splitter::BYTE_BACKSLASH ? '\\' : chr($symbol); |
|
80
|
|
|
$capturedLength = strlen((string)$captures[$index]) + 1; |
|
81
|
|
|
$symbolLine .= $colors[$contexts[$index]] ?? "\033[01;0m"; |
|
82
|
|
|
$symbolLine .= str_repeat(' ', max($capturedLength - 1, 0)) . $char . $spacing; |
|
83
|
|
|
#echo str_repeat((string)$contexts[$index], $capturedLength > 0 ? $capturedLength : 1); |
|
84
|
|
|
$symbolLine .= "\033[0m"; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$this->writeLogLine($symbolLine, 0); |
|
88
|
|
|
|
|
89
|
|
|
$captureLine = ''; |
|
90
|
|
|
foreach ($captures as $index => $capture) { |
|
91
|
|
|
$char = $symbol === Splitter::BYTE_BACKSLASH ? '\\' : chr($symbol); |
|
|
|
|
|
|
92
|
|
|
$captureLine .= $colors[$contexts[$index]] ?? "\033[01;0m"; |
|
93
|
|
|
$captureLine .= $capture; |
|
94
|
|
|
//$captureLine .= str_pad(addslashes(chr($symbols[$index])), 1, ' ') . ' '; |
|
95
|
|
|
$captureLine .= str_repeat(' ', strlen($char)) . $spacing; |
|
96
|
|
|
$captureLine .= "\033[0m"; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$this->writeLogLine($captureLine); |
|
100
|
|
|
|
|
101
|
|
|
$nestingLine = ''; |
|
102
|
|
|
foreach ($nesting as $index => $depth) { |
|
103
|
|
|
$capturedLength = strlen(str_replace(PHP_EOL, '', (string)$captures[$index])) + 1; |
|
104
|
|
|
$nestingLine .= str_repeat((string)$depth, ($capturedLength > 0 ? $capturedLength : 0)) . $spacing; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$this->writeLogLine($nestingLine); |
|
108
|
|
|
$this->writeLogLine(str_repeat('—', strlen(str_replace(PHP_EOL, '', $sequencer->source->source)))); |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
} |
This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.
The variable may have been renamed without also renaming all references.