amine-lejmi/messenger-maker

Separate and enhance the creation of symfony messenger queries, events and commands messages.

Installs: 353

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 1

Forks: 0

Open Issues: 0

Type:symfony-bundle

v1.0.1 2024-07-19 09:27 UTC

This package is auto-updated.

Last update: 2024-11-15 22:52:17 UTC


README

The MessengerMaker is a Symfony bundle that simplifies the creation and registration of messages, distinctly separating queries, commands, and events.

Key Features:

  • Automated Message Creation: Effortlessly creates and segregates queries, events, and commands.
  • Input and Getter Generation: Automatically generates inputs and getter methods for messages.
  • Bus Registration: Seamlessly registers messages in the specified message transport.

This bundle adds a layer on top of Symfony/Messenger, leveraging its configuration. For more details, please refer to the Symfony Messenger Component documentation.

Installation

Make sure Composer is installed globally, as explained in the installation chapter of the Composer documentation.

Applications that use Symfony Flex

Open a command console, enter your project directory and execute:

composer require amine-lejmi/messenger-maker

Applications that don't use Symfony Flex

Step 1: Download the Bundle

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

Step 2: Enable the Bundle

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],
];

After Installation

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.

Usage

The bundle provides three different console commands for creating commands, queries, and events:

1- Creating a new command

php bin/console make:messenger:command <command-name>

Note: The <command-name> must end with "Command".

2- Creating a new query

php bin/console make:messenger:query <query-name>

Note: The <query-name> must end with "Query".

3- Creating a new event

php bin/console make:messenger:event <event-name>

Note: The <event-name> must end with "Event".

How It Works

Executing the command

For each of these commands, the console will prompt you:

  1. to choose the corresponding transport if configured in 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
        # ...
  1. To add Any additional inputs required for the specific message
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.

Creation of the files

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:

  • query.bus
  • event.bus
  • command.bus

Examples

<?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 
    }
}