Browse Source

Basic merge compiler

master
Adam Pippin 3 years ago
parent
commit
de8ab2d0d2
  1. 60
      app/Dom/Node.php
  2. 10
      app/Dom/NodeFunctionValue.php
  3. 10
      app/Dom/NodeValue.php
  4. 57
      app/Engine/Cfnpp.php
  5. 9
      app/Engine/CfnppOptions.php
  6. 3
      app/Engine/Engine.php

60
app/Dom/Node.php

@ -97,6 +97,24 @@ class Node implements \ArrayAccess, \Iterator
return $this->children;
}
/**
* Attempt to find a child node by name.
*
* @param string $name
* @return ?Node
*/
public function getChildByName(string $name): ?Node
{
foreach ($this->children as $child)
{
if ($child->name == $name)
{
return $child;
}
}
return null;
}
/**
* Add a node to this node's children.
*
@ -108,6 +126,48 @@ class Node implements \ArrayAccess, \Iterator
$this->children[] = $child;
}
/**
* Remove a child node.
*
* @param Node $child
* @return void
*/
public function removeChild(Node $child): void
{
for ($i = 0; $i < sizeof($this->children); $i++)
{
if ($this->children[$i] === $child)
{
array_splice($this->children, $i, 1);
return;
}
}
}
public function isMap(): bool
{
foreach ($this->children as $child)
{
if (!$child->hasName())
{
return false;
}
}
return true;
}
public function isArray(): bool
{
foreach ($this->children as $child)
{
if ($child->hasName())
{
return false;
}
}
return true;
}
// ArrayAccess
public function offsetExists($offset): bool

10
app/Dom/NodeFunctionValue.php

@ -47,4 +47,14 @@ class NodeFunctionValue extends NodeFunction
{
return $this->value;
}
public function isMap(): bool
{
return false;
}
public function isArray(): bool
{
return false;
}
}

10
app/Dom/NodeValue.php

@ -47,4 +47,14 @@ class NodeValue extends Node
{
return $this->value;
}
public function isMap(): bool
{
return false;
}
public function isArray(): bool
{
return false;
}
}

57
app/Engine/Cfnpp.php

@ -5,6 +5,8 @@ declare(strict_types=1);
namespace App\Engine;
use App\Dom\Document;
use App\Dom\Node;
use App\Dom\NodeValue;
class Cfnpp implements ICompile
{
@ -18,7 +20,60 @@ class Cfnpp implements ICompile
public function compile(Document $document, IOptions $options): void
{
// noop
$this->compileNodes($this->document, $document);
}
protected function compileNodes(Node $original, Node $target): void
{
var_dump(get_class($original), get_class($target));
if ($target instanceof NodeValue)
{
if ($original instanceof NodeValue)
{
$original->setValue($target->getValue());
}
else
{
$parent = $original->getParent();
$parent->removeChild($original);
$parent->addChild($target);
}
}
// TODO: Add nodefunctions
elseif ($target instanceof Node)
{
// if they're both maps, add/overwrite by name
if ($target->isMap() && $original->isMap())
{
foreach ($target as $target_child)
{
$orig_child = $original->getChildByName($target_child->getName());
if (!isset($orig_child))
{
$original->addChild($target_child);
}
else
{
$this->compileNodes($orig_child, $target_child);
}
}
}
// if they're both arrays, append
elseif ($target->isArray() && $original->isArray())
{
foreach ($target as $child)
{
$original->addChild($child);
}
}
// if there's a mismatch, replace
else
{
$parent = $original->getParent();
$parent->removeChild($original);
$parent->addChild($target);
}
}
}
public function getDocument(): Document

9
app/Engine/CfnppOptions.php

@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace App\Engine;
class CfnppOptions implements IOptions
{
}

3
app/Engine/Engine.php

@ -64,7 +64,8 @@ class Engine
public function compileDocument(\App\Dom\Document $document, IOptions $options): Engine
{
return $this->compile->compile($document, $options);
$this->compile->compile($document, $options);
return $this;
}
public function getOutputDocument(): \App\Dom\Document

Loading…
Cancel
Save