Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify sandbox code #4060

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 8 additions & 13 deletions src/Node/CheckSecurityNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ class CheckSecurityNode extends Node
private $usedTags;
private $usedFunctions;

/**
* @param array<string, int> $usedFilters
* @param array<string, int> $usedTags
* @param array<string, int> $usedFunctions
*/
public function __construct(array $usedFilters, array $usedTags, array $usedFunctions)
{
$this->usedFilters = $this->collect($usedFilters);
$this->usedTags = $this->collect($usedTags);
$this->usedFunctions = $this->collect($usedFunctions);
$this->usedFilters = $usedFilters;
$this->usedTags = $usedTags;
$this->usedFunctions = $usedFunctions;

parent::__construct();
}
Expand Down Expand Up @@ -77,14 +82,4 @@ public function compile(Compiler $compiler): void
->write("}\n")
;
}

private function collect(array $used)
{
$collected = [];
foreach ($used as $name => $node) {
$collected[$name] = $node instanceof Node ? $node->getTemplateLine() : null;
}

return $collected;
}
}
11 changes: 7 additions & 4 deletions src/NodeVisitor/SandboxNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@
final class SandboxNodeVisitor implements NodeVisitorInterface
{
private $inAModule = false;
/** @var array<string, int> */
private $tags;
/** @var array<string, int> */
private $filters;
/** @var array<string, int> */
private $functions;
private $needsToStringWrap = false;

Expand All @@ -51,22 +54,22 @@ public function enterNode(Node $node, Environment $env): Node
} elseif ($this->inAModule) {
// look for tags
if ($node->getNodeTag() && !isset($this->tags[$node->getNodeTag()])) {
$this->tags[$node->getNodeTag()] = $node;
$this->tags[$node->getNodeTag()] = $node->getTemplateLine();
}

// look for filters
if ($node instanceof FilterExpression && !isset($this->filters[$node->getNode('filter')->getAttribute('value')])) {
$this->filters[$node->getNode('filter')->getAttribute('value')] = $node;
$this->filters[$node->getNode('filter')->getAttribute('value')] = $node->getTemplateLine();
}

// look for functions
if ($node instanceof FunctionExpression && !isset($this->functions[$node->getAttribute('name')])) {
$this->functions[$node->getAttribute('name')] = $node;
$this->functions[$node->getAttribute('name')] = $node->getTemplateLine();
}

// the .. operator is equivalent to the range() function
if ($node instanceof RangeBinary && !isset($this->functions['range'])) {
$this->functions['range'] = $node;
$this->functions['range'] = $node->getTemplateLine();
}

if ($node instanceof PrintNode) {
Expand Down