8000 introducing native php serialize() support for Messenger transport · symfony/symfony@18a4de7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 18a4de7

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

File tree

5 files changed

+91
-2
lines changed

5 files changed

+91
-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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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\Stamp\SerializerStamp;
17+
use Symfony\Component\Messenger\Stamp\ValidationStamp;
18+
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
19+
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;
20+
use Symfony\Component\Messenger\Transport\Serialization\Serializer;
21+
use Symfony\Component\Serializer as SerializerComponent;
22+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
23+
24+
class PhpSerializerTest extends TestCase
25+
{
26+
public function testEncodedIsDecodable()
27+
{
28+
$serializer = new PhpSerializer();
29+
30+
$envelope = new Envelope(new DummyMessage('Hello'));
31+
32+
$this->assertEquals($envelope, $serializer->decode($serializer->encode($envelope)));
33+
}
34+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
use Symfony\Component\Messenger\Exception\LogicException;
17+
use Symfony\Component\Messenger\Stamp\SerializerStamp;
18+
use Symfony\Component\Serializer\Encoder\JsonEncoder;
19+
use Symfony\Component\Serializer\Encoder\XmlEncoder;
20+
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
21+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
22+
use Symfony\Component\Serializer\Serializer as SymfonySerializer;
23+
use Symfony\Component\Serializer\SerializerInterface as SymfonySerializerInterface;
24+
25+
/**
26+
* @author Ruyan Weaver<ryan@symfonycasts.com>
27+
*
28+
* @experimental in 4.2
29+
*/
30+
class PhpSerializer implements SerializerInterface
31+
{
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function decode(array $encodedEnvelope): Envelope
36+
{
37+
if (empty($encodedEnvelope['body'])) {
9FEB 38+
throw new InvalidArgumentException('Encoded envelope should have at least a "body".');
39+
}
40+
41+
return unserialize($encodedEnvelope['body']);
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
public function encode(Envelope $envelope): array
48+
{
49+
return array(
50+
'body' => serialize($envelope),
51+
);
52+
}
53+
}

0 commit comments

Comments
 (0)
0