Compare commits
No commits in common. "master" and "0.2.0.1" have entirely different histories.
@ -2,8 +2,6 @@
|
||||
|
||||
namespace NoccyLabs\React\CommandBus;
|
||||
|
||||
use ReflectionFunction;
|
||||
use ReflectionNamedType;
|
||||
use React\Promise\Deferred;
|
||||
use React\Promise\Promise;
|
||||
use React\Promise\PromiseInterface;
|
||||
@ -19,13 +17,10 @@ class Command
|
||||
/** @var callable $handler The handler */
|
||||
private $handler;
|
||||
|
||||
private ?array $signature;
|
||||
|
||||
public function __construct(string $name, callable $handler, ?array $signature = null)
|
||||
public function __construct(string $name, callable $handler)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->handler = $handler;
|
||||
$this->signature = $signature;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -26,14 +26,4 @@ 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());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user