Skip to content

Commit 7fd4b53

Browse files
xepozzvjik
andauthored
Sort headers, fix overflowing, copy curl (#112)
Co-authored-by: Sergei Predvoditelev <[email protected]>
1 parent 99f7e93 commit 7fd4b53

File tree

6 files changed

+66
-31
lines changed

6 files changed

+66
-31
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 3.2.2 under development
44

5-
- no changes in this release.
5+
- Enh #112: Add copy cURL button, sort request headers, fix UI (@xepozz)
66

77
## 3.2.1 March 07, 2024
88

src/Renderer/HtmlRenderer.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
use function ob_implicit_flush;
4141
use function ob_start;
4242
use function realpath;
43-
use function rtrim;
4443
use function str_replace;
4544
use function stripos;
4645
use function strlen;
@@ -347,19 +346,22 @@ public function renderRequest(ServerRequestInterface $request): string
347346
{
348347
$output = $request->getMethod() . ' ' . $request->getUri() . "\n";
349348

350-
foreach ($request->getHeaders() as $name => $values) {
351-
if ($name === 'Host') {
352-
continue;
353-
}
349+
$headers = $request->getHeaders();
350+
unset($headers['Host']);
351+
ksort($headers);
354352

353+
foreach ($headers as $name => $values) {
355354
foreach ($values as $value) {
356355
$output .= "$name: $value\n";
357356
}
358357
}
359358

360-
$output .= "\n" . $request->getBody() . "\n\n";
359+
$body = (string)$request->getBody();
360+
if (!empty($body)) {
361+
$output .= "\n" . $body . "\n\n";
362+
}
361363

362-
return '<pre class="codeBlock language-text">' . $this->htmlEncode(rtrim($output, "\n")) . '</pre>';
364+
return $output;
363365
}
364366

365367
/**
@@ -374,10 +376,10 @@ public function renderCurl(ServerRequestInterface $request): string
374376
->setRequest($request)
375377
->build();
376378
} catch (Throwable $e) {
377-
return $this->htmlEncode('Error generating curl command: ' . $e->getMessage());
379+
return 'Error generating curl command: ' . $e->getMessage();
378380
}
379381

380-
return '<div class="codeBlock language-sh">' . $this->htmlEncode($output) . '</div>';
382+
return $output;
381383
}
382384

383385
/**

templates/_call-stack-item.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<li class="<?= (!empty($lines) && ($index === 1 || !$isVendorFile)) ? 'application ' : '' ?>call-stack-item"
2222
data-line="<?= (int) ($line - $begin) ?>">
2323
<div class="element-wrap">
24-
<div class="flex-1">
24+
<div class="flex-1 mw-100">
2525
<?php if ($file !== null): ?>
2626
<span class="file-name">
2727
<?= "{$index}. in {$this->htmlEncode($file)}" ?>

templates/development.css

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@ html,
33
body,
44
div,
55
span,
6-
h1,
7-
h2,
8-
h3,
9-
h4,
10-
h5,
11-
h6,
126
p,
137
pre,
148
a,
@@ -344,13 +338,13 @@ header .previous h3 {
344338
height: 150px;
345339
}
346340

347-
#copy-stacktrace {
341+
.copy-clipboard {
348342
position: absolute;
349343
right: 40px;
350344
top: 44px;
351345
}
352346

353-
#copy-stacktrace:hover svg path {
347+
.copy-clipboard:hover svg path {
354348
fill: var(--icon-hover-color);
355349
}
356350

@@ -387,6 +381,10 @@ main {
387381
flex: 1;
388382
}
389383

384+
.mw-100 {
385+
max-width: 100%;
386+
}
387+
390388
/* call stack */
391389
.call-stack ul li,
392390
.request {
@@ -460,6 +458,9 @@ main {
460458

461459
.call-stack ul li .element-wrap .function-info {
462460
display: inline-block;
461+
line-break: normal;
462+
overflow-wrap: break-word;
463+
word-break: break-word;
463464
}
464465

465466
.call-stack ul li.application .element-wrap {
@@ -557,12 +558,15 @@ main {
557558

558559
/* request */
559560
.request {
560-
padding: 20px 30px;
561+
position: relative;
561562
font-size: 14px;
562563
line-height: 18px;
563564
overflow-x: auto;
564565
font-family: JetBrains Mono, Consolas, monospace;
565566
}
567+
.request .body {
568+
overflow: auto;
569+
}
566570
/* end request */
567571

568572
/* footer */

templates/development.php

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@
8888
<textarea id="clipboard"><?= $this->htmlEncode($throwable) ?></textarea>
8989
<span id="copied">Copied!</span>
9090

91-
<a href="#" id="copy-stacktrace" title="Copy the stacktrace for use in a bug report or pastebin">
91+
<a href="#"
92+
class="copy-clipboard"
93+
data-clipboard="<?= $this->htmlEncode($throwable) ?>"
94+
title="Copy the stacktrace for use in a bug report or pastebin"
95+
>
9296
<svg width="26" height="30" fill="none" xmlns="http://www.w3.org/2000/svg">
9397
<path d="M17.9998.333344H3.33317C1.8665.333344.666504 1.53334.666504 3.00001V20.3333c0 .7334.599996 1.3334 1.333336 1.3334.73333 0 1.33333-.6 1.33333-1.3334V4.33334c0-.73333.6-1.33333 1.33333-1.33333h13.3333c.7334 0 1.3334-.6 1.3334-1.33333 0-.733337-.6-1.333336-1.3334-1.333336zm5.3334 5.333336H8.6665c-1.46666 0-2.66666 1.2-2.66666 2.66666V27c0 1.4667 1.2 2.6667 2.66666 2.6667h14.6667c1.4666 0 2.6666-1.2 2.6666-2.6667V8.33334c0-1.46666-1.2-2.66666-2.6666-2.66666zM21.9998 27H9.99984c-.73333 0-1.33334-.6-1.33334-1.3333V9.66668c0-.73334.60001-1.33334 1.33334-1.33334H21.9998c.7334 0 1.3334.6 1.3334 1.33334V25.6667c0 .7333-.6 1.3333-1.3334 1.3333z" fill="#787878"/>
9498
</svg>
@@ -107,12 +111,30 @@
107111
</div>
108112
<?php if ($request && ($requestInfo = $this->renderRequest($request)) !== ''): ?>
109113
<div class="request">
110-
<?= $requestInfo ?>
114+
<h2>Request info</h2>
115+
<div class="body">
116+
<pre class="codeBlock language-text"><?= $this->htmlEncode(rtrim($requestInfo, "\n")) ?></pre>
117+
</div>
111118
</div>
112119
<?php endif ?>
113120
<?php if ($request && ($curlInfo = $this->renderCurl($request)) !== 'curl'): ?>
114121
<div class="request">
115-
<?= $curlInfo ?>
122+
<textarea id="clipboard"><?= $curlInfo ?></textarea>
123+
<span id="copied" style="top: 10px">Copied!</span>
124+
<h2>cURL</h2>
125+
<a href="#"
126+
class="copy-clipboard"
127+
data-clipboard="<?= $curlInfo ?>"
128+
title="Copy the cURL"
129+
style="right: 10px; top: 5px"
130+
>
131+
<svg width="26" height="30" fill="none" xmlns="http://www.w3.org/2000/svg">
132+
<path d="M17.9998.333344H3.33317C1.8665.333344.666504 1.53334.666504 3.00001V20.3333c0 .7334.599996 1.3334 1.333336 1.3334.73333 0 1.33333-.6 1.33333-1.3334V4.33334c0-.73333.6-1.33333 1.33333-1.33333h13.3333c.7334 0 1.3334-.6 1.3334-1.33333 0-.733337-.6-1.333336-1.3334-1.333336zm5.3334 5.333336H8.6665c-1.46666 0-2.66666 1.2-2.66666 2.66666V27c0 1.4667 1.2 2.6667 2.66666 2.6667h14.6667c1.4666 0 2.6666-1.2 2.6666-2.6667V8.33334c0-1.46666-1.2-2.66666-2.6666-2.66666zM21.9998 27H9.99984c-.73333 0-1.33334-.6-1.33334-1.3333V9.66668c0-.73334.60001-1.33334 1.33334-1.33334H21.9998c.7334 0 1.3334.6 1.3334 1.33334V25.6667c0 .7333-.6 1.3333-1.3334 1.3333z" fill="#787878"/>
133+
</svg>
134+
</a>
135+
<div class="body">
136+
<div class="codeBlock language-sh"><?= $this->htmlEncode($curlInfo) ?></div>
137+
</div>
116138
</div>
117139
<?php endif ?>
118140
<div class="footer">
@@ -254,29 +276,36 @@
254276
}
255277

256278
// handle copy stacktrace action on clipboard button
257-
document.getElementById('copy-stacktrace').onclick = function(e) {
279+
const copyIntoClipboard = function(e) {
258280
e.preventDefault();
259-
var textarea = document.getElementById('clipboard');
281+
const parentContainer = e.currentTarget.parentElement;
282+
const textarea = parentContainer.querySelector('#clipboard');
260283
textarea.focus();
261284
textarea.select();
262285

263-
var succeeded;
286+
let succeeded;
264287
try {
265288
succeeded = document.execCommand('copy');
266289
} catch (err) {
267290
succeeded = false;
268291
}
269292
if (succeeded) {
270-
var hint = document.getElementById('copied');
293+
const hint = parentContainer.querySelector('#copied');
294+
if (!hint) {
295+
return
296+
}
271297
hint.style.display = 'block';
272-
setTimeout(function () {
273-
hint.style.display = 'none';
274-
}, 2000);
298+
setTimeout(() => hint.style.display = 'none', 2000);
275299
} else {
276300
// fallback: show textarea if browser does not support copying directly
277301
textarea.style.top = 0;
278302
}
279303
}
304+
const elements = document.querySelectorAll('.copy-clipboard')
305+
for (let element of elements) {
306+
element.onclick = copyIntoClipboard;
307+
}
308+
280309

281310
// handle theme change
282311
document.getElementById('dark-mode').onclick = function(e) {

tests/Renderer/HtmlRendererTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public function testRenderRequest(): void
149149
$output = $renderer->renderRequest($this->createServerRequestMock());
150150

151151
$this->assertSame(
152-
"<pre class=\"codeBlock language-text\">GET https:/example.com\nAccept: text/html</pre>",
152+
"GET https:/example.com\nAccept: text/html\n",
153153
$output
154154
);
155155
}

0 commit comments

Comments
 (0)