Browse Source
We're going to use 'options' to also hold some mutated state, so let's just keep it simplemaster
Adam Pippin
3 years ago
4 changed files with 103 additions and 19 deletions
@ -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; |
|||
} |
|||
} |
Loading…
Reference in new issue