Skip to content

Commit c8bce91

Browse files
feature #29958 introducing native php serialize() support for Messenger transport (weaverryan, xabbuh)
This PR was merged into the 4.3-dev branch. Discussion ---------- introducing native php serialize() support for Messenger transport | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes and no | New feature? | yes and no | BC breaks? | maybe (yes if we change the default) | Deprecations? | no | Tests pass? | yes | Fixed tickets | #29163 | License | MIT | Doc PR | TODO! Messenger currently uses the Serialize to serialize to JSON and then unserialize. This creates a lot of issues: 1) The default serializer requires you to have getter & setter method (or public properties) for them to be serialized. This makes it easy for data to disappear. I've seen several really smart people have this problem. 2) Related to the above, the forced getters/setters (and no required constructor args) force you to design your message classes around this. It's not that the serializer is doing a bad job - it's just not the right use-case for it. This PR proposes simply using `serialize()` and `unserialize()`. This is the behavior we want: we want to put objects to sleep and wake them back up. I believe the original reason we did not do this was so that we could export "generic JSON", in case we wanted other workers (not our Symfony app) to consume the messages. But, that's an edge case, and could still be accomplished by creating your own serializer. Btw, Laravel uses `serialize()` as does Enqueue for (un)serializing Event objects. We're making our life more difficult for no benefit. Cheers! Commits ------- 97e2e32af4 Changing default serializer in Messenger component to PhpSerializer 3111cef9a4 Update src/Symfony/Bundle/FrameworkBundle/Resources/config/messenger.xml 4132bfebe7 updating CHANGELOGs and fixing tests b4788e4808 introducing native php serialize() support for Messenger transport
2 parents 20c31b2 + 0ed5d19 commit c8bce91

File tree

6 files changed

+12
-5
lines changed

6 files changed

+12
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ CHANGELOG
99
* Added `ControllerTrait::isFormValid()`
1010
* Added an `help_html` form option to display the `help` text as HTML
1111

12+
* [BC Break] When using Messenger, the default transport changed from
13+
using Symfony's serializer service to use `PhpSerializer`, which uses
14+
PHP's native `serialize()` and `unserialize()` functions. To use the
15+
original serialization method, set the `framework.messenger.serializer.id`
16+
config option to `messenger.transport.symfony_serializer`.
17+
1218
4.2.0
1319
-----
1420

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)

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')

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\PhpSerializer" />
28+
2729
<!-- Middleware -->
2830
<service id="messenger.middleware.handle_message" class="Symfony\Component\Messenger\Middleware\HandleMessageMiddleware" abstract="true">
2931
<argument /> <!-- Bus handler resolver -->

Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use Symfony\Component\Config\Definition\Processor;
2020
use Symfony\Component\Lock\Store\SemaphoreStore;
2121
use Symfony\Component\Messenger\MessageBusInterface;
22-
use Symfony\Component\Serializer\Serializer;
2322

2423
class ConfigurationTest extends TestCase
2524
{
@@ -295,7 +294,7 @@ class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphor
295294
'routing' => [],
296295
'transports' => [],
297296
'serializer' => [
298-
'id' => !class_exists(FullStack::class) && class_exists(Serializer::class) ? 'messenger.transport.symfony_serializer' : null,
297+
'id' => 'messenger.transport.native_php_serializer',
299298
'format' => 'json',
300299
'context' => [],
301300
],

Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ public function testMessengerRouting()
660660

661661
/**
662662
* @expectedException \Symfony\Component\DependencyInjection\Exception\LogicException
663-
* @expectedExceptionMessage The default Messenger serializer cannot be enabled as the Serializer support is not available. Try enabling it or running "composer require symfony/serializer-pack".
663+
* @expectedExceptionMessage The Messenger serializer cannot be enabled as the Serializer support is not available. Try enabling it or running "composer require symfony/serializer-pack".
664664
*/
665665
public function testMessengerTransportConfigurationWithoutSerializer()
666666
{

0 commit comments

Comments
 (0)