Skip to content

Commit 11f9497

Browse files
committed
bug symfony#10643 [TwigBridge] Removed strict check when found variables inside a translation (goetas)
This PR was submitted for the master branch but it was merged into the 2.3 branch instead (closes symfony#10643). Discussion ---------- [TwigBridge] Removed strict check when found variables inside a translation | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no? | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#10638 | License | MIT Commits ------- 074191e Removed strict check when found variables inside a translation
2 parents d28149f + 074191e commit 11f9497

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

src/Symfony/Bridge/Twig/Node/TransNode.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function compile(\Twig_Compiler $compiler)
3636
$defaults = $this->getNode('vars');
3737
$vars = null;
3838
}
39-
list($msg, $defaults) = $this->compileString($this->getNode('body'), $defaults);
39+
list($msg, $defaults) = $this->compileString($this->getNode('body'), $defaults, (boolean) $vars);
4040

4141
$method = null === $this->getNode('count') ? 'trans' : 'transChoice';
4242

@@ -83,7 +83,7 @@ public function compile(\Twig_Compiler $compiler)
8383
$compiler->raw(");\n");
8484
}
8585

86-
protected function compileString(\Twig_NodeInterface $body, \Twig_Node_Expression_Array $vars)
86+
protected function compileString(\Twig_NodeInterface $body, \Twig_Node_Expression_Array $vars, $ignoreStrictCheck = false)
8787
{
8888
if ($body instanceof \Twig_Node_Expression_Constant) {
8989
$msg = $body->getAttribute('value');
@@ -98,7 +98,9 @@ protected function compileString(\Twig_NodeInterface $body, \Twig_Node_Expressio
9898
foreach ($matches[1] as $var) {
9999
$key = new \Twig_Node_Expression_Constant('%'.$var.'%', $body->getLine());
100100
if (!$vars->hasElement($key)) {
101-
$vars->addElement(new \Twig_Node_Expression_Name($var, $body->getLine()), $key);
101+
$varExpr = new \Twig_Node_Expression_Name($var, $body->getLine());
102+
$varExpr->setAttribute('ignore_strict_check', $ignoreStrictCheck);
103+
$vars->addElement($varExpr, $key);
102104
}
103105
}
104106

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Twig\Tests\Node;
13+
14+
use Symfony\Bridge\Twig\Node\TransNode;
15+
16+
/**
17+
*
18+
* @author Asmir Mustafic <goetas@gmail.com>
19+
*
20+
*/
21+
class TransNodeTest extends \PHPUnit_Framework_TestCase
22+
{
23+
24+
public function testCompileStrict()
25+
{
26+
$body = new \Twig_Node_Text('trans %var%', 0);
27+
$vars = new \Twig_Node_Expression_Name('foo', 0);
28+
$node = new TransNode($body, null, null, $vars);
29+
30+
$env = new \Twig_Environment(null, array(
31+
'strict_variables' => true,
32+
));
33+
$compiler = new \Twig_Compiler($env);
34+
35+
$this->assertEquals(
36+
sprintf(
37+
'echo $this->env->getExtension(\'translator\')->getTranslator()->trans("trans %%var%%", array_merge(array("%%var%%" => %s), %s), "messages");',
38+
$this->getVariableGetterWithoutStrictCheck('var'),
39+
$this->getVariableGetterWithStrictCheck('foo')
40+
),
41+
trim($compiler->compile($node)->getSource())
42+
);
43+
}
44+
protected function getVariableGetterWithoutStrictCheck($name)
45+
{
46+
if (version_compare(phpversion(), '5.4.0RC1', '>=')) {
47+
return sprintf('(isset($context["%s"]) ? $context["%s"] : null)', $name, $name);
48+
}
49+
50+
return sprintf('$this->getContext($context, "%s", true)', $name);
51+
}
52+
protected function getVariableGetterWithStrictCheck($name)
53+
{
54+
if (version_compare(phpversion(), '5.4.0RC1', '>=')) {
55+
return sprintf('(isset($context["%s"]) ? $context["%s"] : $this->getContext($context, "%s"))', $name, $name, $name);
56+
}
57+
58+
return sprintf('$this->getContext($context, "%s")', $name);
59+
}
60+
}

0 commit comments

Comments
 (0)