Browse Source

Moved basic compilation/include logic into Engine (needs cleanup)

master
Adam Pippin 3 years ago
parent
commit
5538661bca
  1. 18
      app/Commands/Stack/Compile.php
  2. 48
      app/Engine/Engine.php
  3. 33
      app/Engine/File.php

18
app/Commands/Stack/Compile.php

@ -34,23 +34,9 @@ class Compile extends Command
$serializer = $this->getSerializer();
$engine->setSerializer($serializer)->setUnserializer($serializer)->setCompiler(new \App\Engine\Cfnpp());
$start_file = $this->argument('in_file');
$start_document = $serializer->unserialize(file_get_contents($start_file));
$output = $engine->process($this->argument('in_file'), new \App\Engine\CfnppOptions());
$files = $start_document->getMeta('stack');
$files[] = $start_file;
$base_path = realpath(dirname($start_file));
$top_file = array_shift($files);
$this->info($top_file);
$engine->setInput(file_get_contents($base_path.DIRECTORY_SEPARATOR.$top_file));
foreach ($files as $file)
{
$this->info($file);
$engine->compile(file_get_contents($base_path.DIRECTORY_SEPARATOR.$file), new \App\Engine\CfnppOptions());
}
file_put_contents($this->argument('out_file'), $engine->getOutput());
file_put_contents($this->argument('out_file'), $output);
}
protected function getSerializer(): \App\Serialize\IUnserialize

48
app/Engine/Engine.php

@ -23,6 +23,54 @@ class Engine
/** @var \App\Dom\Document */
protected $document;
public function process(string $input_file, IOptions $options): string
{
$file_helper = new File([dirname(realpath($input_file))]);
$input_document = $this->unserialize->unserialize(file_get_contents($file_helper->resolve($input_file)));
$files = $input_document->getMeta('stack');
$files[] = $input_file;
$processed = [$input_file];
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;
if ($i == 0)
{
// Initial document load
$this->setInputDocument($document);
}
else
{
// Transform
$this->compileDocument($document, $options);
}
}
return $this->getOutput();
}
public function setCompiler(ICompile $compiler): Engine
{
$this->compile = $compiler;

33
app/Engine/File.php

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace App\Engine;
class File
{
/** @var string */
protected $paths;
public function __construct(array $paths)
{
$this->paths = $paths;
}
public function resolve(string $file)
{
foreach ($this->paths as $path)
{
if (file_exists($path.DIRECTORY_SEPARATOR.$file))
{
return realpath($path.DIRECTORY_SEPARATOR.$file);
}
}
throw new \Exception('File not found: '.$file);
}
public function addPath(string $path)
{
$this->paths[] = $path;
}
}
Loading…
Cancel
Save