mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-25 17:06:26 +00:00
Optimize compiled code for "set" tag
This commit is contained in:
+24
-6
@@ -33,9 +33,15 @@ class SetNode extends Node implements NodeCaptureInterface
|
||||
$safe = false;
|
||||
if ($capture) {
|
||||
$safe = true;
|
||||
if ($values instanceof TextNode) {
|
||||
if (Node::class === get_class($values) && !count($values)) {
|
||||
$values = new ConstantExpression('', $values->getTemplateLine());
|
||||
$capture = false;
|
||||
} elseif ($values instanceof TextNode) {
|
||||
$values = new ConstantExpression($values->getAttribute('data'), $values->getTemplateLine());
|
||||
$capture = false;
|
||||
} elseif ($values instanceof PrintNode && $values->getNode('expr') instanceof ConstantExpression) {
|
||||
$values = $values->getNode('expr');
|
||||
$capture = false;
|
||||
} else {
|
||||
$values = new CaptureNode($values, $values->getTemplateLine());
|
||||
}
|
||||
@@ -78,11 +84,23 @@ class SetNode extends Node implements NodeCaptureInterface
|
||||
$compiler->raw(']');
|
||||
} else {
|
||||
if ($this->getAttribute('safe')) {
|
||||
$compiler
|
||||
->raw("('' === \$tmp = ")
|
||||
->subcompile($this->getNode('values'))
|
||||
->raw(") ? '' : new Markup(\$tmp, \$this->env->getCharset())")
|
||||
;
|
||||
if ($this->getNode('values') instanceof ConstantExpression) {
|
||||
if ('' === $this->getNode('values')->getAttribute('value')) {
|
||||
$compiler->raw('""');
|
||||
} else {
|
||||
$compiler
|
||||
->raw('new Markup(')
|
||||
->subcompile($this->getNode('values'))
|
||||
->raw(', $this->env->getCharset())')
|
||||
;
|
||||
}
|
||||
} else {
|
||||
$compiler
|
||||
->raw("('' === \$tmp = ")
|
||||
->subcompile($this->getNode('values'))
|
||||
->raw(") ? '' : new Markup(\$tmp, \$this->env->getCharset())")
|
||||
;
|
||||
}
|
||||
} else {
|
||||
$compiler->subcompile($this->getNode('values'));
|
||||
}
|
||||
|
||||
+19
-1
@@ -77,7 +77,25 @@ EOF
|
||||
$node = new SetNode(true, $names, $values, 1);
|
||||
$tests[] = [$node, <<<EOF
|
||||
// line 1
|
||||
\$context["foo"] = ('' === \$tmp = "foo") ? '' : new Markup(\$tmp, \$this->env->getCharset());
|
||||
\$context["foo"] = new Markup("foo", \$this->env->getCharset());
|
||||
EOF
|
||||
];
|
||||
|
||||
$names = new Node([new AssignNameExpression('foo', 1)], [], 1);
|
||||
$values = new TextNode('', 1);
|
||||
$node = new SetNode(true, $names, $values, 1);
|
||||
$tests[] = [$node, <<<EOF
|
||||
// line 1
|
||||
\$context["foo"] = "";
|
||||
EOF
|
||||
];
|
||||
|
||||
$names = new Node([new AssignNameExpression('foo', 1)], [], 1);
|
||||
$values = new PrintNode(new ConstantExpression('foo', 1), 1);
|
||||
$node = new SetNode(true, $names, $values, 1);
|
||||
$tests[] = [$node, <<<EOF
|
||||
// line 1
|
||||
\$context["foo"] = new Markup("foo", \$this->env->getCharset());
|
||||
EOF
|
||||
];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user