mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-30 12:06:56 +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;
|
$safe = false;
|
||||||
if ($capture) {
|
if ($capture) {
|
||||||
$safe = true;
|
$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());
|
$values = new ConstantExpression($values->getAttribute('data'), $values->getTemplateLine());
|
||||||
$capture = false;
|
$capture = false;
|
||||||
|
} elseif ($values instanceof PrintNode && $values->getNode('expr') instanceof ConstantExpression) {
|
||||||
|
$values = $values->getNode('expr');
|
||||||
|
$capture = false;
|
||||||
} else {
|
} else {
|
||||||
$values = new CaptureNode($values, $values->getTemplateLine());
|
$values = new CaptureNode($values, $values->getTemplateLine());
|
||||||
}
|
}
|
||||||
@@ -78,11 +84,23 @@ class SetNode extends Node implements NodeCaptureInterface
|
|||||||
$compiler->raw(']');
|
$compiler->raw(']');
|
||||||
} else {
|
} else {
|
||||||
if ($this->getAttribute('safe')) {
|
if ($this->getAttribute('safe')) {
|
||||||
$compiler
|
if ($this->getNode('values') instanceof ConstantExpression) {
|
||||||
->raw("('' === \$tmp = ")
|
if ('' === $this->getNode('values')->getAttribute('value')) {
|
||||||
->subcompile($this->getNode('values'))
|
$compiler->raw('""');
|
||||||
->raw(") ? '' : new Markup(\$tmp, \$this->env->getCharset())")
|
} 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 {
|
} else {
|
||||||
$compiler->subcompile($this->getNode('values'));
|
$compiler->subcompile($this->getNode('values'));
|
||||||
}
|
}
|
||||||
|
|||||||
+19
-1
@@ -77,7 +77,25 @@ EOF
|
|||||||
$node = new SetNode(true, $names, $values, 1);
|
$node = new SetNode(true, $names, $values, 1);
|
||||||
$tests[] = [$node, <<<EOF
|
$tests[] = [$node, <<<EOF
|
||||||
// line 1
|
// 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
|
EOF
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user