Skip to content

Commit a5c7a85

Browse files
committed
made Symfony compatible with both Twig 1.x and 2.x
1 parent d6d93dd commit a5c7a85

25 files changed

+47
-42
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"require": {
1919
"php": ">=5.3.3",
2020
"doctrine/common": "~2.3",
21-
"twig/twig": "~1.12,>=1.12.3",
21+
"twig/twig": "~1.20|~2.0",
2222
"psr/log": "~1.0"
2323
},
2424
"replace": {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
class FormThemeNode extends \Twig_Node
1818
{
19-
public function __construct(\Twig_NodeInterface $form, \Twig_NodeInterface $resources, $lineno, $tag = null)
19+
public function __construct(\Twig_Node $form, \Twig_Node $resources, $lineno, $tag = null)
2020
{
2121
parent::__construct(array('form' => $form, 'resources' => $resources), array(), $lineno, $tag);
2222
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
class TransNode extends \Twig_Node
1818
{
19-
public function __construct(\Twig_NodeInterface $body, \Twig_NodeInterface $domain = null, \Twig_Node_Expression $count = null, \Twig_Node_Expression $vars = null, \Twig_Node_Expression $locale = null, $lineno = 0, $tag = null)
19+
public function __construct(\Twig_Node $body, \Twig_Node $domain = null, \Twig_Node_Expression $count = null, \Twig_Node_Expression $vars = null, \Twig_Node_Expression $locale = null, $lineno = 0, $tag = null)
2020
{
2121
parent::__construct(array('count' => $count, 'body' => $body, 'domain' => $domain, 'vars' => $vars, 'locale' => $locale), array(), $lineno, $tag);
2222
}
@@ -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, $ignoreStrictCheck = false)
86+
protected function compileString(\Twig_Node $body, \Twig_Node_Expression_Array $vars, $ignoreStrictCheck = false)
8787
{
8888
if ($body instanceof \Twig_Node_Expression_Constant) {
8989
$msg = $body->getAttribute('value');

src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @author Fabien Potencier <[email protected]>
2121
*/
22-
class TranslationDefaultDomainNodeVisitor implements \Twig_NodeVisitorInterface
22+
class TranslationDefaultDomainNodeVisitor extends \Twig_BaseNodeVisitor
2323
{
2424
/**
2525
* @var Scope
@@ -37,7 +37,7 @@ public function __construct()
3737
/**
3838
* {@inheritdoc}
3939
*/
40-
public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
40+
protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
4141
{
4242
if ($node instanceof \Twig_Node_Block || $node instanceof \Twig_Node_Module) {
4343
$this->scope = $this->scope->enter();
@@ -89,7 +89,7 @@ public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
8989
/**
9090
* {@inheritdoc}
9191
*/
92-
public function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env)
92+
protected function doLeaveNode(\Twig_Node $node, \Twig_Environment $env)
9393
{
9494
if ($node instanceof TransDefaultDomainNode) {
9595
return false;

src/Symfony/Bridge/Twig/NodeVisitor/TranslationNodeVisitor.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @author Fabien Potencier <[email protected]>
2020
*/
21-
class TranslationNodeVisitor implements \Twig_NodeVisitorInterface
21+
class TranslationNodeVisitor extends \Twig_BaseNodeVisitor
2222
{
2323
const UNDEFINED_DOMAIN = '_undefined';
2424

@@ -45,7 +45,7 @@ public function getMessages()
4545
/**
4646
* {@inheritdoc}
4747
*/
48-
public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
48+
protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
4949
{
5050
if (!$this->enabled) {
5151
return $node;
@@ -85,7 +85,7 @@ public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
8585
/**
8686
* {@inheritdoc}
8787
*/
88-
public function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env)
88+
protected function doLeaveNode(\Twig_Node $node, \Twig_Environment $env)
8989
{
9090
return $node;
9191
}

src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@
195195
{# Labels #}
196196

197197
{%- block form_label -%}
198-
{% if label is not sameas(false) %}
198+
{% if label is not same as(false) %}
199199
{%- if not compound -%}
200200
{% set label_attr = label_attr|merge({'for': id}) %}
201201
{%- endif -%}

src/Symfony/Bridge/Twig/Tests/Extension/RoutingExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class RoutingExtensionTest extends TestCase
2121
*/
2222
public function testEscaping($template, $mustBeEscaped)
2323
{
24-
$twig = new \Twig_Environment(null, array('debug' => true, 'cache' => false, 'autoescape' => true, 'optimizations' => 0));
24+
$twig = new \Twig_Environment($this->getMock('Twig_LoaderInterface'), array('debug' => true, 'cache' => false, 'autoescape' => 'html', 'optimizations' => 0));
2525
$twig->addExtension(new RoutingExtension($this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface')));
2626

2727
$nodes = $twig->parse($twig->tokenize($template));

src/Symfony/Bridge/Twig/Tests/Node/FormThemeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function testCompile()
4242

4343
$node = new FormThemeNode($form, $resources, 0);
4444

45-
$compiler = new \Twig_Compiler(new \Twig_Environment());
45+
$compiler = new \Twig_Compiler(new \Twig_Environment($this->getMock('Twig_LoaderInterface')));
4646

4747
$this->assertEquals(
4848
sprintf(

src/Symfony/Bridge/Twig/Tests/Node/SearchAndRenderBlockNodeTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function testCompileWidget()
2424

2525
$node = new SearchAndRenderBlockNode('form_widget', $arguments, 0);
2626

27-
$compiler = new \Twig_Compiler(new \Twig_Environment());
27+
$compiler = new \Twig_Compiler(new \Twig_Environment($this->getMock('Twig_LoaderInterface')));
2828

2929
$this->assertEquals(
3030
sprintf(
@@ -47,7 +47,7 @@ public function testCompileWidgetWithVariables()
4747

4848
$node = new SearchAndRenderBlockNode('form_widget', $arguments, 0);
4949

50-
$compiler = new \Twig_Compiler(new \Twig_Environment());
50+
$compiler = new \Twig_Compiler(new \Twig_Environment($this->getMock('Twig_LoaderInterface')));
5151

5252
$this->assertEquals(
5353
sprintf(
@@ -67,7 +67,7 @@ public function testCompileLabelWithLabel()
6767

6868
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
6969

70-
$compiler = new \Twig_Compiler(new \Twig_Environment());
70+
$compiler = new \Twig_Compiler(new \Twig_Environment($this->getMock('Twig_LoaderInterface')));
7171

7272
$this->assertEquals(
7373
sprintf(
@@ -87,7 +87,7 @@ public function testCompileLabelWithNullLabel()
8787

8888
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
8989

90-
$compiler = new \Twig_Compiler(new \Twig_Environment());
90+
$compiler = new \Twig_Compiler(new \Twig_Environment($this->getMock('Twig_LoaderInterface')));
9191

9292
// "label" => null must not be included in the output!
9393
// Otherwise the default label is overwritten with null.
@@ -109,7 +109,7 @@ public function testCompileLabelWithEmptyStringLabel()
109109

110110
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
111111

112-
$compiler = new \Twig_Compiler(new \Twig_Environment());
112+
$compiler = new \Twig_Compiler(new \Twig_Environment($this->getMock('Twig_LoaderInterface')));
113113

114114
// "label" => null must not be included in the output!
115115
// Otherwise the default label is overwritten with null.
@@ -130,7 +130,7 @@ public function testCompileLabelWithDefaultLabel()
130130

131131
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
132132

133-
$compiler = new \Twig_Compiler(new \Twig_Environment());
133+
$compiler = new \Twig_Compiler(new \Twig_Environment($this->getMock('Twig_LoaderInterface')));
134134

135135
$this->assertEquals(
136136
sprintf(
@@ -154,7 +154,7 @@ public function testCompileLabelWithAttributes()
154154

155155
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
156156

157-
$compiler = new \Twig_Compiler(new \Twig_Environment());
157+
$compiler = new \Twig_Compiler(new \Twig_Environment($this->getMock('Twig_LoaderInterface')));
158158

159159
// "label" => null must not be included in the output!
160160
// Otherwise the default label is overwritten with null.
@@ -183,7 +183,7 @@ public function testCompileLabelWithLabelAndAttributes()
183183

184184
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
185185

186-
$compiler = new \Twig_Compiler(new \Twig_Environment());
186+
$compiler = new \Twig_Compiler(new \Twig_Environment($this->getMock('Twig_LoaderInterface')));
187187

188188
$this->assertEquals(
189189
sprintf(
@@ -211,7 +211,7 @@ public function testCompileLabelWithLabelThatEvaluatesToNull()
211211

212212
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
213213

214-
$compiler = new \Twig_Compiler(new \Twig_Environment());
214+
$compiler = new \Twig_Compiler(new \Twig_Environment($this->getMock('Twig_LoaderInterface')));
215215

216216
// "label" => null must not be included in the output!
217217
// Otherwise the default label is overwritten with null.
@@ -248,7 +248,7 @@ public function testCompileLabelWithLabelThatEvaluatesToNullAndAttributes()
248248

249249
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
250250

251-
$compiler = new \Twig_Compiler(new \Twig_Environment());
251+
$compiler = new \Twig_Compiler(new \Twig_Environment($this->getMock('Twig_LoaderInterface')));
252252

253253
// "label" => null must not be included in the output!
254254
// Otherwise the default label is overwritten with null.

src/Symfony/Bridge/Twig/Tests/Node/TransNodeTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function testCompileStrict()
2424
$vars = new \Twig_Node_Expression_Name('foo', 0);
2525
$node = new TransNode($body, null, null, $vars);
2626

27-
$env = new \Twig_Environment(null, array('strict_variables' => true));
27+
$env = new \Twig_Environment($this->getMock('Twig_LoaderInterface'), array('strict_variables' => true));
2828
$compiler = new \Twig_Compiler($env);
2929

3030
$this->assertEquals(
@@ -47,6 +47,10 @@ protected function getVariableGetterWithoutStrictCheck($name)
4747

4848
protected function getVariableGetterWithStrictCheck($name)
4949
{
50+
if (version_compare(\Twig_Environment::VERSION, '2.0.0-DEV', '>=')) {
51+
return sprintf('(isset($context["%s"]) || array_key_exists("%s", $context) ? $context["%s"] : $this->notFound("%s", 0))', $name, $name, $name, $name);
52+
}
53+
5054
if (PHP_VERSION_ID >= 50400) {
5155
return sprintf('(isset($context["%s"]) ? $context["%s"] : $this->getContext($context, "%s"))', $name, $name, $name);
5256
}

0 commit comments

Comments
 (0)