Skip to content

Commit

Permalink
uses AuxiliaryNode to hide usage of $this [Closes #347]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 31, 2023
1 parent 794f252 commit bdb37ec
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
14 changes: 6 additions & 8 deletions src/Latte/Essential/Passes.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@

use Latte;
use Latte\CompileException;
use Latte\Compiler\ExpressionBuilder;
use Latte\Compiler\Node;
use Latte\Compiler\Nodes\AuxiliaryNode;
use Latte\Compiler\Nodes\Php\Expression\FunctionCallableNode;
use Latte\Compiler\Nodes\Php\Expression\FunctionCallNode;
use Latte\Compiler\Nodes\Php\Expression;
use Latte\Compiler\Nodes\Php\Expression\VariableNode;
use Latte\Compiler\Nodes\Php\NameNode;
use Latte\Compiler\Nodes\TemplateNode;
Expand Down Expand Up @@ -82,18 +80,18 @@ public static function customFunctionsPass(TemplateNode $node, array $functions)
$names = array_combine(array_map('strtolower', $names), $names);

(new NodeTraverser)->traverse($node, function (Node $node) use ($names) {
if (($node instanceof FunctionCallNode || $node instanceof FunctionCallableNode)
if (($node instanceof Expression\FunctionCallNode || $node instanceof Expression\FunctionCallableNode)
&& $node->name instanceof NameNode
&& ($orig = $names[strtolower((string) $node->name)] ?? null)
) {
if ((string) $node->name !== $orig) {
trigger_error("Case mismatch on function name '{$node->name}', correct name is '$orig'.", E_USER_WARNING);
}

return ExpressionBuilder::function(
ExpressionBuilder::variable('$this')->property('global')->property('fn')->property($orig),
$node->args,
)->build();
return new Expression\AuxiliaryNode(
fn(PrintContext $context, ...$args) => '($this->global->fn->' . $orig . ')(' . $context->implode($args) . ')',
...$node->args,
);
}
});
}
Expand Down
14 changes: 9 additions & 5 deletions src/Latte/Sandbox/SandboxExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
namespace Latte\Sandbox;

use Latte;
use Latte\Compiler\ExpressionBuilder;
use Latte\Compiler\Node;
use Latte\Compiler\Nodes\Php;
use Latte\Compiler\Nodes\Php\Expression;
use Latte\Compiler\Nodes\TemplateNode;
use Latte\Compiler\NodeTraverser;
use Latte\Compiler\PrintContext;
use Latte\Engine;
use Latte\Runtime\Template;
use Latte\SecurityViolationException;
Expand Down Expand Up @@ -95,8 +95,10 @@ private function sandboxVisitor(Node $node): Node
throw new SecurityViolationException("Function $node->name() is not allowed.", $node->position);

} elseif ($node->args) {
$arg = ExpressionBuilder::variable('$this')->property('global')->property('sandbox')->method('args', $node->args)
->build();
$arg = new Expression\AuxiliaryNode(
fn(PrintContext $context, ...$args) => '$this->global->sandbox->args(' . $context->implode($args) . ')',
...$node->args,
);
$node->args = [new Php\ArgumentNode($arg, unpack: true)];
}

Expand All @@ -108,8 +110,10 @@ private function sandboxVisitor(Node $node): Node
throw new SecurityViolationException("Filter |$name is not allowed.", $node->position);

} elseif ($node->args) {
$arg = ExpressionBuilder::variable('$this')->property('global')->property('sandbox')->method('args', $node->args)
->build();
$arg = new Expression\AuxiliaryNode(
fn(PrintContext $context, ...$args) => '$this->global->sandbox->args(' . $context->implode($args) . ')',
...$node->args,
);
$node->args = [new Php\ArgumentNode($arg, unpack: true)];
}

Expand Down

0 comments on commit bdb37ec

Please sign in to comment.