Skip to content

Commit b4788e4

Browse files
committed
introducing native php serialize() support for Messenger transport
1 parent a9f8ca5 commit b4788e4

File tree

5 files changed

+78
-2
lines changed

5 files changed

+78
-2
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@ function ($a) {
10601060
})
10611061
->end()
10621062
->children()
1063-
->scalarNode('id')->defaultValue(!class_exists(FullStack::class) && class_exists(Serializer::class) ? 'messenger.transport.symfony_serializer' : null)->end()
1063+
->scalarNode('id')->defaultValue('messenger.transport.native_php_serializer')->end()
10641064
->scalarNode('format')->defaultValue('json')->end()
10651065
->arrayNode('context')
10661066
->normalizeKeys(false)

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1529,7 +1529,7 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
15291529
} else {
15301530
if ('messenger.transport.symfony_serializer' === $config['serializer']['id']) {
15311531
if (!$this->isConfigEnabled($container, $serializerConfig)) {
1532-
throw new LogicException('The default Messenger serializer cannot be enabled as the Serializer support is not available. Try enabling it or running "composer require symfony/serializer-pack".');
1532+
throw new LogicException('The Messenger serializer cannot be enabled as the Serializer support is not available. Try enabling it or running "composer require symfony/serializer-pack".');
15331533
}
15341534

15351535
$container->getDefinition('messenger.transport.symfony_serializer')

src/Symfony/Bundle/FrameworkBundle/Resources/config/messenger.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
</service>
2525
<service id="Symfony\Component\Messenger\Transport\Serialization\SerializerInterface" alias="messenger.transport.serializer" />
2626

27+
<service id="messenger.transport.native_php_serializer" class="Symfony\Component\Messenger\Transport\Serialization\Serializer" />
28+
2729
<!-- Middleware -->
2830
<service id="messenger.middleware.handle_message" class="Symfony\Component\Messenger\Middleware\HandleMessageMiddleware" abstract="true">
2931
<argument /> <!-- Bus handler resolver -->
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Tests\Transport\Serialization;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Messenger\Envelope;
16+
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
17+
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;
18+
19+
class PhpSerializerTest extends TestCase
20+
{
21+
public function testEncodedIsDecodable()
22+
{
23+
$serializer = new PhpSerializer();
24+
25+
$envelope = new Envelope(new DummyMessage('Hello'));
26+
27+
$this->assertEquals($envelope, $serializer->decode($serializer->encode($envelope)));
28+
}
29+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Transport\Serialization;
13+
14+
use Symfony\Component\Messenger\Envelope;
15+
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
16+
17+
/**
18+
* @author Ruyan Weaver<ryan@symfonycasts.com>
19+
*
20+
* @experimental in 4.2
21+
*/
22+
class PhpSerializer implements SerializerInterface
23+
{
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
public function decode(array $encodedEnvelope): Envelope
28+
{
29+
if (empty($encodedEnvelope['body'])) {
30+
throw new InvalidArgumentException('Encoded envelope should have at least a "body".');
31+
}
32+
33+
return unserialize($encodedEnvelope['body']);
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function encode(Envelope $envelope): array
40+
{
41+
return [
42+
'body' => serialize($envelope),
43+
];
44+
}
45+
}

0 commit comments

Comments
 (0)