Skip to content

Commit 730ef61

Browse files
author
julien
committed
first(commit): initial commit
0 parents  commit 730ef61

21 files changed

+423
-0
lines changed

README.md

Whitespace-only changes.

composer.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "twc/bus-bundle",
3+
"description": "Provide simple way to implement Message Bus concept in Symfony 4",
4+
"keywords": [
5+
"symfony",
6+
"command bus",
7+
"Domain events",
8+
"cqrs",
9+
"cqs",
10+
"php",
11+
"message bus"
12+
],
13+
"homepage": "https://github.com/thewalkingcoder/TwcBusBundle",
14+
"license": "MIT",
15+
"authors": [
16+
{
17+
"name": "Julien T. (TheWalkingCoder)",
18+
"email": "thewalkingcoder@gmail.com"
19+
}
20+
],
21+
"require": {
22+
"php": "^7.1",
23+
"symfony/dependency-injection": "^4.2",
24+
"symfony/framework-bundle": "^4.2"
25+
},
26+
"autoload": {
27+
"psr-4": {
28+
"Twc\\BusBundle\\": "src/"
29+
}
30+
}
31+
}

src/Command/CommandBus.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Twc\BusBundle\Command;
4+
5+
use Twc\BusBundle\Command\Interfaces\Command;
6+
use Twc\BusBundle\Command\Interfaces\CommandBusMiddleware;
7+
8+
abstract class CommandBus implements CommandBusMiddleware
9+
{
10+
abstract public function dispatch(Command $command): CommandResponse;
11+
}

src/Command/CommandBusDispatcher.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Twc\BusBundle\Command;
4+
5+
use Twc\BusBundle\Command\Interfaces\Command;
6+
use Twc\BusBundle\Command\Interfaces\CommandBusMiddleware;
7+
8+
9+
final class CommandBusDispatcher extends CommandBus implements CommandBusMiddleware
10+
{
11+
private $handlers;
12+
13+
public function __construct(iterable $handlers)
14+
{
15+
$this->handlers = [];
16+
17+
foreach ($handlers as $handler) {
18+
$this->handlers[$handler->listenTo()] = $handler;
19+
}
20+
21+
}
22+
23+
public function dispatch(Command $command): CommandResponse
24+
{
25+
$commandClass = get_class($command);
26+
27+
if (false === array_key_exists($commandClass, $this->handlers)) {
28+
throw new \LogicException("Handler for command $commandClass not found");
29+
}
30+
$handler = $this->handlers[$commandClass];
31+
return $handler->handle($command);
32+
}
33+
34+
public function appendMiddleware(CommandBusMiddleware $next): CommandBusMiddleware
35+
{
36+
return $this;
37+
}
38+
39+
}

src/Command/CommandBusFactory.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Twc\BusBundle\Command;
4+
5+
use Twc\BusBundle\Command\Interfaces\CommandBusMiddleware;
6+
7+
class CommandBusFactory
8+
{
9+
10+
public static function build(iterable $handlers, iterable $middlewares): CommandBusMiddleware
11+
{
12+
13+
$bus = new CommandBusDispatcher($handlers);
14+
/** @var CommandBusMiddleware $middleware */
15+
foreach ($middlewares as $middleware) {
16+
$bus = $middleware->appendMiddleware($bus);
17+
}
18+
19+
return $bus;
20+
}
21+
}

src/Command/CommandResponse.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Twc\BusBundle\Command;
4+
5+
final class CommandResponse
6+
{
7+
private $response;
8+
private $status;
9+
private $events;
10+
11+
public function __construct($response, int $status = 200, ?iterable $events = null)
12+
{
13+
14+
$this->response = $response;
15+
$this->status = $status;
16+
$this->events = $events;
17+
}
18+
19+
/**
20+
* @return mixed
21+
*/
22+
public function getResponse()
23+
{
24+
return $this->response;
25+
}
26+
27+
/**
28+
* @return int
29+
*/
30+
public function getStatus(): int
31+
{
32+
return $this->status;
33+
}
34+
35+
/**
36+
* @return bool
37+
*/
38+
public function hasEvents(): bool
39+
{
40+
return !empty($this->events);
41+
}
42+
43+
/**
44+
* @return iterable|null
45+
*/
46+
public function getEvents(): ?iterable
47+
{
48+
return $this->events;
49+
}
50+
51+
52+
}

src/Command/Interfaces/Command.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Twc\BusBundle\Command\Interfaces;
4+
5+
interface Command
6+
{
7+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Twc\BusBundle\Command\Interfaces;
4+
5+
6+
use Twc\BusBundle\Command\CommandResponse;
7+
8+
interface CommandBusMiddleware
9+
{
10+
11+
public function dispatch(Command $command): CommandResponse;
12+
13+
public function appendMiddleware(CommandBusMiddleware $next): self;
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Twc\BusBundle\Command\Interfaces;
4+
5+
use Twc\BusBundle\Command\CommandResponse;
6+
7+
interface CommandHandler
8+
{
9+
public function handle(Command $command): CommandResponse;
10+
11+
public function listenTo(): string;
12+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Twc\BusBundle\DependencyInjection;
4+
5+
use Symfony\Component\Config\FileLocator;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
use Symfony\Component\DependencyInjection\Extension\Extension;
8+
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
9+
10+
class TwcBusExtension extends Extension
11+
{
12+
public function load(array $configs, ContainerBuilder $container)
13+
{
14+
$loader = new YamlFileLoader(
15+
$container,
16+
new FileLocator(__DIR__ . '/../Resources/config')
17+
);
18+
19+
$loader->load('services.yaml');
20+
}
21+
22+
}

0 commit comments

Comments
 (0)