Browse Source

Pass options in to compiler at construction instead of with each pass

We're going to use 'options' to also hold some mutated state, so let's just keep it simple
master
Adam Pippin 3 years ago
parent
commit
de247299cd
  1. 5
      app/Commands/Stack/Compile.php
  2. 19
      app/Engine/Cfnpp.php
  3. 95
      app/Engine/CfnppFunctions.php
  4. 3
      app/Engine/ICompile.php

5
app/Commands/Stack/Compile.php

@ -39,9 +39,10 @@ class Compile extends Command
$serializer = $this->getSerializer();
$unserializer = $this->getUnserializer();
$engine->setSerializer($serializer)->setUnserializer($unserializer)->setCompiler(new \App\Engine\Cfnpp());
$options = null;
$engine->setSerializer($serializer)->setUnserializer($unserializer)->setCompiler(new \App\Engine\Cfnpp($options = new \App\Engine\CfnppOptions()));
$output = $engine->process($this->argument('in_file'), new \App\Engine\CfnppOptions());
$output = $engine->process($this->argument('in_file'), $options);
file_put_contents($this->argument('out_file'), $output);
}

19
app/Engine/Cfnpp.php

@ -44,23 +44,13 @@ class Cfnpp implements ICompile
*/
protected $merge_functions;
public function __construct()
public function __construct(IOptions $options)
{
$this->document = new Document();
$this->functions = [];
// Suppress, we'll move these out of here later and clean them up.
// @phan-suppress-next-line PhanPluginUnknownClosureReturnType
$this->registerMergeFunction('Replace', static function(Node $orig_p, Node $tgt_p, Node $t) {
// todo: nodefunctionvalue
$repl = new Node(null, $tgt_p->hasName() ? $tgt_p->getName() : null);
$repl->setChildren($t->getChildren());
return $repl;
});
// @phan-suppress-next-line PhanPluginUnknownClosureReturnType
$this->registerFunction('Unset', static function(Node $node, Node $func) {
});
$cfnpp_functions = new CfnppFunctions($this, $options);
$cfnpp_functions->register($this);
}
/**
@ -113,10 +103,9 @@ class Cfnpp implements ICompile
* Apply a new document to the current state.
*
* @param Document $document
* @param IOptions $options
* @return void
*/
public function compile(Document $document, IOptions $options): void
public function compile(Document $document): void
{
$this->runMergeFunctions($this->document, $document);
$this->merge($this->document, $document);

95
app/Engine/CfnppFunctions.php

@ -0,0 +1,95 @@
<?php
declare(strict_types=1);
namespace App\Engine;
use App\Dom\Document;
use App\Dom\Node;
use App\Dom\NodeFunction;
use App\Dom\NodeFunctionValue;
/**
* Functions available in a cfnpp document.
*
* @author Adam Pippin <hello@adampippin.ca>
*/
class CfnppFunctions
{
/**
* cfnpp compiler.
* @var Cfnpp
*/
protected $compiler;
/**
* cfnpp compiler options, stores state.
* @var CfnppOptions
*/
protected $options;
public function __construct(Cfnpp $compiler, CfnppOptions $options)
{
}
/**
* Examines this class with reflection and registers all functions on a cfnpp
* compiler instance.
*
* @param Cfnpp $compiler
* @return void
*/
public function register(Cfnpp $compiler): void
{
$reflection = new \ReflectionClass(static::class);
$methods = $reflection->getMethods();
foreach ($methods as $method)
{
if (!stristr($method->name, '_'))
{
continue;
}
[$method_type, $method_name] = explode('_', $method->name, 2);
switch ($method_type)
{
case 'mf':
$compiler->registerMergeFunction($method_name, [$this, $method->name]);
break;
case 'f':
$compiler->registerFunction($method_name, [$this, $method->name]);
break;
}
}
}
/**
* Replace all children nodes in original with those from target, rather than
* merging them.
*
* @param Node $original
* @param Node $target
* @param NodeFunction $function
* @return ?Node
*/
public function mf_replace(Node $original, Node $target, NodeFunction $function): ?Node
{
// TODO: Deal with nodefunctionvalue
$replacement = new Node(null, $target->hasName() ? $target->getName() : null);
$replacement->setChildren($function->getChildren());
return $replacement;
}
/**
* Unset a node, completely removing it from the document.
*
* @param Node $node
* @param NodeFunction $function
* @return ?Node
*/
public function f_unset(Node $node, NodeFunction $function): ?Node
{
return null;
}
}

3
app/Engine/ICompile.php

@ -24,10 +24,9 @@ interface ICompile
* using the one passed in as instructions.
*
* @param \App\Dom\Document $document document to compile
* @param IOptions $options options to control compilation process
* @return void
*/
public function compile(\App\Dom\Document $document, IOptions $options): void;
public function compile(\App\Dom\Document $document): void;
/**
* Retrieve the current result.

Loading…
Cancel
Save