From f3a3d21a36e5b0ca60c6093445e482a011890a9f Mon Sep 17 00:00:00 2001 From: Adam Pippin Date: Sun, 14 Feb 2021 14:20:30 -0800 Subject: [PATCH] Track parameters like variables Not in love with this. Cannot use !functions in variables/parameters block because they're grabbed before being compiled. --- app/Engine/CfnppOptions.php | 68 +++++++++++++++++++++++++++++++++++++ app/Engine/Engine.php | 26 +++++++++++++- app/Engine/IOptions.php | 40 ++++++++++++++++++++++ 3 files changed, 133 insertions(+), 1 deletion(-) diff --git a/app/Engine/CfnppOptions.php b/app/Engine/CfnppOptions.php index 78840ad..ffc825e 100644 --- a/app/Engine/CfnppOptions.php +++ b/app/Engine/CfnppOptions.php @@ -18,9 +18,17 @@ class CfnppOptions implements IOptions */ protected $variables; + /** + * Store the parameters we send to cloudformation. + * + * @var array + */ + protected $parameters; + public function __construct() { $this->variables = []; + $this->parameters = []; } /** @@ -82,4 +90,64 @@ class CfnppOptions implements IOptions { return isset($this->variables[$name]); } + + /** + * Set all parameters. + * + * @param array $parameters + * @return void + */ + public function setParameters(array $parameters): void + { + $this->parameters = $parameters; + } + + /** + * Retrieve all parameters. + * + * @return array + */ + public function getParameters(): array + { + return $this->parameters; + } + + /** + * Retrieve the value of a single parameter. + * + * @param string $name + * @throws \Exception if parameter does not exist + * @return mixed + */ + public function getParameter(string $name) + { + if (!isset($this->parameters[$name])) + { + throw new \Exception('Unset parameter: '.$name); + } + return $this->parameters[$name]; + } + + /** + * Set a single parameter. + * + * @param string $name + * @param mixed $value + * @return void + */ + public function setParameter(string $name, $value): void + { + $this->parameters[$name] = $value; + } + + /** + * Check if a parameter exists. + * + * @param string $name + * @return bool + */ + public function hasParameter(string $name): bool + { + return isset($this->parameters[$name]); + } } diff --git a/app/Engine/Engine.php b/app/Engine/Engine.php index e9151dc..2049276 100644 --- a/app/Engine/Engine.php +++ b/app/Engine/Engine.php @@ -44,6 +44,7 @@ class Engine * Process a domdocument, applying some higher level operations such as * stacking documents. * + * @todo break all this stuff out * @param string $input_file * @param IOptions $options * @return string @@ -78,8 +79,9 @@ class Engine } } - // Go through and load all the variables + // Go through and load all the variables and parameters $variables = []; + $parameters = []; for ($i = 0; $i < sizeof($files); $i++) { $file_variables = []; @@ -95,6 +97,20 @@ class Engine { $variables[$k] = $v; } + + $file_parameters = []; + try + { + $file_parameters = $documents[$files[$i]]->getMeta('parameters'); + } + catch (\Exception $ex) + { + } + + foreach ($file_parameters as $k => $v) + { + $parameters[$k] = $v; + } } foreach ($variables as $k => $v) @@ -105,6 +121,14 @@ class Engine } } + foreach ($parameters as $k => $v) + { + if (!$options->hasParameter($k)) + { + $options->setParameter($k, $v); + } + } + // Now actually process all the documents for ($i = 0; $i < sizeof($files); $i++) { diff --git a/app/Engine/IOptions.php b/app/Engine/IOptions.php index 74bb1e7..b6d8ce3 100644 --- a/app/Engine/IOptions.php +++ b/app/Engine/IOptions.php @@ -50,4 +50,44 @@ interface IOptions * @return bool */ public function hasVariable(string $name): bool; + + /** + * Set parameters to send to cloudformation. + * + * @param array $parameters + * @return void + */ + public function setParameters(array $parameters): void; + + /** + * Retrieve all parameters set. + * + * @return array + */ + public function getParameters(): array; + + /** + * Retrieve the value of a single parameter. + * + * @param string $name + * @return mixed + */ + public function getParameter(string $name); + + /** + * Set a single parameter. + * + * @param string $name + * @param mixed $value + * @return void + */ + public function setParameter(string $name, $value): void; + + /** + * Check if a parameter exists. + * + * @param string $name + * @return bool + */ + public function hasParameter(string $name): bool; }