Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9f85103

Browse files
SamFlemingfabpot
authored andcommitted
[DX][WebProfilerBundle] Add Pretty Print functionality for Request Content
1 parent 0acf9e1 commit 9f85103

File tree

5 files changed

+91
-6
lines changed

5 files changed

+91
-6
lines changed

src/Symfony/Bundle/TwigBundle/Resources/views/base_js.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
4242
/* create the tab navigation for each group of tabs */
4343
for (var i = 0; i < tabGroups.length; i++) {
44-
var tabs = tabGroups[i].querySelectorAll('.tab');
44+
var tabs = tabGroups[i].querySelectorAll(':scope > .tab');
4545
var tabNavigation = document.createElement('ul');
4646
tabNavigation.className = 'tab-navigation';
4747
@@ -67,7 +67,7 @@
6767
6868
/* display the active tab and add the 'click' event listeners */
6969
for (i = 0; i < tabGroups.length; i++) {
70-
tabNavigation = tabGroups[i].querySelectorAll('.tab-navigation li');
70+
tabNavigation = tabGroups[i].querySelectorAll(':scope >.tab-navigation li');
7171
7272
for (j = 0; j < tabNavigation.length; j++) {
7373
tabId = tabNavigation[j].getAttribute('data-tab-id');

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/request.html.twig

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,27 @@
178178
<p>Request content not available (it was retrieved as a resource).</p>
179179
</div>
180180
{% elseif collector.content %}
181-
<div class="card">
182-
<pre class="break-long-words">{{ collector.content }}</pre>
181+
<div class="sf-tabs">
182+
{% set prettyJson = collector.isJsonRequest ? collector.prettyJson : null %}
183+
{% if prettyJson is not null %}
184+
<div class="tab">
185+
<h3 class="tab-title">Pretty</h3>
186+
<div class="tab-content">
187+
<div class="card" style="max-height: 500px; overflow-y: auto;">
188+
<pre class="break-long-words">{{ prettyJson }}</pre>
189+
</div>
190+
</div>
191+
</div>
192+
{% endif %}
193+
194+
<div class="tab">
195+
<h3 class="tab-title">Raw</h3>
196+
<div class="tab-content">
197+
<div class="card">
198+
<pre class="break-long-words">{{ collector.content }}</pre>
199+
</div>
200+
</div>
201+
</div>
183202
</div>
184203
{% else %}
185204
<div class="empty">

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/base_js.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@
552552
553553
/* create the tab navigation for each group of tabs */
554554
for (var i = 0; i < tabGroups.length; i++) {
555-
var tabs = tabGroups[i].querySelectorAll('.tab');
555+
var tabs = tabGroups[i].querySelectorAll(':scope > .tab');
556556
var tabNavigation = document.createElement('ul');
557557
tabNavigation.className = 'tab-navigation';
558558
@@ -578,7 +578,7 @@
578578
579579
/* display the active tab and add the 'click' event listeners */
580580
for (i = 0; i < tabGroups.length; i++) {
581-
tabNavigation = tabGroups[i].querySelectorAll('.tab-navigation li');
581+
tabNavigation = tabGroups[i].querySelectorAll(':scope > .tab-navigation li');
582582
583583
for (j = 0; j < tabNavigation.length; j++) {
584584
tabId = tabNavigation[j].getAttribute('data-tab-id');

src/Symfony/Component/HttpKernel/DataCollector/RequestDataCollector.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,18 @@ public function getContent()
263263
return $this->data['content'];
264264
}
265265

266+
public function isJsonRequest()
267+
{
268+
return 1 === preg_match('{^application/(?:\w+\++)*json$}i', $this->data['request_headers']['content-type']);
269+
}
270+
271+
public function getPrettyJson()
272+
{
273+
$decoded = json_decode($this->getContent());
274+
275+
return JSON_ERROR_NONE === json_last_error() ? json_encode($decoded, JSON_PRETTY_PRINT) : null;
276+
}
277+
266278
public function getContentType()
267279
{
268280
return $this->data['content_type'];

src/Symfony/Component/HttpKernel/Tests/DataCollector/RequestDataCollectorTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,4 +333,58 @@ private function getCookieByName(Response $response, $name)
333333

334334
throw new \InvalidArgumentException(sprintf('Cookie named "%s" is not in response', $name));
335335
}
336+
337+
/**
338+
* @dataProvider provideJsonContentTypes
339+
*/
340+
public function testIsJson($contentType, $expected)
341+
{
342+
$response = $this->createResponse();
343+
$request = $this->createRequest();
344+
$request->headers->set('Content-Type', $contentType);
345+
346+
$c = new RequestDataCollector();
347+
$c->collect($request, $response);
348+
349+
$this->assertSame($expected, $c->isJsonRequest());
350+
}
351+
352+
public function provideJsonContentTypes()
353+
{
354+
return array(
355+
array('text/csv', false),
356+
array('application/json', true),
357+
array('application/JSON', true),
358+
array('application/hal+json', true),
359+
array('application/xml+json', true),
360+
array('application/xml', false),
361+
array('', false),
362+
);
363+
}
364+
365+
/**
366+
* @dataProvider providePrettyJson
367+
*/
368+
public function testGetPrettyJsonValidity($content, $expected)
369+
{
370+
$response = $this->createResponse();
371+
$request = Request::create('/', 'POST', array(), array(), array(), array(), $content);
372+
373+
$c = new RequestDataCollector();
374+
$c->collect($request, $response);
375+
376+
$this->assertSame($expected, $c->getPrettyJson());
377+
}
378+
379+
public function providePrettyJson()
380+
{
381+
return array(
382+
array('null', 'null'),
383+
array('{ "foo": "bar" }', '{
384+
"foo": "bar"
385+
}'),
386+
array('{ "abc" }', null),
387+
array('', null),
388+
);
389+
}
336390
}

0 commit comments

Comments
 (0)