tomcizek / symfony-prooph-asynchronous-router
Installs: 294
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^7.1
- prooph/service-bus-symfony-bundle: ^0.6.0
- psr/container: ^1.0
- symfony/config: ~3.3|^4.0
- symfony/dependency-injection: ~3.3|^4.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ~2.0
- phpstan/phpstan: ^0.9
- phpunit/phpunit: ^6.0
- prooph/php-cs-fixer-config: ^0.2
- react/promise: ^2.5
- satooshi/php-coveralls: ^1.0
- symfony/framework-bundle: ~3.3|^4.0
This package is not auto-updated.
Last update: 2024-12-22 06:36:39 UTC
README
Symfony Bundle for easy routing of asynchronous messages.
Why bother?
It allows you to
- Automatically create MessageProducer services.
- Define routes for each service in yaml or xml config.
- Define AsynchronousMessageProducerBridge for each service, which you can implement using any infrastructure.
Quick start
1) Install this library through composer
composer require tomcizek/symfony-prooph-asynchronous-router
2) Register these Bundles in your kernel (individual, but might be config/bundles.php)
return [ Prooph\Bundle\ServiceBus\ProophServiceBusBundle::class => ['all' => true], TomCizek\AsynchronousRouter\ProophAsynchronousRouterBundle::class => ['all' => true], ];
3) Implement your own TomCizek\AsynchronousRouter\AsynchronousMessageProducerBridge
Simplest working example implementation for RabbitMq php-amqplib infrastructure might look like:
<?php declare(strict_types = 1); namespace YourApp\Infrastructure\Rabbit; use DateTime; use OldSound\RabbitMqBundle\RabbitMq\Producer; use Prooph\Common\Messaging\Message; use Prooph\Common\Messaging\MessageConverter; use Prooph\Common\Messaging\MessageDataAssertion; use TomCizek\AsynchronousRouter\AsynchronousMessageProducerBridge; final class RabbitAsynchronousMessageProducerBridge implements AsynchronousMessageProducerBridge { /** @var Producer */ private $producer; /** @var MessageConverter */ private $messageConverter; public function __construct(Producer $producer, MessageConverter $messageConverter) { $this->producer = $producer; $this->messageConverter = $messageConverter; } public function publishWithRoutingKey(Message $message, string $routingKey): void { $stringMessage = $this->convertMessageToString($message); $this->producer->publish($stringMessage, $routingKey); } private function convertMessageToString(Message $message): string { $messageData = $this->messageConverter->convertToArray($message); MessageDataAssertion::assert($messageData); $messageData['created_at'] = $message->createdAt()->format(DateTime::ATOM); return json_encode($messageData); } }
4) Setup your configuration for prooph components in your symfony *.yml config!
prooph_asynchronous_router: producers: # this key defines service id of created producer, this will create 'prooph_asynchronous_router.firstProducer' firstProducer: # bridge refers to service with your implementation of AsynchronousMessageProducerBridge bridge: 'firstAsynchronousMessageProducerBridge' # Routes are defined that key is message name and value is routing key. # AsynchronousMessageProducerBridge will then recieve message instance and routing key string # in publishWithRoutingKey method. routes: App\Namespace\FirstMessage: first_routing_key # this is standard service-bus-symfony-bundle configuration prooph_service_bus: event_buses: first_event_bus: router: # here you refer to automatically created service you defined few lines above. async_switch: 'prooph_asynchronous_router.firstProducer' services: # here you register your implementation of AsynchronousMessageProducerBridge to which we refer above. firstAsynchronousMessageProducerBridge: class: TomCizek\AsynchronousRouter\Tests\DependencyInjection\Fixture\Model\FirstAsynchronousMessageProducerBridgeAnother configuration example
Contribute
Please feel free to fork and extend existing or add new features and send a pull request with your changes! To establish a consistent code quality, please provide unit tests for all your changes and may adapt the documentation.