The MessengerMaker is a Symfony bundle that simplifies the creation and registration of messages, distinctly separating queries, commands, and events.
This bundle adds a layer on top of Symfony/Messenger, leveraging its configuration. For more details, please refer to the Symfony Messenger Component documentation.
Make sure Composer is installed globally, as explained in the installation chapter of the Composer documentation.
Open a command console, enter your project directory and execute:
composer require amine-lejmi/messenger-maker
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
composer require amine-lejmi/messenger-maker
Then, enable the bundle by adding it to the list of registered bundles
in the config/bundles.php file of your project:
// config/bundles.php
return [
// ...
AmineLejmi\MessengerMaker\MessengerMakerBundle::class => ['all' => true],
];
To use the provided interfaces for automatically registering messages in their corresponding buses, add the following lines to your config/services.yaml file:
services:
# ...
_instanceof:
AmineLejmi\MessengerMaker\Contract\CommandHandlerInterface:
tags:
- { name: messenger.message_handler, bus: command.bus }
AmineLejmi\MessengerMaker\Contract\EventHandlerInterface:
tags:
- { name: messenger.message_handler, bus: event.bus }
AmineLejmi\MessengerMaker\Contract\QueryHandlerInterface:
tags:
- { name: messenger.message_handler, bus: query.bus }
Note : You can only add the interfaces you need in your project.
The bundle provides three different console commands for creating commands, queries, and events:
php bin/console make:messenger:command <command-name>
Note: The <command-name> must end with "Command".
php bin/console make:messenger:query <query-name>
Note: The <query-name> must end with "Query".
php bin/console make:messenger:event <event-name>
Note: The <event-name> must end with "Event".
For each of these commands, the console will prompt you:
config/messenger.yaml:$ php bin/console make:messenger:command SendEmailCommand
Register this command as : [none]:
[0] none
[1] low-priority
[2] high-priority
>
Note: After choosing a transport, the bundle will automatically
register the message in routing section inside
config/packages/messenger.yaml
framework:
# ...
messenger:
# ...
routing:
# ...
App\Messenger\Command\SendEmailCommand: low-priority
# ...
New property name (press <return> to stop adding fields):
>
Field type (enter ? to see all types) [string]:
>
Can this field be null (nullable) (yes/no) [no]:
>
Follow the prompts to complete the creation of the command, query, or event.
Depending on which command you executed, those folders will be created containing the corresponding files:
π¦project_dir
β£ π...
β£ π src
β β π Messenger
β β π Command
β β π CommandHandler
β β π Event
β β π EventHandler
β β π Query
β β π QueryHandler
Note: : Handlers implement interfaces to allow them to be dispatched to the corresponding bus:
<?php
namespace App\Messenger\Command;
class SendEmailCommand
{
private string $address;
private ?string $message;
public function __construct(string $address, ?string $message)
{
$this->address = $address;
$this->message = $message;
}
public function getAddress(): string
{
return $this->address;
}
public function getMessage(): ?string
{
return $this->message;
}
}
<?php
namespace App\Messenger\CommandHandler;
use AmineLejmi\MessengerMaker\Contract\CommandHandlerInterface;
use App\Messenger\Command\SendEmailCommand;
class SendEmailCommandHandler implements CommandHandlerInterface
{
public function __construct()
{
}
public function __invoke(SendEmailCommand $command)
{
$address = $command->getAddress();
$message = $command->getMessage();
// Do something with your variables
}
}
How can I help you explore Laravel packages today?