simple-bus/serialization
Generic PHP interfaces and classes for serializing SimpleBus message objects, supporting consistent message encoding/decoding for transport and storage. Part of the SimpleBus ecosystem; documentation and issues are maintained in the main SimpleBus repository.
Installation:
composer require simple-bus/serialization
Add to composer.json if using SimpleBus MessageBus:
"require": {
"simple-bus/serialization": "^6.0"
}
First Use Case: Serialize a message into an envelope for transport (e.g., to a queue or HTTP API):
use SimpleBus\Serializer\MessageInEnvelopeSerializer;
use SimpleBus\Serializer\MessageInEnvelopeSerializerInterface;
$serializer = new MessageInEnvelopeSerializer();
$envelope = $serializer->serialize($message, $messageName);
Deserialization:
$deserialized = $serializer->deserialize($envelope);
Key Classes:
MessageInEnvelopeSerializer: Core serializer for messages.MessageInEnvelopeSerializerInterface: Contract for custom implementations.MessageInEnvelope: Represents a serialized message with metadata.src/MessageInEnvelope.php: Core envelope structure.src/MessageInEnvelopeSerializer.php: Default implementation (uses json_encode/json_decode by default).$envelope = $serializer->serialize(
$command = new PlaceOrder($orderId, $items),
'App\Command\PlaceOrder'
);
$envelope to a queue (e.g., Redis, RabbitMQ) or HTTP endpoint.$envelope = json_decode($rawEnvelope, true); // If transported as JSON
$deserialized = $serializer->deserialize($envelope);
Extend MessageInEnvelopeSerializer for non-JSON formats (e.g., XML, Protocol Buffers):
class XmlMessageSerializer implements MessageInEnvelopeSerializerInterface {
public function serialize($message, string $messageName): MessageInEnvelope {
$xml = $this->messageToXml($message);
return new MessageInEnvelope($messageName, $xml);
}
public function deserialize(MessageInEnvelope $envelope): object {
return $this->xmlToMessage($envelope->getMessage());
}
}
Use with SimpleBus\MessageBus\MessageBus for decoupled messaging:
$bus = new MessageBus([
new QueueBus([
new RedisQueue(),
new SerializingMessageConverter($serializer),
]),
]);
AppServiceProvider):
$this->app->singleton(MessageInEnvelopeSerializerInterface::class, function ($app) {
return new MessageInEnvelopeSerializer();
});
$serializer->serialize($message, $messageName);
$this->log->info("Serialized message: {$messageName}");
MessageInEnvelopeSerializerInterface for unit tests:
$mockSerializer = $this->createMock(MessageInEnvelopeSerializerInterface::class);
$mockSerializer->method('serialize')->willReturn(new MessageInEnvelope('Test', []));
Message Name Mismatch:
$messageName in MessageInEnvelope doesn’t match the expected class.$envelope->getMessageName() before deserializing or use a factory pattern.Circular References:
Order ↔ Customer).JsonSerializable or a custom serializer with JSON_THROW_ON_ERROR.PHP 8.0+ Strict Types:
TypeError if return types aren’t strict.object return types or generics (if using PHP 8.1+).Performance:
serialize()/unserialize() for PHP-native objects or Protocol Buffers for binary formats.var_dump($envelope->getMessageName(), $envelope->getMessage());
$serialized = $serializer->serialize($message, 'Test');
file_put_contents('debug.json', json_encode($serialized->getMessage()));
json_encode($data, JSON_THROW_ON_ERROR); // For strict validation
Custom Serializers:
Implement MessageInEnvelopeSerializerInterface for domain-specific formats (e.g., Avro, MessagePack):
class AvroSerializer implements MessageInEnvelopeSerializerInterface {
public function serialize($message, string $messageName): MessageInEnvelope {
$avroData = $this->avroEncode($message);
return new MessageInEnvelope($messageName, $avroData);
}
// ...
}
Metadata Handling:
Extend MessageInEnvelope to include headers/metadata:
class ExtendedEnvelope extends MessageInEnvelope {
public function __construct(
string $messageName,
$message,
array $headers = []
) {
parent::__construct($messageName, $message);
$this->headers = $headers;
}
}
Laravel Integration:
$this->app->bind(MessageInEnvelopeSerializerInterface::class, function () {
return new MessageInEnvelopeSerializer(new JsonEncoder());
});
class SendEmailJob implements ShouldQueue {
use Dispatchable, InteractsWithQueue;
public function handle() {
$serializer = app(MessageInEnvelopeSerializerInterface::class);
$envelope = $serializer->serialize(new SendEmail($email), SendEmail::class);
// Store $envelope in DB/Redis for later processing
}
}
json_encode() with JSON_THROW_ON_ERROR (PHP 7.3+). For older PHP, add:
$serializer = new MessageInEnvelopeSerializer(new JsonEncoder(JSON_THROW_ON_ERROR));
$messageName uses fully qualified class names (e.g., App\Command\PlaceOrder), not short names.How can I help you explore Laravel packages today?