PHPackages                             maxkaemmerer/commands - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [CLI &amp; Console](/categories/cli)
4. /
5. maxkaemmerer/commands

ActiveLibrary[CLI &amp; Console](/categories/cli)

maxkaemmerer/commands
=====================

Provides Interfaces and Implementations for Command, CommandHandler, CommandBus communication

v1.0.0(7y ago)0271MITPHPPHP &gt;=7.2

Since Jul 21Pushed 7y agoCompare

[ Source](https://github.com/maxkaemmerer/commands)[ Packagist](https://packagist.org/packages/maxkaemmerer/commands)[ RSS](/packages/maxkaemmerer-commands/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (1)Dependencies (2)Versions (3)Used By (1)

maxkaemmerer/commands
=====================

[](#maxkaemmerercommands)

[![Travis branch](https://camo.githubusercontent.com/924d12b50dbed51589d39b033e32e5f45010dcd9e863c7df65360f848269c230/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6d61786b61656d6d657265722f636f6d6d616e64732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/maxkaemmerer/commands)[![Coveralls github](https://camo.githubusercontent.com/85c754f483014c8cdb0e8d36d64b29da18f4455a4e553de570cb7859339002bf/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f6d61786b61656d6d657265722f636f6d6d616e64732f6d61737465722e7376673f7374796c653d666c61742d737175617265266272616e63683d6d6173746572)](https://coveralls.io/github/maxkaemmerer/commands?branch=master)[![PHP from Packagist](https://camo.githubusercontent.com/205bab625c0e5aade84bb2b5b3ce4e1f74dec3196371afe0b7e7061052722b10/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6d61786b61656d6d657265722f636f6d6d616e64732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/maxkaemmerer/commands)[![Packagist](https://camo.githubusercontent.com/70f5e622b41465483861135f118a79d03907270adc11729a2e9556ea7baf4a64/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d61786b61656d6d657265722f636f6d6d616e64732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/maxkaemmerer/commands)[![Packagist](https://camo.githubusercontent.com/252cbf4170e33b009bd74269b4596a2fdecff1c6bbe72c7cb3e169250bfbe1e6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6d61786b61656d6d657265722f636f6d6d616e64732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/maxkaemmerer/commands)

Description:
------------

[](#description)

This library offers interfaces and implementations for a simple command, command-handler, command-bus structure.

This is of course not an original idea, but my preferred and fairly simple implementation.

The code is fully tested, I however do not take responsibility for use in production.

Use at your own risk.

Installation:
-------------

[](#installation)

`composer require maxkaemmerer/commands`

Usage:
------

[](#usage)

Generally you don't want to register each `CommandHandler` by hand. You might want to use dependency injection via a container or service-manager.

(An example for the Symfony Framework would be using a `CompilerPass`)

You would also want to inject the `CommandBus` itself via dependency injection wherever you need it.

Feel free to create your own implementations of `CommandBus` and `Payload` if you require something more advanced.

#### Register a Command Handler:

[](#register-a-command-handler)

Register a `CommandHandler` to the `CommandBus`. The `CommandHandler`'s `handles()` method returns the name of the `Command` that it handles.

Best practice would be using the fully qualified class name of the command. `MyCommand::class`

The `CommandHandler::handle($command)` method is where your actual domain logic happens.

Feel free to inject services, a container, or whatever else you need, into your `CommandHandler`s.

```
$commandBus = new SimpleCommandBus();

$commandBus->registerHandler(new class implements CommandHandler
{

    /**
     * @param Command $command
     */
    public function handle(Command $command): void
    {
        echo $command->payload()->get('message');
    }

    /**
     * @return string
     * The name of the command this handler handles. Command::name()
     */
    public function handles(): string
    {
        return 'commandName';
    }

});

```

#### Dispatch a Command:

[](#dispatch-a-command)

Dispatching a `Command` causes the `CommandBus` to look for a `CommandHandler` who's `CommandHandler:handles()` method matches the `Command`'s name, specified by `Command:name()`, and calls it's `CommandHandler::handle($command)` method.

IMPORTANT: `CommandHandler`'s and the `CommandBus` never return anything.

```
...

// The CommandHandler was registered

$commandBus->dispatch(new class implements Command
{

    /**
     * @return CommandPayload
     */
    public function payload(): CommandPayload
    {
        // You would of course set the Payload in the constructor of your Command implementation
        return Payload::fromArray(['message' => 'Hello World!']);
    }

    /**
     * @return string
     * The name of this command, it is recommended to use the fully qualified class name.
     */
    public function name(): string
    {
        return 'commandName';
    }

});

```

#### Full Example:

[](#full-example)

```
