Skip to content

Commit

Permalink
minor twigphp#4059 Get rid of weird code (fabpot)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.x branch.

Discussion
----------

Get rid of weird code

Commits
-------

83fe22a Get rid of weird code
  • Loading branch information
fabpot committed Apr 30, 2024
2 parents 68a826c + 83fe22a commit 775a445
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions src/Node/CheckSecurityNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,30 @@ class CheckSecurityNode extends Node

public function __construct(array $usedFilters, array $usedTags, array $usedFunctions)
{
$this->usedFilters = $usedFilters;
$this->usedTags = $usedTags;
$this->usedFunctions = $usedFunctions;
$this->usedFilters = $this->collect($usedFilters);
$this->usedTags = $this->collect($usedTags);
$this->usedFunctions = $this->collect($usedFunctions);

parent::__construct();
}

public function compile(Compiler $compiler): void
{
$tags = $filters = $functions = [];
foreach (['tags', 'filters', 'functions'] as $type) {
foreach ($this->{'used'.ucfirst($type)} as $name => $node) {
if ($node instanceof Node) {
${$type}[$name] = $node->getTemplateLine();
} else {
${$type}[$node] = null;
}
}
}

$compiler
->write("\n")
->write("public function checkSecurity()\n")
->write("{\n")
->indent()
->write('static $tags = ')->repr(array_filter($tags))->raw(";\n")
->write('static $filters = ')->repr(array_filter($filters))->raw(";\n")
->write('static $functions = ')->repr(array_filter($functions))->raw(";\n\n")
->write('static $tags = ')->repr(array_filter($this->usedTags))->raw(";\n")
->write('static $filters = ')->repr(array_filter($this->usedFilters))->raw(";\n")
->write('static $functions = ')->repr(array_filter($this->usedFunctions))->raw(";\n\n")
->write("try {\n")
->indent()
->write("\$this->sandbox->checkSecurity(\n")
->indent()
->write(!$tags ? "[],\n" : "['".implode("', '", array_keys($tags))."'],\n")
->write(!$filters ? "[],\n" : "['".implode("', '", array_keys($filters))."'],\n")
->write(!$functions ? "[],\n" : "['".implode("', '", array_keys($functions))."'],\n")
->write(!$this->usedTags ? "[],\n" : "['".implode("', '", array_keys($this->usedTags))."'],\n")
->write(!$this->usedFilters ? "[],\n" : "['".implode("', '", array_keys($this->usedFilters))."'],\n")
->write(!$this->usedFunctions ? "[],\n" : "['".implode("', '", array_keys($this->usedFunctions))."'],\n")
->write("\$this->source\n")
->outdent()
->write(");\n")
Expand Down Expand Up @@ -88,4 +77,14 @@ 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;
}
}

0 comments on commit 775a445

Please sign in to comment.