Compare commits
No commits in common. "master" and "0.2.0" have entirely different histories.
@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
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;
|
||||||
@ -19,13 +17,10 @@ class Command
|
|||||||
/** @var callable $handler The handler */
|
/** @var callable $handler The handler */
|
||||||
private $handler;
|
private $handler;
|
||||||
|
|
||||||
private ?array $signature;
|
public function __construct(string $name, callable $handler)
|
||||||
|
|
||||||
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
|
||||||
@ -43,36 +38,5 @@ 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,8 +8,9 @@ use Evenement\EventEmitterTrait;
|
|||||||
* A collection of commands that can be executed via CommandBusInterface
|
* A collection of commands that can be executed via CommandBusInterface
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
trait CommandResolverTrait
|
trait CommandResolverTrait implements EventEmitterInterface
|
||||||
{
|
{
|
||||||
|
use EventEmitterTrait;
|
||||||
|
|
||||||
const EVENT_REGISTERED = 'registered';
|
const EVENT_REGISTERED = 'registered';
|
||||||
const EVENT_UNREGISTERED = 'unregistered';
|
const EVENT_UNREGISTERED = 'unregistered';
|
||||||
|
@ -26,14 +26,4 @@ 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());
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user