Updated Invoke instruction to use Callable
instead of a combination of namespace
& method
This commit is contained in:
parent
be3875692c
commit
98744973b2
2 changed files with 37 additions and 49 deletions
|
@ -4,8 +4,7 @@ Invokes a method under a namespace.
|
||||||
|
|
||||||
## Parameters
|
## Parameters
|
||||||
|
|
||||||
* namespace (`string`) - The namespace to invoke the method under.
|
* callable (`string`) - The callable to invoke. (e.g. `namespace.method`, `system.print`, `time.now`)
|
||||||
* method (`string`) - The method to invoke.
|
|
||||||
* parameters (`array`) - The parameters to pass to the method.
|
* parameters (`array`) - The parameters to pass to the method.
|
||||||
* fail_on_error (`boolean`) - Whether to fail if the method throws an exception.
|
* fail_on_error (`boolean`) - Whether to fail if the method throws an exception.
|
||||||
|
|
||||||
|
@ -26,8 +25,7 @@ Invokes a method under a namespace.
|
||||||
{
|
{
|
||||||
"type": "invoke",
|
"type": "invoke",
|
||||||
"_": {
|
"_": {
|
||||||
"namespace": "system",
|
"callable": "system.print",
|
||||||
"method": "print",
|
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"value": "Hello, world!"
|
"value": "Hello, world!"
|
||||||
},
|
},
|
||||||
|
@ -38,5 +36,5 @@ Invokes a method under a namespace.
|
||||||
|
|
||||||
### Last Updated
|
### Last Updated
|
||||||
|
|
||||||
Monday, December 29th, 2022.
|
Monday, December 30th, 2022.
|
||||||
Written by [Netkas](https://git.n64.cc/netkas)
|
Written by [Netkas](https://git.n64.cc/netkas)
|
|
@ -17,18 +17,11 @@
|
||||||
class Invoke implements InstructionInterface
|
class Invoke implements InstructionInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* The namespace of the method to invoke
|
* The name of the namespace & method to invoke
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $Namespace;
|
private $Callable;
|
||||||
|
|
||||||
/**
|
|
||||||
* The name of the method to invoke
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private $Method;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The parameters to pass to the method
|
* The parameters to pass to the method
|
||||||
|
@ -66,10 +59,9 @@
|
||||||
public function toArray(): array
|
public function toArray(): array
|
||||||
{
|
{
|
||||||
return InstructionBuilder::toRaw(self::getType(), [
|
return InstructionBuilder::toRaw(self::getType(), [
|
||||||
'namespace' => $this->Namespace,
|
'callable' => $this->Callable,
|
||||||
'method' => $this->Method,
|
|
||||||
'parameters' => $this->Parameters,
|
'parameters' => $this->Parameters,
|
||||||
'fail_on_error' => $this->FailOnError, // TODO: Implement this
|
'fail_on_error' => $this->FailOnError
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,8 +75,7 @@
|
||||||
public static function fromArray(array $data): InstructionInterface
|
public static function fromArray(array $data): InstructionInterface
|
||||||
{
|
{
|
||||||
$instruction = new self();
|
$instruction = new self();
|
||||||
$instruction->setNamespace($data['namespace'] ?? null);
|
$instruction->setCallable($data['callable'] ?? null);
|
||||||
$instruction->setMethod($data['method'] ?? null);
|
|
||||||
$instruction->setParameters($data['parameters'] ?? []);
|
$instruction->setParameters($data['parameters'] ?? []);
|
||||||
$instruction->setFailOnError($data['fail_on_error'] ?? false);
|
$instruction->setFailOnError($data['fail_on_error'] ?? false);
|
||||||
|
|
||||||
|
@ -102,16 +93,20 @@
|
||||||
*/
|
*/
|
||||||
public function eval(Engine $engine): mixed
|
public function eval(Engine $engine): mixed
|
||||||
{
|
{
|
||||||
$namespace = $engine->eval($this->Namespace);
|
$callable = $engine->eval($this->Callable);
|
||||||
$method = $engine->eval($this->Method);
|
|
||||||
$parameters = [];
|
$parameters = [];
|
||||||
foreach($this->Parameters as $key => $value)
|
foreach($this->Parameters as $key => $value)
|
||||||
$parameters[$key] = $engine->eval($value);
|
$parameters[$key] = $engine->eval($value);
|
||||||
|
|
||||||
if(!is_string($namespace))
|
if(!is_string($callable))
|
||||||
throw new TypeException(sprintf('The namespace must be a string, %s given', Utilities::getType($namespace, true)));
|
throw new TypeException(sprintf('Callable must be a string, %s given', Utilities::getType($callable, true)));
|
||||||
if(!is_string($method))
|
|
||||||
throw new TypeException(sprintf('The method must be a string, %s given', Utilities::getType($method, true)));
|
$callable = explode('.', $callable);
|
||||||
|
if(count($callable) !== 2)
|
||||||
|
throw new ImportException(sprintf('Callable must be in the format of "namespace.method", %s given', $this->Callable));
|
||||||
|
|
||||||
|
$namespace = $callable[0];
|
||||||
|
$method = $callable[1];
|
||||||
|
|
||||||
return $engine->callMethod($namespace, $method, $parameters);
|
return $engine->callMethod($namespace, $method, $parameters);
|
||||||
}
|
}
|
||||||
|
@ -120,34 +115,17 @@
|
||||||
* @return string
|
* @return string
|
||||||
* @noinspection PhpUnused
|
* @noinspection PhpUnused
|
||||||
*/
|
*/
|
||||||
public function getNamespace(): string
|
public function getCallable(): string
|
||||||
{
|
{
|
||||||
return $this->Namespace;
|
return $this->Callable;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $Namespace
|
* @param string $Callable
|
||||||
*/
|
*/
|
||||||
public function setNamespace(string $Namespace): void
|
public function setCallable(string $Callable): void
|
||||||
{
|
{
|
||||||
$this->Namespace = $Namespace;
|
$this->Callable = $Callable;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
* @noinspection PhpUnused
|
|
||||||
*/
|
|
||||||
public function getMethod(): string
|
|
||||||
{
|
|
||||||
return $this->Method;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $Method
|
|
||||||
*/
|
|
||||||
public function setMethod(string $Method): void
|
|
||||||
{
|
|
||||||
$this->Method = $Method;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -194,9 +172,21 @@
|
||||||
foreach ($this->Parameters as $key => $value)
|
foreach ($this->Parameters as $key => $value)
|
||||||
$parameters[] = $key . ': ' . Utilities::entityToString($value);
|
$parameters[] = $key . ': ' . Utilities::entityToString($value);
|
||||||
|
|
||||||
|
$callable = explode('.', Utilities::entityToString($this->Callable));
|
||||||
|
if(count($callable) !== 2)
|
||||||
|
{
|
||||||
|
$namespace = 'unknown';
|
||||||
|
$method = 'unknown';
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$namespace = $callable[0];
|
||||||
|
$method = $callable[1];
|
||||||
|
}
|
||||||
|
|
||||||
$results = sprintf(
|
$results = sprintf(
|
||||||
self::getType() . ' %s::%s(%s)',
|
self::getType() . ' %s.%s(%s)',
|
||||||
$this->Namespace, $this->Method, implode(', ', $parameters)
|
$namespace, $method, implode(', ', $parameters)
|
||||||
);
|
);
|
||||||
|
|
||||||
if(!$this->FailOnError)
|
if(!$this->FailOnError)
|
||||||
|
|
Loading…
Add table
Reference in a new issue