simple-bus/jms-serializer-bundle-bridge
Symfony bundle that wires SimpleBus’s JMS Serializer ObjectSerializer as the default object serializer for SimpleBus AsynchronousBundle. Enable SimpleBusJMSSerializerBundleBridgeBundle in your AppKernel to use it.
Installation:
composer require simple-bus/jms-serializer-bundle-bridge
Ensure simple-bus/asynchronous-bundle and jms/serializer-bundle are also installed.
Enable the Bundle:
Add to config/bundles.php (Symfony 4.4+):
return [
// ...
SimpleBus\JMSSerializerBundleBridge\SimpleBusJMSSerializerBundleBridgeBundle::class => ['all' => true],
];
For Symfony <4.4, add to AppKernel.php:
$bundles[] = new \SimpleBus\JMSSerializerBundleBridge\SimpleBusJMSSerializerBundleBridgeBundle();
First Use Case:
Define a message class (e.g., app/src/Message/ProcessOrder.php):
namespace App\Message;
class ProcessOrder
{
public function __construct(
public string $orderId,
public array $items
) {}
}
Dispatch it via SimpleBus:
$bus->dispatch(new ProcessOrder('123', [['id' => 1, 'name' => 'Item']]));
The bundle automatically serializes/deserializes messages using JMSSerializer.
Message Definition:
Use DTOs (Data Transfer Objects) for messages. Annotate properties for JMSSerializer (e.g., @SerializedName, @Type):
use JMS\Serializer\Annotation as JMS;
class ProcessOrder
{
/** @JMS\Type("string") */
public string $orderId;
/** @JMS\Type("array<string, mixed>") */
public array $items;
}
Handling Messages:
Register handlers in config/packages/simple_bus.yaml:
simple_bus:
handlers:
App\Message\ProcessOrder: App\Handler\ProcessOrderHandler
Custom Serialization:
Extend JMSSerializer configuration (e.g., config/packages/jms_serializer.yaml):
jms_serializer:
metadata:
directories:
App:
namespace_prefix: "App\\Message"
path: "%kernel.project_dir%/config/serializer"
SimpleBusAsynchronousBundle to delegate to Messenger:
simple_bus_asynchronous:
messenger: true
symfony/validator for message validation:
use Symfony\Component\Validator\Constraints as Assert;
class ProcessOrder
{
/** @Assert\NotBlank */
public string $orderId;
}
ObjectSerializer in tests:
$serializer = $this->createMock(\SimpleBus\JMSSerializerBridge\ObjectSerializer::class);
$bus = new \SimpleBus\Asynchronous\AsynchronousBus($serializer, $messageDispatcher);
Circular References:
JMSSerializer may fail on circular references. Use @MaxDepth or @ExclusionPolicy:
/** @JMS\MaxDepth(1) */
class CircularReference {}
Type Mismatches:
Ensure message properties match serialized types (e.g., DateTime vs. string). Use @Type("DateTime<'Y-m-d'>") for custom formats.
Bundle Order:
Enable SimpleBusJMSSerializerBundleBridgeBundle after SimpleBusAsynchronousBundle and JMSSerializerBundle in bundles.php.
Caching Metadata: Clear cache after adding new message classes:
php bin/console cache:clear
Serialization Errors:
Enable JMSSerializer debug mode in config/packages/jms_serializer.yaml:
jms_serializer:
debug: true
Check logs for JMS\Serializer\Exception\RuntimeException.
Handler Not Found:
Verify the handler is registered in simple_bus.yaml and the message class is autoloaded.
Custom Metadata:
Create custom metadata files in config/serializer/App.Message.directory.yml:
App\Message\ProcessOrder:
exclusion_policy: ALL
properties:
orderId:
exclude: false
Event Listeners:
Subscribe to simple_bus.message.serialized and simple_bus.message.deserialized events for logging/auditing:
$eventDispatcher->addListener(
'simple_bus.message.serialized',
fn ($event) => Logger::info('Serialized: ' . $event->getMessage())
);
Alternative Serializers:
Override the default ObjectSerializer in config/packages/simple_bus.yaml:
simple_bus:
object_serializer: App\Custom\ObjectSerializer
How can I help you explore Laravel packages today?