-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Docs for the Messenger integration #708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# Symfony Messenger Integration: CQRS and Async Message Processing | ||
|
||
API Platform provides an integration with the [Symfony Messenger Component](https://symfony.com/doc/current/messenger.html). | ||
|
||
This feature allows to implement the [Command Query Responsibility Segregation (CQRS)](https://martinfowler.com/bliki/CQRS.html) pattern in a convenient way. | ||
It also makes it easy to send messages through the web API that will be consumed asynchronously. | ||
|
||
Many transports are supported to dispatch messages to async consumers, including RabbitMQ, Apache Kafka, Amazon SQS and Google Pub/Sub. | ||
|
||
## Installing Symfony Messenger | ||
|
||
To enable the support of Messenger, install the library: | ||
|
||
$ docker-compose exec php composer require messenger | ||
|
||
## Dispatching a Resource through the Message Bus | ||
|
||
Set the `messenger` attribute to `true`, and API Platform will automatically dispatch the API Resource instance as a message using the message bus provided by the Messenger Component: | ||
|
||
```php | ||
<?php | ||
|
||
// api/src/Entity/ResetPasswordRequest.php | ||
|
||
namespace App\Entity; | ||
|
||
use ApiPlatform\Core\Annotation\ApiResource; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
/** | ||
* @ApiResource( | ||
* messenger=true, | ||
* collectionOperations={ | ||
* "post"={"status"=202} | ||
* }, | ||
* itemOperations={}, | ||
* outputClass=false | ||
* ) | ||
*/ | ||
final class ResetPasswordRequest | ||
{ | ||
/** | ||
* @var string | ||
* | ||
* @Assert\NotBlank | ||
*/ | ||
public $username; | ||
} | ||
``` | ||
|
||
Because the `messenger` attribute is `true`, when a `POST` will be handled by API Platform, the corresponding instance of the `ResetPasswordRequest` will be dispatched. | ||
|
||
For this example, only the `POST` operation is enabled. | ||
We use the `status` attribute to configure API Platform to return a [202 Accepted HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/202). | ||
It indicates that the request has been received and will be treated later, without giving an immediate return to the client. | ||
Finally, the `output_class` attribute is set to `false`, so the HTTP response that will be generated by API Platform will be empty, and the [serialization process](serialization.md) will be skipped. | ||
|
||
## Registering a Message Handler | ||
|
||
To process the message that will be dispatched, [a handler](https://symfony.com/doc/current/messenger.html#registering-handlers) must be created: | ||
|
||
```php | ||
<?php | ||
|
||
// api/src/Handler/ResetPasswordRequestHandler.php | ||
|
||
namespace App\Handler; | ||
|
||
use App\Entity\ResetPasswordRequest; | ||
use Symfony\Component\Messenger\Handler\MessageHandlerInterface; | ||
|
||
final class ResetPasswordRequestHandler implements MessageHandlerInterface | ||
{ | ||
public function __invoke(ResetPasswordRequest $forgotPassword) | ||
{ | ||
// do something with the resource | ||
} | ||
} | ||
``` | ||
|
||
That's all! | ||
|
||
By default, the handler will process your message synchronously. | ||
If you want it to be consumed asynchronously (e.g. by a worker machine), [configure a transport and the consumer](https://symfony.com/doc/current/messenger.html#transports). | ||
|
||
## Accessing to the Data Returned by the Handler | ||
|
||
API Platform automatically uses the `Symfony\Component\Messenger\Stamp\HandledStamp` when set. | ||
It means that if you use a synchronous handler, the data returned by the `__invoke` method replaces the original data. | ||
|
||
## Detecting Removals | ||
|
||
When a `DELETE` operation occurs, API Platform automatically adds a `ApiPlatform\Core\Bridge\Symfony\Messenger\RemoveStamp` ["stamp"](https://symfony.com/doc/current/components/messenger.html#adding-metadata-to-messages-envelopes) instance to the "envelope". | ||
To differentiate typical persists calls (create and update) and removal calls, check for the presence of this stamp using [a custom "middleware"](https://symfony.com/doc/current/components/messenger.html#adding-metadata-to-messages-envelopes). |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.