Skip to content
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

Add documentation #33

Open
jeckel opened this issue Dec 8, 2021 · 1 comment
Open

Add documentation #33

jeckel opened this issue Dec 8, 2021 · 1 comment

Comments

@jeckel
Copy link
Collaborator

jeckel commented Dec 8, 2021

Update readme on how to configure it, and how to use It with DI tools

With PHP/DI

return static function (ContainerBuilder $containerBuilder) {
    $containerBuilder->addDefinitions(
        [
            'command-handlers' => [
                ActivateUserHandler::class,
                CreateUserHandler::class,
            ],
            LoggerInterface::class => DI\autowire(Logger::class),
            CommandBus::class => function(ContainerInterface $container) {
                /** @var array<class-string<CommandHandler<Command>>> $handlers */
                $handlers = $container->get('command-handlers');
                return (new CommandBusBuilder($container))
                    ->addCommandHandler(...$handlers)
                    ->addDecorator(LoggerDecorator::class)
                    ->build();
            },
        ]
    );
};
@jeckel
Copy link
Collaborator Author

jeckel commented Dec 8, 2021

Documentation for obsolete CommandBus:

Simple and optimized command-bus implementation

Warning, this package is still a Work in progress

Installation

Require: php >= 8.0

Install it via composer

composer require jeckel-lab/command-bus

Usage

Initialisation

The best way to start is by using the CommandBusBuilder:

use JeckelLab\CommandBus\CommandBus\CommandBusBuilder;

/**
 * @var Psr\ContainerInterface $container
 */
$build = new CommandBusBuilder($container);

Then you can register your command handlers. Each handler should implement the CommandHandler interface.

// All command handler should implement the `CommandHandler` interface
$builder->addHandler(
    FirstCommandHandler::class,
    SecondCommandHandler::class
);

$builder->addHandler(
    ThridCommandHandler::class
)

Then you can also add some decorators to the bus. Decorators should implement the CommandBusDecorator interface, and it's suggest to extend directly the AbstractCommandBusDecorator class.

$builder->addDecorator(new SimpleCommandBusDecorator());

Finally, just build the command bus:

$commandBus = $builder->build();

Use it

When command bus is correctly build, it's easy to use it:

/** @var CommandResponse $response */
$response = $commandBus->dispatch(new MyCommand());

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

No branches or pull requests

1 participant