|
|
@ -46,14 +46,22 @@ class Expression |
|
|
|
* Expression is tokenized, parsed, and solved on creation. |
|
|
|
* |
|
|
|
* @param string $expression |
|
|
|
* @param \App\Engine\IOptions $options |
|
|
|
*/ |
|
|
|
public function __construct(string $expression, \App\Engine\IOptions $options) |
|
|
|
public function __construct(string $expression) |
|
|
|
{ |
|
|
|
$tokens = static::tokenize($expression); |
|
|
|
$nodes = static::parse($tokens); |
|
|
|
$this->nodes = static::solve($nodes, $options->getVariables(), array_keys($options->getParameters())); |
|
|
|
$this->options = $options; |
|
|
|
$this->nodes = static::parse($tokens); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Solve this expression down to the minimal set of nodes we can |
|
|
|
* |
|
|
|
* @param \App\Engine\IOptions $options |
|
|
|
* @return void |
|
|
|
*/ |
|
|
|
public function solve(\App\Engine\IOptions $options): void |
|
|
|
{ |
|
|
|
$this->nodes = static::_solve($this->nodes, $options->getVariables(), array_keys($options->getParameters())); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -128,6 +136,27 @@ class Expression |
|
|
|
return static::cloudformation($this->nodes); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Examine the expression to determine which variables are referenced so we |
|
|
|
* can figure out the dependencies between them |
|
|
|
* |
|
|
|
* @return string[] names of variables referenced |
|
|
|
*/ |
|
|
|
public function getReferencedVariables(): array |
|
|
|
{ |
|
|
|
$variables = []; |
|
|
|
foreach ($this->nodes as $node) |
|
|
|
{ |
|
|
|
$node->walk(function(GraphNode $node) use (&$variables): void { |
|
|
|
if ($node->getValue() instanceof Token\Variable) |
|
|
|
{ |
|
|
|
$variables[] = $node->getValue()->getName(); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
return array_values(array_unique($variables)); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Convert an expression string into a series of tokens. |
|
|
|
* |
|
|
@ -203,7 +232,7 @@ class Expression |
|
|
|
* @param string[] $parameters parameter names |
|
|
|
* @return GraphNode[] |
|
|
|
*/ |
|
|
|
protected static function solve(array $nodes, array $variables = [], array $parameters = []): array |
|
|
|
protected static function _solve(array $nodes, array $variables = [], array $parameters = []): array |
|
|
|
{ |
|
|
|
$root = new GraphNode(); |
|
|
|
foreach ($nodes as $node) |
|
|
|