Skip to content

Commit 836f793

Browse files
authored
[Profiler] Utilize symfony/var-dumper for dumping response body (#397)
* [Profiler] Utilize symfony/var-dumper for dumping response body * Fix tests with newer symfony version
1 parent a135ee6 commit 836f793

File tree

5 files changed

+49
-10
lines changed

5 files changed

+49
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.
44

5+
# 1.23.0 - ?
6+
7+
- Changed the way request/response body is displayed in profiler. symfony/var-dumper is used now.
8+
59
# 1.22.1 - 2021-07-26
610

711
- Do not deprecate the service alias for the old Http\Client\HttpClient interface because different Symfony

src/Collector/Twig/HttpMessageMarkupExtension.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
namespace Http\HttplugBundle\Collector\Twig;
66

7+
use Symfony\Component\VarDumper\Cloner\ClonerInterface;
8+
use Symfony\Component\VarDumper\Cloner\VarCloner;
9+
use Symfony\Component\VarDumper\Dumper\DataDumperInterface;
10+
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
711
use Twig\Extension\AbstractExtension;
812
use Twig\TwigFilter;
913

@@ -12,6 +16,22 @@
1216
*/
1317
class HttpMessageMarkupExtension extends AbstractExtension
1418
{
19+
/**
20+
* @var ClonerInterface
21+
*/
22+
private $cloner;
23+
24+
/**
25+
* @var HtmlDumper
26+
*/
27+
private $dumper;
28+
29+
public function __construct(?ClonerInterface $cloner = null, ?DataDumperInterface $dumper)
30+
{
31+
$this->cloner = $cloner ?: new VarCloner();
32+
$this->dumper = $dumper ?: new HtmlDumper();
33+
}
34+
1535
/**
1636
* {@inheritdoc}
1737
*
@@ -21,6 +41,7 @@ public function getFilters()
2141
{
2242
return [
2343
new TwigFilter('httplug_markup', [$this, 'markup'], ['is_safe' => ['html']]),
44+
new TwigFilter('httplug_markup_body', [$this, 'markupBody'], ['is_safe' => ['html']]),
2445
];
2546
}
2647

@@ -49,6 +70,18 @@ public function markup($message)
4970
return sprintf("%s\n\n<div class='httplug-http-body httplug-hidden'>%s</div>", $headers, $parts[1]);
5071
}
5172

73+
public function markupBody(string $body): ?string
74+
{
75+
if (in_array(substr($body, 0, 1), ['{','['], true)) {
76+
$json = json_decode($body, true);
77+
if (json_last_error() === JSON_ERROR_NONE) {
78+
$body = $json;
79+
}
80+
}
81+
82+
return $this->dumper->dump($this->cloner->cloneVar($body));
83+
}
84+
5285
public function getName()
5386
{
5487
return 'httplug.message_markup';

src/Resources/config/data-collector.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
</service>
2525

2626
<service id="httplug.collector.twig.http_message" class="Http\HttplugBundle\Collector\Twig\HttpMessageMarkupExtension" public="false">
27+
<argument type="service" id="var_dumper.cloner" on-invalid="null" />
28+
<argument type="service" id="var_dumper.html_dumper" on-invalid="null" />
2729
<tag name="twig.extension" />
2830
</service>
2931

src/Resources/views/http_message.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
{% if row is empty %}
1515
{% set hasReachedBody = true %}
1616
{% elseif hasReachedBody %}
17-
{% set content = content ~ "\n" ~ row %}
17+
{% set content = content ~ row %}
1818
{% else %}
1919
{% set row = row|split(':') %}
2020
{% set value = row|slice(1)|join(':')|trim %}
@@ -33,4 +33,4 @@
3333
</tbody>
3434
</table>
3535

36-
<div class='httplug-http-body httplug-hidden'>{{ content|nl2br ?: '(This message has no captured body)' }}</div>
36+
<div class='httplug-http-body httplug-hidden'>{{ content ? content|httplug_markup_body : '(This message has no captured body)' }}</div>

tests/Functional/DiscoveredClientsTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class DiscoveredClientsTest extends WebTestCase
1919
{
2020
public function testDiscoveredClient(): void
2121
{
22-
$container = $this->getContainer(false);
22+
$container = $this->getCustomContainer(false);
2323

2424
$this->assertTrue($container->has('httplug.auto_discovery.auto_discovered_client'));
2525

@@ -30,7 +30,7 @@ public function testDiscoveredClient(): void
3030

3131
public function testDiscoveredAsyncClient(): void
3232
{
33-
$container = $this->getContainer(false);
33+
$container = $this->getCustomContainer(false);
3434

3535
$this->assertTrue($container->has('httplug.auto_discovery.auto_discovered_async'));
3636

@@ -41,7 +41,7 @@ public function testDiscoveredAsyncClient(): void
4141

4242
public function testDiscoveredClientWithProfilingEnabled(): void
4343
{
44-
$container = $this->getContainer(true);
44+
$container = $this->getCustomContainer(true);
4545

4646
$this->assertTrue($container->has('httplug.auto_discovery.auto_discovered_client'));
4747

@@ -53,7 +53,7 @@ public function testDiscoveredClientWithProfilingEnabled(): void
5353

5454
public function testDiscoveredAsyncClientWithProfilingEnabled(): void
5555
{
56-
$container = $this->getContainer(true);
56+
$container = $this->getCustomContainer(true);
5757

5858
$this->assertTrue($container->has('httplug.auto_discovery.auto_discovered_async'));
5959

@@ -68,7 +68,7 @@ public function testDiscoveredAsyncClientWithProfilingEnabled(): void
6868
*/
6969
public function testDiscovery(): void
7070
{
71-
$container = $this->getContainer(true);
71+
$container = $this->getCustomContainer(true);
7272

7373
$this->assertTrue($container->has('httplug.auto_discovery.auto_discovered_client'));
7474
$this->assertTrue($container->has('httplug.auto_discovery.auto_discovered_async'));
@@ -90,7 +90,7 @@ public function testDiscovery(): void
9090
*/
9191
public function testDisabledDiscovery(): void
9292
{
93-
$container = $this->getContainer(true, 'discovery_disabled');
93+
$container = $this->getCustomContainer(true, 'discovery_disabled');
9494

9595
$this->assertFalse($container->has('httplug.auto_discovery.auto_discovered_client'));
9696
$this->assertFalse($container->has('httplug.auto_discovery.auto_discovered_async'));
@@ -106,7 +106,7 @@ public function testForcedDiscovery(): void
106106
$this->markTestSkipped('Guzzle7 adapter is not installed');
107107
}
108108

109-
$container = $this->getContainer(true, 'discovery_forced');
109+
$container = $this->getCustomContainer(true, 'discovery_forced');
110110

111111
$this->assertFalse($container->has('httplug.auto_discovery.auto_discovered_client'));
112112
$this->assertFalse($container->has('httplug.auto_discovery.auto_discovered_async'));
@@ -118,7 +118,7 @@ public function testForcedDiscovery(): void
118118
$this->assertEquals($container->get('httplug.client.acme'), HttpAsyncClientDiscovery::find());
119119
}
120120

121-
private function getContainer($debug, $environment = 'test')
121+
private function getCustomContainer($debug, $environment = 'test')
122122
{
123123
static::bootKernel(['debug' => $debug, 'environment' => $environment]);
124124

0 commit comments

Comments
 (0)