7 Commits
Author SHA1 Message Date
Christopher Vagnetoft 4790ab9144 Update dependency contrains
* Support 8.x for symfony/uid
2026-11-30 01:06:04 +01:00
noccy 070f607187 Add support for registering classes with CommandRegistry
* Calling registerClass() on CommandRegistry will result in all
  methods having the #[RpcCallable("command-here")] attribute
  being registered as commands.
* Commands can be unregistered by calling unregisterClass().
2025-04-07 00:06:55 +02:00
noccy 6671ec3eac Allow providing manual signature hint to commands 2025-01-18 21:58:51 +01:00
noccy 0bca797e8d Improve command signature logic 2025-01-18 14:23:46 +01:00
noccy 474ccbb012 Add Command->parameters() method to introspect arguments 2025-01-17 01:05:58 +01:00
noccy bf8e95561e Fix a wtf in CommandResolverTrait 2025-01-02 13:47:09 +01:00
noccy 9ab9561270 Refactor names in CommandResolverInterface 2024-12-28 15:35:41 +01:00
11 changed files with 169 additions and 17 deletions

No files matched your search

+1 -1
View File
@@ -20,7 +20,7 @@
"react/socket": "^1.15", "react/socket": "^1.15",
"react/stream": "^1.3", "react/stream": "^1.3",
"react/promise-timer": "^1.10", "react/promise-timer": "^1.10",
"symfony/uid": "^6.0|^7.0" "symfony/uid": "^6.0|^7.0|^8.0"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^11.0", "phpunit/phpunit": "^11.0",
+13
View File
@@ -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
View File
@@ -2,6 +2,8 @@
namespace NoccyLabs\React\CommandBus; namespace NoccyLabs\React\CommandBus;
use ReflectionFunction;
use ReflectionNamedType;
use React\Promise\Deferred; use React\Promise\Deferred;
use React\Promise\Promise; use React\Promise\Promise;
use React\Promise\PromiseInterface; use React\Promise\PromiseInterface;
@@ -17,10 +19,13 @@ class Command
/** @var callable $handler The handler */ /** @var callable $handler The handler */
private $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->name = $name;
$this->handler = $handler; $this->handler = $handler;
$this->signature = $signature;
} }
public function getName(): string 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();
}
} }
+8 -3
View File
@@ -51,6 +51,11 @@ class CommandBus implements CommandBusInterface
$this->resolvers[] = $resolver; $this->resolvers[] = $resolver;
} }
public function removeResolver(CommandResolverInterface $resolver): void
{
// FIXME implement
}
/** /**
* Get the names of defined commands * Get the names of defined commands
* *
@@ -60,7 +65,7 @@ class CommandBus implements CommandBusInterface
{ {
$commands = []; $commands = [];
foreach ($this->resolvers as $resolver) { foreach ($this->resolvers as $resolver) {
$commands = array_merge($commands, $resolver->getNames()); $commands = array_merge($commands, $resolver->getCommandNames());
} }
sort($commands); sort($commands);
return array_unique($commands); return array_unique($commands);
@@ -75,7 +80,7 @@ class CommandBus implements CommandBusInterface
public function findCommand(string $command): ?Command public function findCommand(string $command): ?Command
{ {
foreach ($this->resolvers as $resolver) { foreach ($this->resolvers as $resolver) {
if ($found = $resolver->find($command)) if ($found = $resolver->findCommand($command))
return $found; return $found;
} }
return null; return null;
@@ -165,7 +170,7 @@ class CommandBus implements CommandBusInterface
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function execute(string $command, array $context): PromiseInterface public function execute(string $command, array $context = []): PromiseInterface
{ {
$context = new Context($command, $context); $context = new Context($command, $context);
return $this->executeContext($context); return $this->executeContext($context);
+1 -1
View File
@@ -161,7 +161,7 @@ class CommandBusClient implements CommandBusInterface
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function execute(string $command, array $context): PromiseInterface public function execute(string $command, array $context = []): PromiseInterface
{ {
if (!$this->connection || !$this->connection->isWritable()) { if (!$this->connection || !$this->connection->isWritable()) {
$this->reconnect(); $this->reconnect();
+1 -1
View File
@@ -17,7 +17,7 @@ interface CommandBusInterface extends EventEmitterInterface
* @param array $context Data to pass in the call context * @param array $context Data to pass in the call context
* @®eturn PromiseInterface A promise that will be resolved with the result * @®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 * Notify all connected clients to emit the notify event, and also be
+39 -3
View File
@@ -1,6 +1,7 @@
<?php <?php
namespace NoccyLabs\React\CommandBus; namespace NoccyLabs\React\CommandBus;
use Evenement\EventEmitterInterface; use Evenement\EventEmitterInterface;
use Evenement\EventEmitterTrait; use Evenement\EventEmitterTrait;
@@ -15,8 +16,10 @@ class CommandRegistry implements CommandResolverInterface, EventEmitterInterface
const EVENT_REGISTERED = 'registered'; const EVENT_REGISTERED = 'registered';
const EVENT_UNREGISTERED = 'unregistered'; const EVENT_UNREGISTERED = 'unregistered';
/** @var array<string,Command> */ /** @var array<string,Command> The registered commands and callbacks */
private array $commands = []; 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 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 public function unregister(string $command): void
{ {
if (!array_key_exists($command, $this->commands)) { if (!array_key_exists($command, $this->commands)) {
@@ -40,12 +66,22 @@ class CommandRegistry implements CommandResolverInterface, EventEmitterInterface
$this->emit(self::EVENT_UNREGISTERED, [ $command ]); $this->emit(self::EVENT_UNREGISTERED, [ $command ]);
} }
public function find(string $command): ?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; return $this->commands[$command] ?? null;
} }
public function getNames(): array public function getCommandNames(): array
{ {
return array_keys($this->commands); return array_keys($this->commands);
} }
+2 -2
View File
@@ -11,9 +11,9 @@ use Evenement\EventEmitterTrait;
interface CommandResolverInterface interface CommandResolverInterface
{ {
public function find(string $command): ?Command; public function findCommand(string $command): ?Command;
public function getNames(): array; public function getCommandNames(): array;
} }
+52
View File
@@ -0,0 +1,52 @@
<?php
namespace NoccyLabs\React\CommandBus;
use Evenement\EventEmitterInterface;
use Evenement\EventEmitterTrait;
/**
* A collection of commands that can be executed via CommandBusInterface
*
*/
trait CommandResolverTrait
{
const EVENT_REGISTERED = 'registered';
const EVENT_UNREGISTERED = 'unregistered';
/** @var array<string,Command> */
private array $commands = [];
public function registerCommand(string $command, callable $handler): void
{
$isNew = !array_key_exists($command, $this->commands);
$this->commands[$command] = new Command($command, $handler);
if ($isNew) {
$this->emit(self::EVENT_REGISTERED, [ $command ]);
}
}
public function unregisterCommand(string $command): void
{
if (!array_key_exists($command, $this->commands)) {
return;
}
unset($this->commands[$command]);
$this->emit(self::EVENT_UNREGISTERED, [ $command ]);
}
public function findCommand(string $command): ?Command
{
return $this->commands[$command] ?? null;
}
public function getCommandNames(): array
{
return array_keys($this->commands);
}
}
+4 -4
View File
@@ -48,8 +48,8 @@ class CommandRegistryTest extends \PHPUnit\Framework\TestCase
$reg->register('a', $cmda); $reg->register('a', $cmda);
$reg->register('b', $cmdb); $reg->register('b', $cmdb);
$this->assertEquals('a', $reg->find('a')?->getName()); $this->assertEquals('a', $reg->findCommand('a')?->getName());
$this->assertEquals('b', $reg->find('b')?->getName()); $this->assertEquals('b', $reg->findCommand('b')?->getName());
} }
public function testGettingCommandNames() public function testGettingCommandNames()
@@ -61,7 +61,7 @@ class CommandRegistryTest extends \PHPUnit\Framework\TestCase
$reg->register('a', $cmda); $reg->register('a', $cmda);
$reg->register('b', $cmdb); $reg->register('b', $cmdb);
$this->assertEquals(['a','b'], $reg->getNames()); $this->assertEquals(['a','b'], $reg->getCommandNames());
} }
} }
+11 -1
View File
@@ -26,4 +26,14 @@ class CommandTest extends \PHPUnit\Framework\TestCase
$this->assertEquals(true, $hit); $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());
}
}