cloudformation-plus-plus: cfn template preprocessor
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

116 lines
2.6 KiB

<?php
declare(strict_types=1);
namespace App\Engine;
/**
* Main class responsible for controlling the compilation process.
*
* @author Adam Pippin <hello@adampippin.ca>
*/
class Engine
{
/** @var \App\Engine\ICompile */
protected $compile;
/** @var \App\Serialize\ISerialize */
protected $serialize;
/** @var \App\Serialize\IUnserialize */
protected $unserialize;
/** @var \App\Dom\Document */
protected $document;
public function process(string $input_file, IOptions $options): string
{
$file_helper = new File([dirname(realpath($input_file))]);
$files[] = $input_file;
$processed = [];
for ($i = 0; $i < sizeof($files); $i++)
{
$document = $this->unserialize->unserialize(file_get_contents($file_helper->resolve($files[$i])));
if (!in_array($files[$i], $processed))
{ // only process includes once so when we hit the stack again we don't loop forever
$additional_files = [];
try
{
$additional_files = $document->getMeta('stack');
}
catch (\Exception $ex)
{
}
if (sizeof($additional_files))
{
$processed[] = $files[$i];
array_splice($files, $i, 0, $additional_files);
$i -= sizeof($additional_files);
continue;
}
}
echo '=> '.$files[$i].PHP_EOL;
$this->compileDocument($document, $options);
}
return $this->getOutput();
}
public function setCompiler(ICompile $compiler): Engine
{
$this->compile = $compiler;
return $this;
}
public function setSerializer(\App\Serialize\ISerialize $serializer): Engine
{
$this->serialize = $serializer;
return $this;
}
public function setUnserializer(\App\Serialize\IUnserialize $unserializer): Engine
{
$this->unserialize = $unserializer;
return $this;
}
public function setInputDocument(\App\Dom\Document $document): Engine
{
$this->document = $document;
$this->compile->setDocument($document);
return $this;
}
public function setInput(string $document_string): Engine
{
$document = $this->unserialize->unserialize($document_string);
$this->setInputDocument($document);
return $this;
}
public function compile(string $document_string, IOptions $options): Engine
{
$document = $this->unserialize->unserialize($document_string);
$this->compileDocument($document, $options);
return $this;
}
public function compileDocument(\App\Dom\Document $document, IOptions $options): Engine
{
$this->compile->compile($document, $options);
return $this;
}
public function getOutputDocument(): \App\Dom\Document
{
return $this->compile->getDocument();
}
public function getOutput(): string
{
$document = $this->getOutputDocument();
return $this->serialize->serialize($document);
}
}