Comments (1)
Hi @FlorianCcj, I'm glad I can help!
If you consider the following interfaces:
<?php
interface CommandHandler
{
public function supports($command): bool
public function handle($command);
}
interface CommandBus
{
public function register(CommandHandler $commandHandler);
public function handle($command);
}
You can have a "base" CommandBus
implementation that registers CommandHandlers, and then when given a Command will try to find a Handler that supports it:
<?php
class RegisteringCommandBus implements CommandBus
{
private $commandHandlers = [];
public function add(CommandHandler $commandHandler)
{
$this->commandHandlers[] = $commandHandler;
}
public function handle($command)
{
foreach ($this->commandHandlers as $commandHandler) {
if ($commandHandler->supports($command)) {
return $commandHandler->handle($command);
}
}
throw new NotSupportedException('The given Command is not supported by any registered CommandHandlers.');
}
}
For most use cases, this is enough, but sometimes you want to always execute some code before or after all or some CommandHandlers, which is where the "Middleware" pattern is used: you create an implementation that does that extra task, and you inject in it the "base" implementation so you can call it before or after that task.
For example here's a CommandBus implementation that always log a message after any command handler have been executed:
<?php
class LogCommandBus implements CommandBus
{
private $commandBus;
private $logger;
public function __construct(
CommandBus $commandBus,
Logger $logger
) {
$this->commandBus = $commandBus;
$this->logger = $logger;
}
public function handle($command)
{
$value = $this->commandBus->handle($command);
$this->logger->log("C'est pas faux");
return $value;
}
}
In most of my use cases, I have no use for Middleware, so I usually skip the CommandBus entitrely but still stick to the Command / CommandHandler. You can read more about this here: http://gnugat.github.io/2017/09/20/pragmaticlean-command-bus.html
I hope this is useful, let me know if you have more questions.
from gnugat.github.io.
Related Issues (8)
- Console component configuration HOT 3
- Grammar HOT 2
- Atom entry link HOT 3
- Blog broken on HTTPS HOT 7
- Continue the idea HOT 2
- Improve alternatives description HOT 3
- Strict types landing rover HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gnugat.github.io.