-
-
Notifications
You must be signed in to change notification settings - Fork 375
/
Copy pathKernelDump.php
113 lines (91 loc) · 2.49 KB
/
KernelDump.php
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
<?php
declare(strict_types=1);
namespace Pest;
use Pest\Support\View;
use Symfony\Component\Console\Output\OutputInterface;
final class KernelDump
{
/**
* The output buffer, if any.
*/
private string $buffer = '';
/**
* Creates a new Kernel Dump instance.
*/
public function __construct(
private readonly OutputInterface $output,
) {
// ...
}
/**
* Enable the output buffering.
*/
public function enable(): void
{
ob_start(function (string $message): string {
$this->buffer .= $message;
return '';
});
}
/**
* Disable the output buffering.
*/
public function disable(): void
{
@ob_clean(); // @phpstan-ignore-line
if ($this->buffer !== '') {
$this->flush();
}
}
/**
* Shutdown the output buffering.
*/
public function shutdown(): void
{
$this->disable();
}
/**
* Flushes the buffer.
*/
private function flush(): void
{
View::renderUsing($this->output);
if ($this->isOpeningHeadline($this->buffer)) {
$this->buffer = implode(PHP_EOL, array_slice(explode(PHP_EOL, $this->buffer), 2));
}
$type = 'INFO';
if ($this->isInternalError($this->buffer)) {
$type = 'ERROR';
$this->buffer = str_replace(
sprintf('An error occurred inside PHPUnit.%s%sMessage: ', PHP_EOL, PHP_EOL), '', $this->buffer,
);
}
$this->buffer = trim($this->buffer);
$this->buffer = rtrim($this->buffer, '.').'.';
$lines = explode(PHP_EOL, $this->buffer);
$lines = array_reverse($lines);
$firstLine = array_pop($lines);
$lines = array_reverse($lines);
View::render('components.badge', [
'type' => $type,
'content' => $firstLine,
]);
$this->output->writeln($lines);
$this->buffer = '';
}
/**
* Checks if the given output contains an opening headline.
*/
private function isOpeningHeadline(string $output): bool
{
return str_contains($output, 'by Sebastian Bergmann and contributors.');
}
/**
* Checks if the given output contains an opening headline.
*/
private function isInternalError(string $output): bool
{
return str_contains($output, 'An error occurred inside PHPUnit.')
|| str_contains($output, 'Fatal error');
}
}