|
| 1 | +# BabelQueue for Symfony |
| 2 | + |
| 3 | +[](https://github.com/BabelQueue/symfony/actions/workflows/ci.yml) |
| 4 | +[](https://packagist.org/packages/babelqueue/symfony) |
| 5 | +[](LICENSE) |
| 6 | + |
| 7 | +> **Polyglot Queues, Simplified.** A Symfony Messenger serializer that speaks the |
| 8 | +> canonical BabelQueue envelope — so your Symfony services exchange messages with |
| 9 | +> Laravel, Go, Python, .NET and Node over one strict JSON format, on the broker |
| 10 | +> you already run. |
| 11 | +
|
| 12 | +This is the Symfony adapter. It plugs into **Symfony Messenger**: you keep |
| 13 | +Messenger's transports, handlers, worker and retry — BabelQueue only changes the |
| 14 | +**wire format** to the language-agnostic envelope (built by the shared core, |
| 15 | +[`babelqueue/php-sdk`](https://packagist.org/packages/babelqueue/php-sdk)). The |
| 16 | +full standard is documented at **[babelqueue.com](https://babelqueue.com)**. |
| 17 | + |
| 18 | +## Requirements |
| 19 | + |
| 20 | +- PHP `^8.2` |
| 21 | +- Symfony `^6.4 | ^7.0` (Messenger) |
| 22 | +- A broker Messenger supports (AMQP/RabbitMQ, Redis, …) |
| 23 | + |
| 24 | +## Installation |
| 25 | + |
| 26 | +```bash |
| 27 | +composer require babelqueue/symfony |
| 28 | +``` |
| 29 | + |
| 30 | +Enable the bundle (if you don't use Symfony Flex) in `config/bundles.php`: |
| 31 | + |
| 32 | +```php |
| 33 | +return [ |
| 34 | + // ... |
| 35 | + BabelQueue\Symfony\BabelQueueBundle::class => ['all' => true], |
| 36 | +]; |
| 37 | +``` |
| 38 | + |
| 39 | +## Configuration |
| 40 | + |
| 41 | +Point a Messenger transport at the BabelQueue serializer, and map inbound URNs to |
| 42 | +message classes: |
| 43 | + |
| 44 | +```yaml |
| 45 | +# config/packages/messenger.yaml |
| 46 | +framework: |
| 47 | + messenger: |
| 48 | + transports: |
| 49 | + babel: |
| 50 | + dsn: '%env(MESSENGER_TRANSPORT_DSN)%' # e.g. amqp:// or redis:// |
| 51 | + serializer: 'babelqueue.messenger.serializer' |
| 52 | + routing: |
| 53 | + 'App\Message\OrderCreated': babel |
| 54 | +``` |
| 55 | +
|
| 56 | +```yaml |
| 57 | +# config/packages/babelqueue.yaml |
| 58 | +babelqueue: |
| 59 | + queue: 'orders' # written to the envelope meta.queue |
| 60 | + messages: # urn => message class (needed to consume) |
| 61 | + 'urn:babel:orders:created': 'App\Message\OrderCreated' |
| 62 | +``` |
| 63 | +
|
| 64 | +## A message |
| 65 | +
|
| 66 | +Implement `BabelQueue\Symfony\Contracts\PolyglotMessage`: |
| 67 | + |
| 68 | +```php |
| 69 | +use BabelQueue\Symfony\Contracts\PolyglotMessage; |
| 70 | +
|
| 71 | +final class OrderCreated implements PolyglotMessage |
| 72 | +{ |
| 73 | + public function __construct(public int $orderId) {} |
| 74 | +
|
| 75 | + public function getBabelUrn(): string |
| 76 | + { |
| 77 | + return 'urn:babel:orders:created'; |
| 78 | + } |
| 79 | +
|
| 80 | + public function toPayload(): array |
| 81 | + { |
| 82 | + return ['order_id' => $this->orderId]; |
| 83 | + } |
| 84 | +
|
| 85 | + public static function fromBabelPayload(array $data): static |
| 86 | + { |
| 87 | + return new self((int) $data['order_id']); |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +## Produce & consume |
| 93 | + |
| 94 | +```php |
| 95 | +// produce — a normal Messenger dispatch |
| 96 | +$bus->dispatch(new OrderCreated(1042)); |
| 97 | +``` |
| 98 | + |
| 99 | +On the wire it becomes the canonical envelope, readable by every BabelQueue SDK: |
| 100 | + |
| 101 | +```json |
| 102 | +{ |
| 103 | + "job": "urn:babel:orders:created", |
| 104 | + "trace_id": "…", |
| 105 | + "data": { "order_id": 1042 }, |
| 106 | + "meta": { "id": "…", "queue": "orders", "lang": "php", "schema_version": 1, "created_at": 1749132727000 }, |
| 107 | + "attempts": 0 |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +```php |
| 112 | +// consume — a normal Messenger handler, routed by message class |
| 113 | +use Symfony\Component\Messenger\Attribute\AsMessageHandler; |
| 114 | +
|
| 115 | +#[AsMessageHandler] |
| 116 | +final class OnOrderCreated |
| 117 | +{ |
| 118 | + public function __invoke(OrderCreated $message): void |
| 119 | + { |
| 120 | + // ... |
| 121 | + } |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +Run the worker as usual: `php bin/console messenger:consume babel`. |
| 126 | + |
| 127 | +## How it maps to Messenger |
| 128 | + |
| 129 | +- **Routing** is Messenger's job: it routes the decoded message class to a handler. |
| 130 | +- **Retry** bridges both ways — Messenger's `RedeliveryStamp` ⇄ the envelope's |
| 131 | + top-level `attempts`. |
| 132 | +- **Tracing** — the inbound `trace_id` is attached as a `BabelTraceStamp`; re-emit |
| 133 | + it on a downstream message (with the stamp) to continue the trace. |
| 134 | +- **Unknown URN** — a message whose URN isn't mapped throws |
| 135 | + `MessageDecodingFailedException`, so Messenger routes it to your failure |
| 136 | + transport (the idiomatic Symfony behavior). |
| 137 | + |
| 138 | +## Testing |
| 139 | + |
| 140 | +```bash |
| 141 | +composer install |
| 142 | +vendor/bin/phpunit |
| 143 | +``` |
| 144 | + |
| 145 | +## License |
| 146 | + |
| 147 | +MIT © Muhammet Şafak. See [LICENSE](LICENSE). |
0 commit comments