Skip to content

Provides an integration with the Symfony Messenger component #2398

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 2 commits into from
Dec 21, 2018

Conversation

dunglas
Copy link
Member

@dunglas dunglas commented Dec 19, 2018

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets n/a
License MIT
Doc PR api-platform/docs#708

This patch adds an easy way to dispatch messages to be handled asynchronously (or synchronously) using the Symfony Messenger component. It allows to create service-oriented endpoints very easily, but still in a restful way.

To be dispatched as a Symfony message, a resource just need to have be marked with messenger attribute:

<?php

namespace Dunglas\UserBundle\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ApiResource(
*      messenger=true,
 *     collectionOperations={
 *         "post"={"status"=202}
 *     },
 *     itemOperations={},
 *     outputClass=false
 * )
 */
class ResetPasswordRequest
{
    /**
     * @var string
     *
     * @Assert\NotBlank
     */
    public $username;
}

The corresponding Message handler:

<?php

namespace Dunglas\UserBundle\Handler;

use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

final class ResetPasswordRequestHandler implements MessageHandlerInterface
{
    public function __invoke(ResetPasswordRequest $forgotPassword)
    {
        // do something with the resource
    }
}

It's an alternative to https://github.com/sroze/api-platform-messenger.

@ragboyjr
Copy link
Contributor

@dunglas With the default configuration in sf messenger, how would oneself make a message handler that handled the remove message?

@ragboyjr
Copy link
Contributor

Also, in the example posted above, would the swagger generated documentation properly reflect that no response is made?

@dunglas
Copy link
Member Author

dunglas commented Dec 19, 2018

@dunglas With the default configuration in sf messenger, how would oneself make a message handler that handled the remove message?

For removal, you'll need a middleware, a simple handler would just get the message.

Also, in the example posted above, would the swagger generated documentation properly reflect that no response is made?

Not yet unfortunately, but it's something we're working on!

@bendavies
Copy link
Contributor

bendavies commented Dec 19, 2018

just a niggle but your example relies on #2396.

https://github.com/krakphp/api-platform-extra also implemented this, FYI.

Copy link
Contributor

@Toflar Toflar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So simple, yet so powerful. Loving it!
I've made a few remarks regarding possible future requests and just more flexibility in general but otherwise, super nice addition - well done!

@dunglas dunglas force-pushed the messenger branch 4 times, most recently from 09c62d7 to 5293729 Compare December 20, 2018 21:02
@dunglas dunglas merged commit 378904c into api-platform:master Dec 21, 2018
@dunglas dunglas deleted the messenger branch December 21, 2018 08:49
{
$this->messageBus->dispatch($data);

return $data;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd return the result of the HandledStamp instead if it exists.

*/
public function supports($data): bool
{
return true === $this->resourceMetadataFactory->create($this->getObjectClass($data))->getAttribute('messenger');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we have created a new ContextAwareDataPersisterInterface, so that the messenger attribute could be set at the operation level?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants