-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
618 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GeekCell\Ddd\Contracts\Application; | ||
|
||
interface Command | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GeekCell\Ddd\Contracts\Application; | ||
|
||
interface CommandBus | ||
{ | ||
/** | ||
* Dispatch a command to the appropriate handler. | ||
* | ||
* @param Command $command | ||
* @return mixed | ||
*/ | ||
public function dispatch(Command $command): mixed; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GeekCell\Ddd\Contracts\Application; | ||
|
||
use GeekCell\Ddd\Contracts\Core\Interactable; | ||
|
||
interface CommandHandler extends Interactable | ||
{ | ||
/** | ||
* Execute a command. | ||
* | ||
* @param Command $command | ||
* @return mixed | ||
*/ | ||
public function execute(Command $command): mixed; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GeekCell\Ddd\Contracts\Application; | ||
|
||
interface Query | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GeekCell\Ddd\Contracts\Application; | ||
|
||
interface QueryBus | ||
{ | ||
/** | ||
* Dispatch a query to the appropriate handler. | ||
* | ||
* @param Query $query | ||
* @return mixed | ||
*/ | ||
public function dispatch(Query $query): mixed; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GeekCell\Ddd\Contracts\Application; | ||
|
||
use GeekCell\Ddd\Contracts\Core\Interactable; | ||
|
||
interface QueryHandler extends Interactable | ||
{ | ||
/** | ||
* Execute a query. | ||
* | ||
* @param Query $query | ||
* @return mixed | ||
*/ | ||
public function execute(Query $query): mixed; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GeekCell\Ddd\Contracts\Core; | ||
|
||
interface Interactable | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GeekCell\Ddd\Infrastructure\InMemory; | ||
|
||
use GeekCell\Ddd\Contracts\Core\Interactable; | ||
use GeekCell\Ddd\Support\Attributes\ForType; | ||
|
||
abstract class AbstractBus | ||
{ | ||
/** | ||
* @var array<string, mixed> | ||
*/ | ||
protected array $handlers = []; | ||
|
||
/** | ||
* @return array<string, mixed> | ||
*/ | ||
public function getHandlers(): array | ||
{ | ||
return $this->handlers; | ||
} | ||
|
||
/** | ||
* Register a handler. | ||
* | ||
* The handler can be a callable or an object that implements | ||
* the Interactable interface and has the ForType class attribute. | ||
* | ||
* @param mixed $handler | ||
* @return void | ||
*/ | ||
abstract public function registerHandler(mixed $handler): void; | ||
|
||
/** | ||
* Register a callable handler. | ||
* | ||
* @param callable $callable The callable to register | ||
* @param string $parameterType The type of the parameter that the callable | ||
* | ||
* @return void | ||
*/ | ||
protected function registerCallableHandler( | ||
callable $callable, | ||
string $parameterType, | ||
): void | ||
{ | ||
$reflectionMethod = new \ReflectionMethod($callable, '__invoke'); | ||
foreach ($reflectionMethod->getParameters() as $parameter) { | ||
$type = $parameter->getType(); | ||
if ( | ||
is_subclass_of($type->getName(), $parameterType) && | ||
$type->allowsNull() === false) { | ||
$this->handlers[$type->getName()] = $callable; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Register a handler class. | ||
* | ||
* @param Interactable $handler The handler to register | ||
* @param string $attributeType Class attribute which is a | ||
* subclass of ForType | ||
* @return void | ||
*/ | ||
protected function registerHandlerClass( | ||
Interactable $handler, | ||
string $attributeType, | ||
): void | ||
{ | ||
$reflectionClass = new \ReflectionClass($handler); | ||
$attributes = $reflectionClass->getAttributes(); | ||
foreach ($attributes as $attribute) { | ||
if ( | ||
is_subclass_of($attributeType, ForType::class) && | ||
$attribute->getName() !== $attributeType) { | ||
continue; | ||
} | ||
|
||
/** @var ForType $context */ | ||
$context = $attribute->newInstance(); | ||
if (!$context->isValid()) { | ||
return; | ||
} | ||
|
||
foreach ($reflectionClass->getMethods() as $method) { | ||
if ($method->getName() === $context->getHandler()) { | ||
$this->handlers[$context->getType()] = [ | ||
$handler, | ||
$method->getName() | ||
]; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GeekCell\Ddd\Infrastructure\InMemory; | ||
|
||
use GeekCell\Ddd\Contracts\Application\Command; | ||
use GeekCell\Ddd\Contracts\Application\CommandBus as CommandBusInterface; | ||
use GeekCell\Ddd\Contracts\Application\CommandHandler; | ||
use GeekCell\Ddd\Support\Attributes\For\Command as ForCommand; | ||
|
||
class CommandBus extends AbstractBus implements CommandBusInterface | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public function dispatch(Command $command): mixed | ||
{ | ||
$commandType = get_class($command); | ||
if (!array_key_exists($commandType, $this->handlers)) { | ||
return null; | ||
} | ||
|
||
$handler = $this->handlers[$commandType]; | ||
if (is_callable($handler)) { | ||
return $handler($command); | ||
} | ||
|
||
if (is_array($handler)) { | ||
[$handler, $method] = $handler; | ||
return call_user_func([$handler, $method], $command); | ||
} | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function registerHandler(mixed $handler): void | ||
{ | ||
if (is_callable($handler)) { | ||
$this->registerCallableHandler($handler, Command::class); | ||
return; | ||
} | ||
|
||
if ($handler instanceof CommandHandler) { | ||
$this->registerHandlerClass($handler, ForCommand::class); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GeekCell\Ddd\Infrastructure\InMemory; | ||
|
||
use GeekCell\Ddd\Contracts\Application\Query; | ||
use GeekCell\Ddd\Contracts\Application\QueryBus as QueryBusInterface; | ||
use GeekCell\Ddd\Contracts\Application\QueryHandler; | ||
use GeekCell\Ddd\Support\Attributes\For\Query as ForQuery; | ||
|
||
final class QueryBus extends AbstractBus implements QueryBusInterface | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public function dispatch(Query $query): mixed | ||
{ | ||
$queryType = get_class($query); | ||
if (!array_key_exists($queryType, $this->handlers)) { | ||
return null; | ||
} | ||
|
||
$handler = $this->handlers[$queryType]; | ||
if (is_callable($handler)) { | ||
return $handler($query); | ||
} | ||
|
||
if (is_array($handler)) { | ||
[$handler, $method] = $handler; | ||
return call_user_func([$handler, $method], $query); | ||
} | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function registerHandler(mixed $handler): void | ||
{ | ||
if (is_callable($handler)) { | ||
$this->registerCallableHandler($handler, Query::class); | ||
return; | ||
} | ||
|
||
if ($handler instanceof QueryHandler) { | ||
$this->registerHandlerClass($handler, ForQuery::class); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GeekCell\Ddd\Support\Attributes\For; | ||
|
||
use Attribute; | ||
use GeekCell\Ddd\Contracts\Application\Command as CommandInterface; | ||
use GeekCell\Ddd\Support\Attributes\ForType; | ||
|
||
#[Attribute(Attribute::TARGET_CLASS)] | ||
class Command extends ForType | ||
{ | ||
protected function supports(): string | ||
{ | ||
return CommandInterface::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GeekCell\Ddd\Support\Attributes\For; | ||
|
||
use Attribute; | ||
use GeekCell\Ddd\Contracts\Application\Query as QueryInterface; | ||
use GeekCell\Ddd\Support\Attributes\ForType; | ||
|
||
#[Attribute(Attribute::TARGET_CLASS)] | ||
class Query extends ForType | ||
{ | ||
protected function supports(): string | ||
{ | ||
return QueryInterface::class; | ||
} | ||
} |
Oops, something went wrong.