Skip to content

Commit f61f145

Browse files
authored
Merge pull request #212 from php-enqueue/bundle-profiler-array-to-string-conversion-fix
[bundle][profiler] Fix array to string conversion notice.
2 parents c6d5197 + 6e1f3e9 commit f61f145

File tree

3 files changed

+13
-17
lines changed

3 files changed

+13
-17
lines changed

Diff for: pkg/enqueue-bundle/Profiler/MessageQueueCollector.php

+4-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Enqueue\Client\MessagePriority;
66
use Enqueue\Client\ProducerInterface;
77
use Enqueue\Client\TraceableProducer;
8+
use Enqueue\Util\JSON;
89
use Symfony\Component\HttpFoundation\Request;
910
use Symfony\Component\HttpFoundation\Response;
1011
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
@@ -65,19 +66,13 @@ public function prettyPrintPriority($priority)
6566
}
6667

6768
/**
68-
* @param string $message
69+
* @param mixed $body
6970
*
7071
* @return string
7172
*/
72-
public function prettyPrintMessage($message)
73+
public function ensureString($body)
7374
{
74-
if (is_scalar($message)) {
75-
return htmlspecialchars($message);
76-
}
77-
78-
return htmlspecialchars(
79-
json_encode($message, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
80-
);
75+
return is_string($body) ? $body : JSON::encode($body);
8176
}
8277

8378
/**

Diff for: pkg/enqueue-bundle/Resources/views/Profiler/panel.html.twig

+4-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@
4444
<td>{{ sentMessage.topic }}</td>
4545
<td style="width: 70%">
4646
<span class="metadata">
47-
<span>{{ sentMessage.body[0:40] }}... </span><a class="btn btn-link text-small sf-toggle" data-toggle-selector="#message-body-{{ loop.index }}" data-toggle-alt-content="Hide trace">Show trace</a>
47+
<span>
48+
{{ collector.ensureString(sentMessage.body)[0:40] }}{% if collector.ensureString(sentMessage.body)[0:40] != collector.ensureString(sentMessage.body) %}...{% endif %}
49+
</span>
50+
<a class="btn btn-link text-small sf-toggle" data-toggle-selector="#message-body-{{ loop.index }}" data-toggle-alt-content="Hide trace">Show trace</a>
4851

4952
<div id="message-body-{{ loop.index }}" class="context sf-toggle-content sf-toggle-hidden">
5053
{{ profiler_dump(sentMessage.body) }}

Diff for: pkg/enqueue-bundle/Tests/Unit/Profiler/MessageQueueCollectorTest.php

+5-7
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,19 @@ public function testShouldPrettyPrintUnknownPriority()
7171
$this->assertEquals('unknownPriority', $collector->prettyPrintPriority('unknownPriority'));
7272
}
7373

74-
public function testShouldPrettyPrintScalarMessage()
74+
public function testShouldEnsureStringKeepStringSame()
7575
{
7676
$collector = new MessageQueueCollector($this->createProducerMock());
7777

78-
$this->assertEquals('foo', $collector->prettyPrintMessage('foo'));
79-
$this->assertEquals('&lt;p&gt;', $collector->prettyPrintMessage('<p>'));
78+
$this->assertEquals('foo', $collector->ensureString('foo'));
79+
$this->assertEquals('bar baz', $collector->ensureString('bar baz'));
8080
}
8181

82-
public function testShouldPrettyPrintArrayMessage()
82+
public function testShouldEnsureStringEncodeArrayToJson()
8383
{
8484
$collector = new MessageQueueCollector($this->createProducerMock());
8585

86-
$expected = "[\n &quot;foo&quot;,\n &quot;bar&quot;\n]";
87-
88-
$this->assertEquals($expected, $collector->prettyPrintMessage(['foo', 'bar']));
86+
$this->assertEquals('["foo","bar"]', $collector->ensureString(['foo', 'bar']));
8987
}
9088

9189
/**

0 commit comments

Comments
 (0)