diff --git a/app/Cfnpp/Compiler.php b/app/Cfnpp/Compiler.php index 2986f66..2c302f1 100644 --- a/app/Cfnpp/Compiler.php +++ b/app/Cfnpp/Compiler.php @@ -41,6 +41,13 @@ class Compiler implements \App\Engine\ICompile */ protected $merge_functions; + /** + * Stores current state of the document so it can be mutated mid-pass. + * + * @var Document + */ + protected $document; + /** * Register a function that can be called by the document. * @@ -66,6 +73,19 @@ class Compiler implements \App\Engine\ICompile $this->merge_functions[$name] = $callback; } + public function addCondition(string $name, Node $node): void + { + $conditions = $this->document->getChildByName('Conditions'); + if (!isset($conditions)) + { + $conditions = new Node($this->document, 'Conditions'); + $this->document->addChild($conditions); + } + + $node->setName($name); + $conditions->addChild($node); + } + /** * Compile a set of documents and return the result. * @@ -81,7 +101,7 @@ class Compiler implements \App\Engine\ICompile } // Initialize state - $document = null; + $this->document = null; $this->functions = []; $this->merge_functions = []; @@ -89,11 +109,11 @@ class Compiler implements \App\Engine\ICompile $cfnpp_functions = new Functions($this, $options); $cfnpp_functions->register($this); - $document = $this->pass_0($documents, $options); - $this->pass_1($document, $options); - $this->pass_2($document, $options); + $this->document = $this->pass_0($documents, $options); + $this->pass_1($this->document, $options); + $this->pass_2($this->document, $options); - return $document; + return $this->document; // Process each passed document /* @@ -207,7 +227,7 @@ class Compiler implements \App\Engine\ICompile { if (isset($nodes[$variable_node->getName()])) { - throw new \Exception("Variables and parameters cannot share the same name."); + throw new \Exception('Variables and parameters cannot share the same name.'); } $nodes[$variable_node->getName()] = $variable_node; $graph->add($variable_node->getName(), $this->pass_1_getDependencies($variable_node)); @@ -230,7 +250,7 @@ class Compiler implements \App\Engine\ICompile $options->setParameter($parameter_node->getName(), Node::toPhp($parameter_node)); } } - else if ($type == 'variables') + elseif ($type == 'variables') { $variable_node = $variables_node->getChildByName($node_name); if (isset($variable_node)) @@ -269,7 +289,7 @@ class Compiler implements \App\Engine\ICompile { $variables[] = $node->getValue(); } - else if ($node instanceof NodeFunctionValue && + elseif ($node instanceof NodeFunctionValue && $node->getName() == 'param') { $variables[] = $node->getValue();