- PHP 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| .woodpecker | ||
| Classes | ||
| Configuration | ||
| Tests/Functional | ||
| .gitignore | ||
| .phpstorm.meta.php | ||
| composer.json | ||
| README.md | ||
DigiComp.FlowSymfonyBridge.Messenger
This packages brings a DI configuration for the symfony/messenger component, so it can be used easily in neos/flow projects.
To see how to use it, you probably want to have a look at the documentation of symfony/messenger.
Getting started
To get it integrated, you all need to do is to get message bus injected:
#[Flow\Inject]
protected MessageBusInterface $messageBus;
And later in your method:
$this->messageBus->dispatch(new CustomMessage())
You should configure a routing, to let the messenger know, over which transport your message should be handled:
DigiComp:
FlowSymfonyBridge:
Messenger:
transports:
"custom-messages":
dsn: "flow-doctrine://default?table_name=test_messenger_messages"
routing:
Acme\Vendor\Messenger\CustomMessage:
- "custom-messages"
In this example we are using a doctrine transport (the speciality "flow-transport" is a transport which uses the already existing connection to doctrine instead of creating a new one - for the rest of the DSN-Format have a look in the documentation of symfony/messenger)
A handler for your CustomMessage could look like this:
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
#[AsMessageHandler]
class CustomMessageHandler
{
public function __invoke(CustomMessage $message)
{
//your code here
}
}
It will be automatically found by Flow // the messenger and messages arriving at the bus will be handled by your handler.
You can annotate any method with AsMessage. This will intercept calls to this method and convert it to a Messenger message. You can control common envelope features using the annotation arguments (delay is given in milliseconds):
use DigiComp\FlowSymfonyBridge\Messenger\Annotations as Messenger;
class CustomService
{
#[Messenger\AsMessage(transportNames: ['custom-transport'], delay: 10000)]
public function doStuff(string $name): void
{
//your code here
}
}
Completely custom stamps can be added after the other arguments. As they are collected by a variadic parameter, they have to be passed positionally (bus name, delay, transport names, stamps):
use DigiComp\FlowSymfonyBridge\Messenger\Annotations as Messenger;
class CustomService
{
#[Messenger\AsMessage(null, 0, ['custom-transport'], new CustomStamp())]
public function doOtherStuff(string $name): void
{
//your code here
}
}
As the call is converted to a message, the annotated method returns null to its caller. Therefore it should be declared with a void or nullable return type.
If you want to use this as a replacement for Flowpack.JobQueue, you can annotate the methods you previously annotated with Flowpack\JobQueue\Common\Annotations\Defer with AsMessage instead.
Probably you'll want to consume the messengers with long living processes or as a cronjob. The Flow command for that task is messenger:consume (more help available)
Maybe have a look at DigiComp.FlowSymfonyBridge.Scheduler, which builds upon the messenger component to allow arbitrary scheduling