Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Messenger Maker Laravel Package

amine-lejmi/messenger-maker

View on GitHub
Deep Wiki
Context7

AmineLejmi/MessengerMaker

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

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver