Developing a Command Bus in PHP

phpcqrsdomain driven design

Last updated: 7th January, 2018 By: Keith Mifsud

3 min read

In this series of blog posts I'll be developing a PHP library with a Command Bus, Command Handling and Subscription features. The main goal of this series is to describe the entire process of developing such a composer package, including making it available on Packagist  and implementing it in Laravel  through a separate package.

What are Commands, Handlers, Buses and Subscribers?

Simply put, a Command is a DTO (Data Transfer Object) with the single purpose of containing the details of a message, in this case a Command telling the model code to do something. Commands can be implemented in several layers of a code base. Mostly the Application and the Domain layers. However, these posts focus on the domain layer and thus the Application will call the Domain command from a service or controller once implemented. Commands are immutable, meaning that their properties (the message details) are set once the Command is constructed and they cannot be changed.

As described in it's name, a Command Handler handles the command. Therefore, a Command Handler will generally only have one notable method, named "handle" or something similar. However, most Command Handlers will have dependencies in their constructor as needed by the handle method such as repositories.

In few words, the Command Bus dispatches the Command to the Command Handler, whilst the Command Subscriber subscribes the Command and its corresponding Handler to the Command Bus so that it knows which Handler to call when a Command is initiated.

There's so many similar libraries, why develop another one?

Good question 😊 There are in fact a multitude of similar packages that provide similar functions and I've used several of them in many a project, however, so far I haven't found one that fits my code architecture and that does not have other dependencies which I don't require in my projects. Also, I need this package as a component to include in another much larger Event Sourcing Framework project, not to mention that I get the opportunity to build this with you! 😊

I want this component to be simple and flexible when implemented in other frameworks such as Laravel and Symfony. Finally, I want to be able to initialise the component by simply passing an array of Commands and their Command Handlers to the Command Subscriber and the Command Bus. Similar to this:

// An array of commands with handlers.
$commandsWithHandlers = [
    \KeithMifsud\Example\Model\Command\DoSomething::class =>
        \KeithMifsud\Example\Model\Handler\DoSomething::class,

    \KeithMifsud\Example\Model\Command\DoSomethingElse::class =>
        \KeithMifsud\Example\Model\Handler\DoSomethingElse::class,
];

// Bootstraps the component (named it Admiral cause it's cool !!)
$commandBus = new \SwellPhp\Admiral\CommandBus(
    new \SwellPhp\Admiral\ArrayResolver($commandsWithHandlers)
);

// Instantiates the command.
$command = new \KeithMifsud\Example\Model\Command\DoSomething(
    'some',
    'parameters'
);

// Dispath a command and it will be handled in the backgound.
$commandBus->dispatch($command);
Further to the above basic example, I would also want to be able to automatically inject any Command Handler dependencies when using a framework's application container such as the Laravel Service Container  which I'll explain in more at the end of this series.

Step by step guide into developing an Open Source PHP Composer Package for Admiral Command Component

Are you enjoying reading this article?

Subscribe to receive email notifications when I publish new articles and code libraries.

I will not share your email address with anyone, and you can unsubscribe at any time.
View the privacy policy for more information.

Throughout this small series of blog posts, I will detail the process of developing this component from the initial project setup to its official release as follows:

Part 4. The development of the Bus using a TDD approach.

Part 5. Packaging and versioning for composer including basic usage documentation.

Part 6. Releasing on Packagist.

Part 7. Developing a Laravel implementation.

Have you enjoyed reading this article?

Don't be selfish, share it with your friends 😉

Got questions or feedback? Leave a comment, I will reply.

Latest articles

Build and deploy a static online shop with Nuxt3 using Pinia Store and Stripe Checkout to Firebase

Published on 13th November, 2022
nuxt3piniastripefirebase

Nuxt3 final release is due in a few days and is already released when you're reading this. I've been looking forward to Nuxt3 for almost a year as I've been experimenting with it since the alpha release. I also tried to use Nuxt3 beta release on a couple of projects earlier this year but, unfortunately I was faced with several issues mainly related to missing plugins such as…

Planning the technology stack for an Event Sourcing project

Published on 12th September, 2019
event sourcingevent storedevopslaravel

The most common question I encounter when training or consulting with developers, Engineers and Software development laboratories about a new Event Sourcing project is how and where do we start. This question makes so much sense. I remember trying to get my head around Object Oriented Programming in practice (not the crap I learnt at school), let alone understanding Domain…

Start a new project with an Event Sourcing Architecture

Published on 16th July, 2019
event sourcingphpevent storming

I firmly believe that most, if not all, real-life process-driven applications can greatly benefit from Event Sourcing. If a system needs to know what happened in the past, then Event Sourcing is a good architecture fit. I wish it was that simple🧞! I find that a lot of Engineers, Product owners and developers, believe, or assume, that implementing Event Sourcing as an…

Build and deploy a static online shop with Nuxt3 using Pinia Store and Stripe Checkout to Firebase

Published on 13th November, 2022
nuxt3piniastripefirebase

Nuxt3 final release is due in a few days and is already released when you're reading this. I've been looking forward to Nuxt3 for almost a year as I've been experimenting with it since the alpha release. I also tried to use Nuxt3 beta release on a couple of projects earlier this…

Planning the technology stack for an Event Sourcing project

Published on 12th September, 2019
event sourcingevent storedevopslaravel

The most common question I encounter when training or consulting with developers, Engineers and Software development laboratories about a new Event Sourcing project is how and where do we start. This question makes so much sense. I remember trying to get my head around Object…

Start a new project with an Event Sourcing Architecture

Published on 16th July, 2019
event sourcingphpevent storming

I firmly believe that most, if not all, real-life process-driven applications can greatly benefit from Event Sourcing. If a system needs to know what happened in the past, then Event Sourcing is a good architecture fit. I wish it was that simple🧞! I find that a lot of Engineers…

Swipe gesture