improved the performance of the sandbox

This commit is contained in:
Fabien Potencier
2019-03-10 10:11:44 +01:00
parent 68bbf5eb85
commit 5c66186979
3 changed files with 27 additions and 6 deletions
+1
View File
@@ -1,5 +1,6 @@
* 1.38.0 (2019-XX-XX)
* improved the performance of the sandbox
* added a spaceless filter
* added max value to the "random" function
* made namespace classes the default classes (PSR-0 ones are aliases now)
+15 -3
View File
@@ -12,6 +12,7 @@
namespace Twig\Node;
use Twig\Compiler;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\FilterExpression;
/**
@@ -29,10 +30,21 @@ class SandboxedPrintNode extends PrintNode
{
$compiler
->addDebugInfo($this)
->write('echo $this->env->getExtension(\'\Twig\Extension\SandboxExtension\')->ensureToStringAllowed(')
->subcompile($this->getNode('expr'))
->raw(");\n")
->write('echo ')
;
$expr = $this->getNode('expr');
if ($expr instanceof ConstantExpression) {
$compiler
->subcompile($expr)
->raw(";\n")
;
} else {
$compiler
->write('$this->env->getExtension(\'\Twig\Extension\SandboxExtension\')->ensureToStringAllowed(')
->subcompile($expr)
->raw(");\n")
;
}
}
/**
+11 -3
View File
@@ -9,8 +9,12 @@
* file that was distributed with this source code.
*/
use Twig\Node\Expression\ArrayExpression;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\GetAttrExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\SandboxedPrintNode;
use Twig\Template;
use Twig\Test\NodeTestCase;
class Twig_Tests_Node_SandboxedPrintTest extends NodeTestCase
@@ -24,11 +28,15 @@ class Twig_Tests_Node_SandboxedPrintTest extends NodeTestCase
public function getTests()
{
$tests = [];
$tests[] = [new SandboxedPrintNode(new ConstantExpression('foo', 1), 1), <<<EOF
// line 1
echo \$this->env->getExtension('\Twig\Extension\SandboxExtension')->ensureToStringAllowed("foo");
echo "foo";
EOF
];
$tests[] = [new SandboxedPrintNode(new NameExpression('foo', 1), 1), <<<EOF
// line 1
echo \$this->env->getExtension('\Twig\Extension\SandboxExtension')->ensureToStringAllowed({$this->getVariableGetter('foo', false)});
EOF
];