Skip to content

Commit 057d436

Browse files
committed
introducing native php serialize() support for Messenger transport
1 parent bd65272 commit 057d436

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
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 <[email protected]>
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 <[email protected]>
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<[email protected]>
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)