Developing a Command Bus in PHP
Last updated: 7th January, 2018 By: Keith Mifsud
3 min read
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);
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.
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.