Browse Source

Track parameters like variables

Not in love with this. Cannot use !functions in variables/parameters block because they're grabbed before being compiled.
master
Adam Pippin 3 years ago
parent
commit
f3a3d21a36
  1. 68
      app/Engine/CfnppOptions.php
  2. 26
      app/Engine/Engine.php
  3. 40
      app/Engine/IOptions.php

68
app/Engine/CfnppOptions.php

@ -18,9 +18,17 @@ class CfnppOptions implements IOptions
*/ */
protected $variables; protected $variables;
/**
* Store the parameters we send to cloudformation.
*
* @var array<string,mixed>
*/
protected $parameters;
public function __construct() public function __construct()
{ {
$this->variables = []; $this->variables = [];
$this->parameters = [];
} }
/** /**
@ -82,4 +90,64 @@ class CfnppOptions implements IOptions
{ {
return isset($this->variables[$name]); return isset($this->variables[$name]);
} }
/**
* Set all parameters.
*
* @param array<string,mixed> $parameters
* @return void
*/
public function setParameters(array $parameters): void
{
$this->parameters = $parameters;
}
/**
* Retrieve all parameters.
*
* @return array<string,mixed>
*/
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]);
}
} }

26
app/Engine/Engine.php

@ -44,6 +44,7 @@ class Engine
* Process a domdocument, applying some higher level operations such as * Process a domdocument, applying some higher level operations such as
* stacking documents. * stacking documents.
* *
* @todo break all this stuff out
* @param string $input_file * @param string $input_file
* @param IOptions $options * @param IOptions $options
* @return string * @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 = []; $variables = [];
$parameters = [];
for ($i = 0; $i < sizeof($files); $i++) for ($i = 0; $i < sizeof($files); $i++)
{ {
$file_variables = []; $file_variables = [];
@ -95,6 +97,20 @@ class Engine
{ {
$variables[$k] = $v; $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) 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 // Now actually process all the documents
for ($i = 0; $i < sizeof($files); $i++) for ($i = 0; $i < sizeof($files); $i++)
{ {

40
app/Engine/IOptions.php

@ -50,4 +50,44 @@ interface IOptions
* @return bool * @return bool
*/ */
public function hasVariable(string $name): bool; public function hasVariable(string $name): bool;
/**
* Set parameters to send to cloudformation.
*
* @param array<string, mixed> $parameters
* @return void
*/
public function setParameters(array $parameters): void;
/**
* Retrieve all parameters set.
*
* @return array<string,mixed>
*/
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;
} }

Loading…
Cancel
Save