introducing native php serialize() support for Messenger transport

This commit is contained in:
Ryan Weaver 2019-01-22 15:29:52 -05:00
parent a9f8ca57af
commit b4788e4808
5 changed files with 78 additions and 2 deletions

View File

@ -1060,7 +1060,7 @@ class Configuration implements ConfigurationInterface
})
->end()
->children()
->scalarNode('id')->defaultValue(!class_exists(FullStack::class) && class_exists(Serializer::class) ? 'messenger.transport.symfony_serializer' : null)->end()
->scalarNode('id')->defaultValue('messenger.transport.native_php_serializer')->end()
->scalarNode('format')->defaultValue('json')->end()
->arrayNode('context')
->normalizeKeys(false)

View File

@ -1529,7 +1529,7 @@ class FrameworkExtension extends Extension
} else {
if ('messenger.transport.symfony_serializer' === $config['serializer']['id']) {
if (!$this->isConfigEnabled($container, $serializerConfig)) {
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".');
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".');
}
$container->getDefinition('messenger.transport.symfony_serializer')

View File

@ -24,6 +24,8 @@
</service>
<service id="Symfony\Component\Messenger\Transport\Serialization\SerializerInterface" alias="messenger.transport.serializer" />
<service id="messenger.transport.native_php_serializer" class="Symfony\Component\Messenger\Transport\Serialization\Serializer" />
<!-- Middleware -->
<service id="messenger.middleware.handle_message" class="Symfony\Component\Messenger\Middleware\HandleMessageMiddleware" abstract="true">
<argument /> <!-- Bus handler resolver -->

View File

@ -0,0 +1,29 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Messenger\Tests\Transport\Serialization;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;
class PhpSerializerTest extends TestCase
{
public function testEncodedIsDecodable()
{
$serializer = new PhpSerializer();
$envelope = new Envelope(new DummyMessage('Hello'));
$this->assertEquals($envelope, $serializer->decode($serializer->encode($envelope)));
}
}

View File

@ -0,0 +1,45 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Messenger\Transport\Serialization;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
/**
* @author Ruyan Weaver<ryan@symfonycasts.com>
*
* @experimental in 4.2
*/
class PhpSerializer implements SerializerInterface
{
/**
* {@inheritdoc}
*/
public function decode(array $encodedEnvelope): Envelope
{
if (empty($encodedEnvelope['body'])) {
throw new InvalidArgumentException('Encoded envelope should have at least a "body".');
}
return unserialize($encodedEnvelope['body']);
}
/**
* {@inheritdoc}
*/
public function encode(Envelope $envelope): array
{
return [
'body' => serialize($envelope),
];
}
}