-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTraceWriter.php
More file actions
168 lines (141 loc) · 4.09 KB
/
Copy pathTraceWriter.php
File metadata and controls
168 lines (141 loc) · 4.09 KB
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
<?php
/**
* TraceWriter class file
*
* @package Mantle
*/
declare(strict_types=1);
namespace Mantle\Testing;
use NunoMaduro\Collision\Highlighter;
use Spatie\Backtrace\Frame;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;
use function Mantle\Support\Helpers\collect;
use function Termwind\render;
use function Termwind\renderUsing;
/**
* Output traces in console and unit testing environments.
*
* @internal
*/
class TraceWriter {
/**
* Number of lines to show in code snippets.
*/
private readonly int $lines;
/**
* Constructor.
*
* @param string $title
* @param string $description
* @param Frame[] $frames
* @param string|null $prefix
* @param OutputInterface $output
*/
public function __construct(
public readonly string $title,
public readonly string $description,
public readonly array $frames,
public readonly ?string $prefix = null,
private readonly OutputInterface $output = new ConsoleOutput( verbosity: OutputInterface::VERBOSITY_NORMAL, decorated: true ),
) {
$this->lines = Utils::env( 'MANTLE_TESTING_TRACE_SNIPPET_LINES', 10 );
}
/**
* Write the trace to the output.
*/
public function write(): void {
renderUsing( $this->output );
$this->renderTitleAndDescription();
$this->renderSnippet();
$this->renderTrace();
renderUsing( null );
}
/**
* Render the title of the trace.
*/
private function renderTitleAndDescription(): void {
if ( $this->prefix ) {
$prefix = strtoupper( $this->prefix );
$prefix = "<span class=\"bg-red-400 text-black font-bold px-1\">{$prefix}</span> ";
} else {
$prefix = '';
}
$description = strip_tags( $this->description );
render(
<<<HTML
<div class="mx-1 mt-1">
{$prefix}<span class="font-bold">{$this->title}</span>
<div class="my-1">{$description}</div>
</div>
HTML
);
}
/**
* Render the code snippet of the first frame.
*/
private function renderSnippet(): void {
$frame = $this->getFirstFrame();
assert( $frame instanceof Frame );
if ( class_exists( Highlighter::class ) && method_exists( Highlighter::class, 'getCodeSnippet' ) ) {
$highlighter = new Highlighter();
$this->output->writeln( $highlighter->getCodeSnippet(
source: (string) file_get_contents( $frame->file ),
lineNumber: $frame->lineNumber,
linesBefore: (int) floor( $this->lines / 2 ),
linesAfter: (int) ceil( $this->lines / 2 ) - 1,
) );
} else {
$starting_line = $this->getStartingLine( $frame, $this->lines );
$snippet = collect( $frame->getSnippet( $this->lines ) )
->values()
->implode( PHP_EOL );
render( "<code start-line=\"{$starting_line}\" line=\"{$frame->lineNumber}\">{$snippet}</code>" );
}
}
/**
* Render the trace frames.
*/
private function renderTrace(): void {
foreach ( $this->frames as $i => $frame ) {
// Skip vendor frames unless in verbose mode.
if ( $this->output->getVerbosity() < OutputInterface::VERBOSITY_VERBOSE && strpos( $frame->file, '/vendor/' ) !== false ) {
continue;
}
$pos = str_pad( (string) ( (int) $i + 1 ), 4, ' ' );
$file = $this->getFileRelativePath( $frame->file );
$this->output->writeln(
"<fg=yellow>{$pos}</><fg=default;options=bold>{$file}</>:<fg=default;options=bold>{$frame->lineNumber}</>",
);
}
$this->output->writeln( '' );
}
/**
* Get the starting line for a code snippet.
*
* @param Frame $frame
* @param int $lines
*/
private function getStartingLine( Frame $frame, int $lines ): int {
$starting_line = $frame->lineNumber - (int) floor( $lines / 2 );
return max( 1, $starting_line );
}
/**
* Get the first frame from the trace.
*/
private function getFirstFrame(): ?Frame {
return collect( $this->frames )->first();
}
/**
* Returns the relative path of the given file path.
*
* @param string $filePath Absolute file path.
*/
private function getFileRelativePath( string $filePath ): string {
$cwd = (string) getcwd();
if ( ! empty( $cwd ) ) {
return str_replace( $cwd . DIRECTORY_SEPARATOR, '', $filePath );
}
return $filePath;
}
}