Browse Source

Added some simple WIP document-level stuff and basic compile command

master
Adam Pippin 3 years ago
parent
commit
7aa447f9d9
  1. 68
      app/Commands/Stack/Compile.php
  2. 22
      app/Dom/Document.php
  3. 29
      app/Dom/Node.php

68
app/Commands/Stack/Compile.php

@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
namespace App\Commands\Stack;
use LaravelZero\Framework\Commands\Command;
class Compile extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'stack:compile {in_file} {out_file} {--format=Yaml}';
/**
* The description of the command.
*
* @var string
*/
protected $description = 'Read an input file, process, output result to a file';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$engine = new \App\Engine\Engine();
$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));
$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());
}
protected function getSerializer(): \App\Serialize\IUnserialize
{
$format = $this->option('format');
$class = '\\App\\Serialize\\'.$format;
if (!class_exists($class))
{
throw new \Exception('Unknown formatter: '.$format);
}
return new $class();
}
}

22
app/Dom/Document.php

@ -15,4 +15,26 @@ class Document extends Node
{
$this->children = [];
}
public function getMeta(string $path)
{
$node = $this->getChildByPath('cfnpp.'.$path);
if (!isset($node))
{
throw new \Exception('Path not found: '.$path);
}
if ($node instanceof NodeValue)
{
return $node->getValue();
}
elseif ($node->isArray())
{
$values = [];
foreach ($node as $child)
{
$values[] = $child->getValue();
}
return $values;
}
}
}

29
app/Dom/Node.php

@ -179,6 +179,35 @@ class Node implements \ArrayAccess, \Iterator
return true;
}
public function getChildByPath(string $path): ?Node
{
$path = explode('.', $path);
return $this->findChild($path, $this);
}
protected function findChild(array $path, Node $current): ?Node
{
$next = array_shift($path);
if (is_numeric($next))
{
$next = $current[$next];
}
else
{
$next = $current->getChildByName($next);
}
if (empty($path))
{
return $next;
}
if (!isset($next))
{
return null;
}
return $this->findChild($path, $next);
}
// ArrayAccess
public function offsetExists($offset): bool

Loading…
Cancel
Save