8000 Make getPrettyJson return string|null · symfony/symfony@0430c77 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0430c77

Browse files
committed
Make getPrettyJson return string|null
1 parent 68ae2e0 commit 0430c77

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,13 @@
179179
</div>
180180
{% elseif collector.content %}
181181
<div class="sf-tabs">
182-
{% if collector.isJsonRequest and collector.prettyJson.valid %}
182+
{% set prettyJson = collector.isJsonRequest ? collector.prettyJson : null %}
183+
{% if prettyJson is not null %}
183184
<div class="tab">
184185
<h3 class="tab-title">Pretty</h3>
185186
<div class="tab-content">
186187
<div class="card" style="max-height: 500px; overflow-y: auto;">
187-
<pre class="break-long-words">{{ collector.prettyJson.content }}</pre>
188+
<pre class="break-long-words">{{ collector.prettyJson }}</pre>
188189
</div>
189190
</div>
190191
</div>

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -263,28 +263,23 @@ 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+
266271
public function getPrettyJson()
267272
{
268273
$decoded = json_decode($this->getContent());
269-
$valid = JSON_ERROR_NONE === json_last_error();
270-
$content = json_encode($decoded, JSON_PRETTY_PRINT);
271274

272-
return array(
273-
'valid' => $valid,
274-
'content' => $content,
275-
);
275+
return JSON_ERROR_NONE === json_last_error() ? json_encode($decoded, JSON_PRETTY_PRINT) : null;
276276
}
277277

278278
public function getContentType()
279279
{
280280
return $this->data['content_type'];
281281
}
282282

283-
public function isJsonRequest()
284-
{
285-
return 1 === preg_match('{^application/(?:\w+\++)*json$}i', $this->data['request_headers']['content-type']);
286-
}
287-
288283
public function getStatusText()
289284
{
290285
return $this->data['status_text'];

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,12 +379,12 @@ public function testGetPrettyJsonValidity($content, $expected)
379379
public function providePrettyJson()
380380
{
381381
return array(
382-
array('null', array('valid' => true, 'content' => 'null')),
383-
array('{ "foo": "bar" }', array('valid' => true, 'content' => '{
382+
array('null', 'null'),
383+
array('{ "foo": "bar" }', '{
384384
"foo": "bar"
385-
}')),
386-
array('{ "abc" }', array('valid' => false, 'content' => 'null')),
387-
array('', array('valid' => false, 'content' => 'null')),
385+
}'),
386+
array('{ "abc" }', null),
387+
array('', null),
388388
);
389389
}
390390
}

0 commit comments

Comments
 (0)
0