Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0bca797e8d | ||
|
|
474ccbb012 | ||
|
|
bf8e95561e |
No files matched your search
@@ -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;
|
||||||
@@ -38,5 +40,31 @@ 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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -8,9 +8,8 @@ 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 implements EventEmitterInterface
|
trait CommandResolverTrait
|
||||||
{
|
{
|
||||||
use EventEmitterTrait;
|
|
||||||
|
|
||||||
const EVENT_REGISTERED = 'registered';
|
const EVENT_REGISTERED = 'registered';
|
||||||
const EVENT_UNREGISTERED = 'unregistered';
|
const EVENT_UNREGISTERED = 'unregistered';
|
||||||
|
|||||||
+11
-1
@@ -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'
|
||||||
|
];
|
||||||
|
$this->assertEquals($expect, $command->parameters());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user