Skip to content

Commit 3fc477e

Browse files
committed
wip #21
1 parent 2cfa55b commit 3fc477e

File tree

13 files changed

+200
-99
lines changed

13 files changed

+200
-99
lines changed

demo/demo.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@
3333
'/var/www/html',
3434
$writer->__toString()
3535
);
36-
if ($filename === 'console.log') {
36+
if (PHP_SAPI === 'cli') {
37+
if ($filename === 'console.log') {
38+
echo $dumping;
39+
}
40+
} elseif ($filename === 'html.html') {
3741
echo $dumping;
3842
}
3943
file_put_contents(__DIR__ . '/output/' . $filename, $dumping);

demo/output/console.log

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Arg#1 Chevere\VarDump\VarDump#2
1212
private outputHr string ------------------------------------------------------------ (length=69)
1313
private writer Chevere\Writer\StreamWriter#13
1414
private stream Nyholm\Psr7\Stream#14
15-
private stream Resource id #43 (type=stream)
15+
private stream Resource id #47 (type=stream)
1616
private seekable bool true
1717
private readable bool true
1818
private writable bool true

demo/output/html.html

Lines changed: 30 additions & 31 deletions
Large diffs are not rendered by default.

demo/output/plain.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ private output Chevere\VarDump\Outputs\PlainOutput#9
1212
private outputHr string ------------------------------------------------------------ (length=60)
1313
private writer Chevere\Writer\StreamWriter#16
1414
private stream Nyholm\Psr7\Stream#17
15-
private stream Resource id #76 (type=stream)
15+
private stream Resource id #80 (type=stream)
1616
private seekable bool true
1717
private readable bool true
1818
private writable bool true

src/Formats/HtmlFormat.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@
1919

2020
final class HtmlFormat implements FormatInterface
2121
{
22-
public const HTML_INLINE_PREFIX = '<span style="border-left: 1px solid rgba(108 108 108 / 35%);"></span>';
22+
public const HTML_INLINE_PREFIX = '<span class="chv-dump-inline"></span>';
2323

2424
public const HTML_EMPHASIS = '<em>%s</em>';
2525

26-
public const HTML_DETAILS_OPEN = '<details style="line-height: normal; display: block; margin-top: -1.2em;"%s>';
26+
public const HTML_DETAILS_OPEN = '<details class="chv-dump-details"%s>';
2727

2828
public const HTML_DETAILS_CLOSE = '</details>';
2929

30-
public const HTML_SUMMARY = '<summary style="height: 1.2em; margin-left: -0.8em; position: relative;"></summary>';
30+
public const HTML_SUMMARY = '<summary class="chv-dump-summary"></summary>';
3131

32-
public const DETAILS_PULL_UP = '<div style="margin-top: -1.2em; height: 0;"></div>';
32+
public const DETAILS_PULL_UP = '<div class="chv-details-pull-up"></div>';
3333

3434
public function indent(int $indent): string
3535
{

src/Highlights/ConsoleHighlight.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public static function palette(): array
8484
VarDumperInterface::FUNCTION => ['39'],
8585
VarDumperInterface::VARIABLE => ['39'],
8686
// Orchid
87-
VarDumperInterface::MODIFIERS => ['170'],
87+
VarDumperInterface::MODIFIER => ['170'],
8888
// dark gray italic
8989
VarDumperInterface::EMPHASIS => ['242', '3'],
9090
];

src/Highlights/HtmlHighlight.php

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,21 @@ final class HtmlHighlight implements HighlightInterface
2222
{
2323
use AssertKeyTrait;
2424

25-
private string $color;
25+
private string $class;
2626

2727
public function __construct(
2828
private string $key
2929
) {
3030
$this->assertKey($key);
31-
$this->color = $this->palette()[$this->key] ?? 'inherit';
31+
$this->class = $this->palette()[$this->key]
32+
?? '';
3233
}
3334

3435
public function highlight(string $dump): string
3536
{
36-
return '<span style="color:' . $this->color . '">' . $dump . '</span>';
37+
return <<<HTML
38+
<span class="chv-dump-{$this->class}">{$dump}</span>
39+
HTML;
3740
}
3841

3942
/**
@@ -42,21 +45,21 @@ public function highlight(string $dump): string
4245
public static function palette(): array
4346
{
4447
return [
45-
TypeInterface::STRING => '#ff8700',
46-
TypeInterface::FLOAT => '#ff8700',
47-
TypeInterface::INT => '#ff8700',
48-
TypeInterface::BOOL => '#ff8700',
49-
TypeInterface::NULL => '#ff8700',
50-
TypeInterface::OBJECT => '#fabb00',
51-
TypeInterface::ARRAY => '#27ae60',
52-
TypeInterface::RESOURCE => '#ff5f5f',
53-
VarDumperInterface::FILE => '#87afff',
54-
VarDumperInterface::CLASS_REG => '#fabb00',
55-
VarDumperInterface::OPERATOR => '#6c6c6c',
56-
VarDumperInterface::FUNCTION => '#00afff',
57-
VarDumperInterface::VARIABLE => '#00afff',
58-
VarDumperInterface::MODIFIERS => '#d75fd7',
59-
VarDumperInterface::EMPHASIS => 'rgb(108 108 108 / 65%);',
48+
TypeInterface::STRING => 'string',
49+
TypeInterface::FLOAT => 'float',
50+
TypeInterface::INT => 'int',
51+
TypeInterface::BOOL => 'bool',
52+
TypeInterface::NULL => 'null',
53+
TypeInterface::OBJECT => 'object',
54+
TypeInterface::ARRAY => 'array',
55+
TypeInterface::RESOURCE => 'resource',
56+
VarDumperInterface::FILE => 'file',
57+
VarDumperInterface::CLASS_REG => 'class-reg',
58+
VarDumperInterface::OPERATOR => 'operator',
59+
VarDumperInterface::FUNCTION => 'function',
60+
VarDumperInterface::VARIABLE => 'variable',
61+
VarDumperInterface::MODIFIER => 'modifier',
62+
VarDumperInterface::EMPHASIS => 'emphasis',
6063
];
6164
}
6265
}

src/Interfaces/HighlightInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ interface HighlightInterface
3333
VarDumperInterface::CLASS_REG,
3434
VarDumperInterface::OPERATOR,
3535
VarDumperInterface::FUNCTION,
36-
VarDumperInterface::MODIFIERS,
36+
VarDumperInterface::MODIFIER,
3737
VarDumperInterface::VARIABLE,
3838
VarDumperInterface::EMPHASIS,
3939
];

src/Interfaces/VarDumperInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ interface VarDumperInterface
3939

4040
public const FUNCTION = '_function';
4141

42-
public const MODIFIERS = '_modifiers';
42+
public const MODIFIER = '_modifier';
4343

4444
public const VARIABLE = '_variable';
4545

src/Outputs/HtmlOutput.php

Lines changed: 113 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,100 @@
1313

1414
namespace Chevere\VarDump\Outputs;
1515

16+
use Chevere\VarDump\Interfaces\FormatInterface;
17+
1618
final class HtmlOutput extends Output
1719
{
18-
public const BACKGROUND = '#132537';
19-
20-
public const BACKGROUND_SHADE = '#132537';
21-
22-
/**
23-
* @var string Dump style, no double quotes.
24-
*/
25-
public const STYLE = 'font-size:14px;'
26-
. "font-family:'Fira Code Retina','Operator Mono',Inconsolata,Menlo,Monaco,Consolas,monospace;"
27-
. 'line-height:normal;'
28-
. 'color:#ecf0f1;'
29-
. 'padding:1.25em;'
30-
. 'margin:10px 0;'
31-
. 'word-break:break-word;'
32-
. 'white-space:pre-wrap;'
33-
. 'background:' . self::BACKGROUND . ';'
34-
. 'display:block;'
35-
. 'text-align:left;'
36-
. 'border:none;'
37-
. 'border-radius:4px;';
20+
public const CSS = <<<CSS
21+
html.chv-dump {
22+
background: #132537;
23+
}
24+
pre.chv-dump {
25+
font-size: 14px;
26+
font-family: 'Fira Code Retina', 'Operator Mono', Inconsolata, Menlo, Monaco, Consolas, monospace;
27+
line-height: normal;
28+
color: #ecf0f1;
29+
padding: 1.25em;
30+
margin: 0.8em 0;
31+
word-break: break-word;
32+
white-space: pre-wrap;
33+
background: #132537;
34+
display: block;
35+
text-align: left;
36+
border: none;
37+
border-radius: 0.2857em;
38+
}
39+
.chv-dump-hr {
40+
opacity: 0.25;
41+
}
42+
.chv-dump-inline {
43+
border-left: 1px solid rgba(108 108 108 / 35%);
44+
}
45+
.chv-dump-details {
46+
line-height: normal;
47+
display: block;
48+
margin-top: -1.2em;
49+
}
50+
.chv-dump-summary {
51+
height: 1.2em;
52+
margin-left: -0.8em;
53+
position: relative;
54+
}
55+
.chv-details-pull-up {
56+
margin-top: -1.2em;
57+
height: 0;
58+
}
59+
.chv-dump-float {
60+
color: #ff8700;
61+
}
62+
.chv-dump-int {
63+
color: #ff8700;
64+
}
65+
.chv-dump-string {
66+
color: #ff8700;
67+
}
68+
.chv-dump-bool {
69+
color: #ff8700;
70+
}
71+
.chv-dump-null {
72+
color: #ff8700;
73+
}
74+
.chv-dump-object {
75+
color: #fabb00;
76+
}
77+
.chv-dump-array {
78+
color: #27ae60;
79+
}
80+
.chv-dump-resource {
81+
color: #ff5f5f;
82+
}
83+
.chv-dump-file {
84+
color: #87afff;
85+
}
86+
.chv-dump-class-reg {
87+
color: #fabb00;
88+
}
89+
.chv-dump-operator {
90+
color: #6c6c6c;
91+
}
92+
.chv-dump-function {
93+
color: #00afff;
94+
}
95+
.chv-dump-variable {
96+
color: #00afff;
97+
}
98+
.chv-dump-modifier {
99+
color: #d75fd7;
100+
}
101+
.chv-dump-emphasis {
102+
color: rgb(108 108 108 / 65%);
103+
}
104+
CSS;
38105

39106
private bool $hasHeader = false;
40107

108+
private static $isStyleWritten = false;
109+
41110
public function tearDown(): void
42111
{
43112
$this->writer()->write('</pre>');
@@ -52,17 +121,33 @@ public function prepare(): void
52121
if (! headers_sent() || headers_list() === []) {
53122
$this->hasHeader = true;
54123
$this->writer()->write(
55-
'<html style="background: '
56-
. self::BACKGROUND_SHADE
57-
. ';"><head><meta charset="UTF-8"></head><body>'
124+
'<html class="chv-dump"><head><meta charset="UTF-8"></head><body>'
125+
);
126+
}
127+
if (! self::$isStyleWritten) {
128+
$this->writer()->write(
129+
'<style>' . preg_replace('/\s+/', ' ', self::CSS) . '</style>'
58130
);
131+
self::$isStyleWritten = true;
59132
}
60133
$this->writer()->write(
61-
implode('', [
62-
'<pre style="' . self::STYLE . '">',
63-
$this->caller(),
64-
'<hr style="opacity:.25">',
65-
])
134+
'<pre class="chv-dump">'
135+
. $this->caller()
136+
. '<hr class="chv-dump-hr">'
137+
);
138+
}
139+
140+
public function writeCallerFile(FormatInterface $format): void
141+
{
142+
$highlight = $this->getCallerFile($format);
143+
if ($highlight === '') {
144+
return;
145+
}
146+
$this->writer()->write(
147+
<<<HTML
148+
{$highlight}
149+
150+
HTML
66151
);
67152
}
68153
}

src/Outputs/Output.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,17 @@ final public function setUp(WriterInterface $writer, array $trace): void
4444

4545
public function writeCallerFile(FormatInterface $format): void
4646
{
47-
$item = $this->trace[0] ?? null;
48-
if (isset($item['file'])) {
49-
$fileLine = $item['file'] . ':' . $item['line'];
50-
$highlight = $format->highlight('_file', $fileLine);
51-
$this->writer->write(
52-
<<<PLAIN
47+
$highlight = $this->getCallerFile($format);
48+
if ($highlight === '') {
49+
return;
50+
}
51+
$this->writer()->write(
52+
<<<PLAIN
5353
54-
{$highlight}
54+
{$highlight}
5555
56-
PLAIN
57-
);
58-
}
56+
PLAIN
57+
);
5958
}
6059

6160
final public function trace(): array
@@ -68,6 +67,17 @@ final public function caller(): string
6867
return $this->caller;
6968
}
7069

70+
protected function getCallerFile(FormatInterface $format): string
71+
{
72+
$item = $this->trace[0] ?? null;
73+
if (! isset($item['file'])) {
74+
return '';
75+
}
76+
$fileLine = $item['file'] . ':' . $item['line'];
77+
78+
return $format->highlight('_file', $fileLine);
79+
}
80+
7181
final protected function writer(): WriterInterface
7282
{
7383
return $this->writer;

src/Processors/ObjectProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ private function processProperty(
201201
}
202202
$indentString = $this->varDumper->indentString();
203203
$modifier = $this->varDumper->format()->highlight(
204-
VarDumperInterface::MODIFIERS,
204+
VarDumperInterface::MODIFIER,
205205
$modifier
206206
);
207207
$variable = $this->varDumper->format()->highlight(

tests/Highlights/ConsoleHighlightTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function testConstruct(): void
5050
VarDumperInterface::CLASS_REG => '%c0%m' . $dump,
5151
VarDumperInterface::OPERATOR => '%c0%m' . $dump,
5252
VarDumperInterface::FUNCTION => '%c0%m' . $dump,
53-
VarDumperInterface::MODIFIERS => '%c0%m' . $dump,
53+
VarDumperInterface::MODIFIER => '%c0%m' . $dump,
5454
VarDumperInterface::VARIABLE => '%c0%m' . $dump,
5555
VarDumperInterface::EMPHASIS => '%c1%m' . $open . '%c0%m' . $dump . $close,
5656
];

0 commit comments

Comments
 (0)