Add Command->parameters() method to introspect arguments
This commit is contained in:
		@@ -2,6 +2,8 @@
 | 
			
		||||
 | 
			
		||||
namespace NoccyLabs\React\CommandBus;
 | 
			
		||||
 | 
			
		||||
use ReflectionFunction;
 | 
			
		||||
use ReflectionNamedType;
 | 
			
		||||
use React\Promise\Deferred;
 | 
			
		||||
use React\Promise\Promise;
 | 
			
		||||
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->getName();
 | 
			
		||||
                } else {
 | 
			
		||||
                    $type = null;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            if ($parameter->isOptional()) $type = "?{$type}";
 | 
			
		||||
            if ($type !== null) $args[$name] = $type;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $args;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user