[DX][WebProfilerBundle] Add Pretty Print functionality for Request Content

This commit is contained in:
Sam Fleming 2018-10-19 21:49:47 +01:00 committed by Fabien Potencier
parent 0acf9e17d4
commit 9f85103151
5 changed files with 91 additions and 6 deletions

View File

@ -41,7 +41,7 @@
/* create the tab navigation for each group of tabs */
for (var i = 0; i < tabGroups.length; i++) {
var tabs = tabGroups[i].querySelectorAll('.tab');
var tabs = tabGroups[i].querySelectorAll(':scope > .tab');
var tabNavigation = document.createElement('ul');
tabNavigation.className = 'tab-navigation';
@ -67,7 +67,7 @@
/* display the active tab and add the 'click' event listeners */
for (i = 0; i < tabGroups.length; i++) {
tabNavigation = tabGroups[i].querySelectorAll('.tab-navigation li');
tabNavigation = tabGroups[i].querySelectorAll(':scope >.tab-navigation li');
for (j = 0; j < tabNavigation.length; j++) {
tabId = tabNavigation[j].getAttribute('data-tab-id');

View File

@ -178,8 +178,27 @@
<p>Request content not available (it was retrieved as a resource).</p>
</div>
{% elseif collector.content %}
<div class="card">
<pre class="break-long-words">{{ collector.content }}</pre>
<div class="sf-tabs">
{% set prettyJson = collector.isJsonRequest ? collector.prettyJson : null %}
{% if prettyJson is not null %}
<div class="tab">
<h3 class="tab-title">Pretty</h3>
<div class="tab-content">
<div class="card" style="max-height: 500px; overflow-y: auto;">
<pre class="break-long-words">{{ prettyJson }}</pre>
</div>
</div>
</div>
{% endif %}
<div class="tab">
<h3 class="tab-title">Raw</h3>
<div class="tab-content">
<div class="card">
<pre class="break-long-words">{{ collector.content }}</pre>
</div>
</div>
</div>
</div>
{% else %}
<div class="empty">

View File

@ -552,7 +552,7 @@
/* create the tab navigation for each group of tabs */
for (var i = 0; i < tabGroups.length; i++) {
var tabs = tabGroups[i].querySelectorAll('.tab');
var tabs = tabGroups[i].querySelectorAll(':scope > .tab');
var tabNavigation = document.createElement('ul');
tabNavigation.className = 'tab-navigation';
@ -578,7 +578,7 @@
/* display the active tab and add the 'click' event listeners */
for (i = 0; i < tabGroups.length; i++) {
tabNavigation = tabGroups[i].querySelectorAll('.tab-navigation li');
tabNavigation = tabGroups[i].querySelectorAll(':scope > .tab-navigation li');
for (j = 0; j < tabNavigation.length; j++) {
tabId = tabNavigation[j].getAttribute('data-tab-id');

View File

@ -263,6 +263,18 @@ class RequestDataCollector extends DataCollector implements EventSubscriberInter
return $this->data['content'];
}
public function isJsonRequest()
{
return 1 === preg_match('{^application/(?:\w+\++)*json$}i', $this->data['request_headers']['content-type']);
}
public function getPrettyJson()
{
$decoded = json_decode($this->getContent());
return JSON_ERROR_NONE === json_last_error() ? json_encode($decoded, JSON_PRETTY_PRINT) : null;
}
public function getContentType()
{
return $this->data['content_type'];

View File

@ -333,4 +333,58 @@ class RequestDataCollectorTest extends TestCase
throw new \InvalidArgumentException(sprintf('Cookie named "%s" is not in response', $name));
}
/**
* @dataProvider provideJsonContentTypes
*/
public function testIsJson($contentType, $expected)
{
$response = $this->createResponse();
$request = $this->createRequest();
$request->headers->set('Content-Type', $contentType);
$c = new RequestDataCollector();
$c->collect($request, $response);
$this->assertSame($expected, $c->isJsonRequest());
}
public function provideJsonContentTypes()
{
return array(
array('text/csv', false),
array('application/json', true),
array('application/JSON', true),
array('application/hal+json', true),
array('application/xml+json', true),
array('application/xml', false),
array('', false),
);
}
/**
* @dataProvider providePrettyJson
*/
public function testGetPrettyJsonValidity($content, $expected)
{
$response = $this->createResponse();
$request = Request::create('/', 'POST', array(), array(), array(), array(), $content);
$c = new RequestDataCollector();
$c->collect($request, $response);
$this->assertSame($expected, $c->getPrettyJson());
}
public function providePrettyJson()
{
return array(
array('null', 'null'),
array('{ "foo": "bar" }', '{
"foo": "bar"
}'),
array('{ "abc" }', null),
array('', null),
);
}
}