Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4790ab9144 | ||
|
|
070f607187 | ||
|
|
6671ec3eac | ||
|
|
0bca797e8d | ||
|
|
474ccbb012 |
No files matched your search
+1
-1
@@ -20,7 +20,7 @@
|
||||
"react/socket": "^1.15",
|
||||
"react/stream": "^1.3",
|
||||
"react/promise-timer": "^1.10",
|
||||
"symfony/uid": "^6.0|^7.0"
|
||||
"symfony/uid": "^6.0|^7.0|^8.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^11.0",
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace NoccyLabs\React\CommandBus\Attributes;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_METHOD|\Attribute::TARGET_CLASS)]
|
||||
class RpcCallable
|
||||
{
|
||||
public function __construct(
|
||||
public readonly string $name,
|
||||
)
|
||||
{
|
||||
}
|
||||
}
|
||||
+37
-1
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace NoccyLabs\React\CommandBus;
|
||||
|
||||
use ReflectionFunction;
|
||||
use ReflectionNamedType;
|
||||
use React\Promise\Deferred;
|
||||
use React\Promise\Promise;
|
||||
use React\Promise\PromiseInterface;
|
||||
@@ -17,10 +19,13 @@ class Command
|
||||
/** @var callable $handler The handler */
|
||||
private $handler;
|
||||
|
||||
public function __construct(string $name, callable $handler)
|
||||
private ?array $signature;
|
||||
|
||||
public function __construct(string $name, callable $handler, ?array $signature = null)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->handler = $handler;
|
||||
$this->signature = $signature;
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
@@ -38,5 +43,36 @@ class Command
|
||||
});
|
||||
}
|
||||
|
||||
public function parameters(): array
|
||||
{
|
||||
$refl = new ReflectionFunction($this->handler);
|
||||
$args = [];
|
||||
|
||||
foreach ($refl->getParameters() as $parameter) {
|
||||
$name = $parameter->getName();
|
||||
$type = null;
|
||||
if (!$parameter->hasType()) {
|
||||
$type = 'any';
|
||||
} else {
|
||||
$type = $parameter->getType();
|
||||
if ($type instanceof ReflectionNamedType && $type->isBuiltin()) {
|
||||
$type = ($type->allowsNull() ? "?" : "") . $type->getName();
|
||||
} else {
|
||||
$type = null;
|
||||
}
|
||||
}
|
||||
|
||||
if ($parameter->isDefaultValueAvailable()) $type = "{$type}=".\json_encode($parameter->getDefaultValue(),\JSON_UNESCAPED_SLASHES);
|
||||
if ($type !== null) $args[$name] = $type;
|
||||
}
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
public function getSignature(): array
|
||||
{
|
||||
return $this->signature ?? $this->parameters();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -170,7 +170,7 @@ class CommandBus implements CommandBusInterface
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function execute(string $command, array $context): PromiseInterface
|
||||
public function execute(string $command, array $context = []): PromiseInterface
|
||||
{
|
||||
$context = new Context($command, $context);
|
||||
return $this->executeContext($context);
|
||||
|
||||
@@ -161,7 +161,7 @@ class CommandBusClient implements CommandBusInterface
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function execute(string $command, array $context): PromiseInterface
|
||||
public function execute(string $command, array $context = []): PromiseInterface
|
||||
{
|
||||
if (!$this->connection || !$this->connection->isWritable()) {
|
||||
$this->reconnect();
|
||||
|
||||
@@ -17,7 +17,7 @@ interface CommandBusInterface extends EventEmitterInterface
|
||||
* @param array $context Data to pass in the call context
|
||||
* @®eturn PromiseInterface A promise that will be resolved with the result
|
||||
*/
|
||||
public function execute(string $command, array $context): PromiseInterface;
|
||||
public function execute(string $command, array $context = []): PromiseInterface;
|
||||
|
||||
/**
|
||||
* Notify all connected clients to emit the notify event, and also be
|
||||
|
||||
+37
-1
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace NoccyLabs\React\CommandBus;
|
||||
|
||||
use Evenement\EventEmitterInterface;
|
||||
use Evenement\EventEmitterTrait;
|
||||
|
||||
@@ -15,8 +16,10 @@ class CommandRegistry implements CommandResolverInterface, EventEmitterInterface
|
||||
const EVENT_REGISTERED = 'registered';
|
||||
const EVENT_UNREGISTERED = 'unregistered';
|
||||
|
||||
/** @var array<string,Command> */
|
||||
/** @var array<string,Command> The registered commands and callbacks */
|
||||
private array $commands = [];
|
||||
/** @var array<string,array<string>> Commands registered by scanning a class */
|
||||
private array $classCommands = [];
|
||||
|
||||
public function register(string $command, callable $handler): void
|
||||
{
|
||||
@@ -29,6 +32,29 @@ class CommandRegistry implements CommandResolverInterface, EventEmitterInterface
|
||||
}
|
||||
}
|
||||
|
||||
public function registerClass(object $target): void
|
||||
{
|
||||
$a = function (\ReflectionMethod $r, string $a) {
|
||||
$attrs = $r->getAttributes($a);
|
||||
if (!$attrs) return null;
|
||||
return array_shift($attrs)->newInstance();
|
||||
};
|
||||
|
||||
$r = new \ReflectionObject($target);
|
||||
|
||||
$name = get_class($target);
|
||||
$this->classCommands[$name] = [];
|
||||
|
||||
foreach ($r->getMethods() as $method) {
|
||||
$methodRpc = $a($method, Attributes\RpcCallable::class);
|
||||
if ($methodRpc) {
|
||||
$this->register($methodRpc->name, $method->getClosure($target));
|
||||
$this->classCommands[$name][] = $methodRpc->name;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function unregister(string $command): void
|
||||
{
|
||||
if (!array_key_exists($command, $this->commands)) {
|
||||
@@ -40,6 +66,16 @@ class CommandRegistry implements CommandResolverInterface, EventEmitterInterface
|
||||
$this->emit(self::EVENT_UNREGISTERED, [ $command ]);
|
||||
}
|
||||
|
||||
public function unregisterClass(object $target): void
|
||||
{
|
||||
$name = get_class($target);
|
||||
if (!isset($this->classCommands[$name])) return;
|
||||
foreach ($this->classCommands[$name] as $command) {
|
||||
$this->unregister($command);
|
||||
}
|
||||
unset($this->classCommands[$name]);
|
||||
}
|
||||
|
||||
public function findCommand(string $command): ?Command
|
||||
{
|
||||
return $this->commands[$command] ?? null;
|
||||
|
||||
@@ -26,4 +26,14 @@ class CommandTest extends \PHPUnit\Framework\TestCase
|
||||
$this->assertEquals(true, $hit);
|
||||
}
|
||||
|
||||
public function testCommandReflection()
|
||||
{
|
||||
$command = new Command("test", function (string $a, ?int $b, bool $c = false) { });
|
||||
$expect = [
|
||||
'a' => 'string',
|
||||
'b' => '?int',
|
||||
'c' => 'bool=false'
|
||||
];
|
||||
$this->assertEquals($expect, $command->parameters());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user