Skip to content

Commit

Permalink
made Symfony compatible with both Twig 1.x and 2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Aug 18, 2015
1 parent d6d93dd commit a5c7a85
Show file tree
Hide file tree
Showing 25 changed files with 47 additions and 42 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": ">=5.3.3",
"doctrine/common": "~2.3",
"twig/twig": "~1.12,>=1.12.3",
"twig/twig": "~1.20|~2.0",
"psr/log": "~1.0"
},
"replace": {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Twig/Node/FormThemeNode.php
Expand Up @@ -16,7 +16,7 @@
*/
class FormThemeNode extends \Twig_Node
{
public function __construct(\Twig_NodeInterface $form, \Twig_NodeInterface $resources, $lineno, $tag = null)
public function __construct(\Twig_Node $form, \Twig_Node $resources, $lineno, $tag = null)
{
parent::__construct(array('form' => $form, 'resources' => $resources), array(), $lineno, $tag);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bridge/Twig/Node/TransNode.php
Expand Up @@ -16,7 +16,7 @@
*/
class TransNode extends \Twig_Node
{
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)
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)
{
parent::__construct(array('count' => $count, 'body' => $body, 'domain' => $domain, 'vars' => $vars, 'locale' => $locale), array(), $lineno, $tag);
}
Expand Down Expand Up @@ -83,7 +83,7 @@ public function compile(\Twig_Compiler $compiler)
$compiler->raw(");\n");
}

protected function compileString(\Twig_NodeInterface $body, \Twig_Node_Expression_Array $vars, $ignoreStrictCheck = false)
protected function compileString(\Twig_Node $body, \Twig_Node_Expression_Array $vars, $ignoreStrictCheck = false)
{
if ($body instanceof \Twig_Node_Expression_Constant) {
$msg = $body->getAttribute('value');
Expand Down
Expand Up @@ -19,7 +19,7 @@
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class TranslationDefaultDomainNodeVisitor implements \Twig_NodeVisitorInterface
class TranslationDefaultDomainNodeVisitor extends \Twig_BaseNodeVisitor
{
/**
* @var Scope
Expand All @@ -37,7 +37,7 @@ public function __construct()
/**
* {@inheritdoc}
*/
public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
{
if ($node instanceof \Twig_Node_Block || $node instanceof \Twig_Node_Module) {
$this->scope = $this->scope->enter();
Expand Down Expand Up @@ -89,7 +89,7 @@ public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
/**
* {@inheritdoc}
*/
public function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env)
protected function doLeaveNode(\Twig_Node $node, \Twig_Environment $env)
{
if ($node instanceof TransDefaultDomainNode) {
return false;
Expand Down
Expand Up @@ -18,7 +18,7 @@
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class TranslationNodeVisitor implements \Twig_NodeVisitorInterface
class TranslationNodeVisitor extends \Twig_BaseNodeVisitor
{
const UNDEFINED_DOMAIN = '_undefined';

Expand All @@ -45,7 +45,7 @@ public function getMessages()
/**
* {@inheritdoc}
*/
public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
{
if (!$this->enabled) {
return $node;
Expand Down Expand Up @@ -85,7 +85,7 @@ public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
/**
* {@inheritdoc}
*/
public function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env)
protected function doLeaveNode(\Twig_Node $node, \Twig_Environment $env)
{
return $node;
}
Expand Down
Expand Up @@ -195,7 +195,7 @@
{# Labels #}

{%- block form_label -%}
{% if label is not sameas(false) %}
{% if label is not same as(false) %}
{%- if not compound -%}
{% set label_attr = label_attr|merge({'for': id}) %}
{%- endif -%}
Expand Down
Expand Up @@ -21,7 +21,7 @@ class RoutingExtensionTest extends TestCase
*/
public function testEscaping($template, $mustBeEscaped)
{
$twig = new \Twig_Environment(null, array('debug' => true, 'cache' => false, 'autoescape' => true, 'optimizations' => 0));
$twig = new \Twig_Environment($this->getMock('Twig_LoaderInterface'), array('debug' => true, 'cache' => false, 'autoescape' => 'html', 'optimizations' => 0));
$twig->addExtension(new RoutingExtension($this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface')));

$nodes = $twig->parse($twig->tokenize($template));
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Twig/Tests/Node/FormThemeTest.php
Expand Up @@ -42,7 +42,7 @@ public function testCompile()

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

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

$this->assertEquals(
sprintf(
Expand Down
20 changes: 10 additions & 10 deletions src/Symfony/Bridge/Twig/Tests/Node/SearchAndRenderBlockNodeTest.php
Expand Up @@ -24,7 +24,7 @@ public function testCompileWidget()

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

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

$this->assertEquals(
sprintf(
Expand All @@ -47,7 +47,7 @@ public function testCompileWidgetWithVariables()

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

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

$this->assertEquals(
sprintf(
Expand All @@ -67,7 +67,7 @@ public function testCompileLabelWithLabel()

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

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

$this->assertEquals(
sprintf(
Expand All @@ -87,7 +87,7 @@ public function testCompileLabelWithNullLabel()

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

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

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

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

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

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

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

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

$this->assertEquals(
sprintf(
Expand All @@ -154,7 +154,7 @@ public function testCompileLabelWithAttributes()

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

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

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

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

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

$this->assertEquals(
sprintf(
Expand Down Expand Up @@ -211,7 +211,7 @@ public function testCompileLabelWithLabelThatEvaluatesToNull()

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

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

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

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

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

// "label" => null must not be included in the output!
// Otherwise the default label is overwritten with null.
Expand Down
6 changes: 5 additions & 1 deletion src/Symfony/Bridge/Twig/Tests/Node/TransNodeTest.php
Expand Up @@ -24,7 +24,7 @@ public function testCompileStrict()
$vars = new \Twig_Node_Expression_Name('foo', 0);
$node = new TransNode($body, null, null, $vars);

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

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

protected function getVariableGetterWithStrictCheck($name)
{
if (version_compare(\Twig_Environment::VERSION, '2.0.0-DEV', '>=')) {
return sprintf('(isset($context["%s"]) || array_key_exists("%s", $context) ? $context["%s"] : $this->notFound("%s", 0))', $name, $name, $name, $name);
}

if (PHP_VERSION_ID >= 50400) {
return sprintf('(isset($context["%s"]) ? $context["%s"] : $this->getContext($context, "%s"))', $name, $name, $name);
}
Expand Down
Expand Up @@ -23,7 +23,7 @@ class TranslationDefaultDomainNodeVisitorTest extends TestCase
/** @dataProvider getDefaultDomainAssignmentTestData */
public function testDefaultDomainAssignment(\Twig_Node $node)
{
$env = new \Twig_Environment(new \Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
$env = new \Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
$visitor = new TranslationDefaultDomainNodeVisitor();

// visit trans_default_domain tag
Expand All @@ -49,7 +49,7 @@ public function testDefaultDomainAssignment(\Twig_Node $node)
/** @dataProvider getDefaultDomainAssignmentTestData */
public function testNewModuleWithoutDefaultDomainTag(\Twig_Node $node)
{
$env = new \Twig_Environment(new \Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
$env = new \Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
$visitor = new TranslationDefaultDomainNodeVisitor();

// visit trans_default_domain tag
Expand Down
Expand Up @@ -19,7 +19,7 @@ class TranslationNodeVisitorTest extends TestCase
/** @dataProvider getMessagesExtractionTestData */
public function testMessagesExtraction(\Twig_Node $node, array $expectedMessages)
{
$env = new \Twig_Environment(new \Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
$env = new \Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
$visitor = new TranslationNodeVisitor();
$visitor->enable();
$visitor->enterNode($node, $env);
Expand Down
Expand Up @@ -22,7 +22,7 @@ class FormThemeTokenParserTest extends TestCase
*/
public function testCompile($source, $expected)
{
$env = new \Twig_Environment(new \Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
$env = new \Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
$env->addTokenParser(new FormThemeTokenParser());
$stream = $env->tokenize($source);
$parser = new \Twig_Parser($env);
Expand Down
Expand Up @@ -23,7 +23,7 @@ class TwigExtractorTest extends TestCase
*/
public function testExtract($template, $messages)
{
$loader = new \Twig_Loader_Array(array());
$loader = $this->getMock('Twig_LoaderInterface');
$twig = new \Twig_Environment($loader, array(
'strict_variables' => true,
'debug' => true,
Expand Down Expand Up @@ -78,7 +78,7 @@ public function getExtractData()
*/
public function testExtractSyntaxError()
{
$twig = new \Twig_Environment(new \Twig_Loader_Array(array()));
$twig = new \Twig_Environment($this->getMock('Twig_LoaderInterface'));
$twig->addExtension(new TranslationExtension($this->getMock('Symfony\Component\Translation\TranslatorInterface')));

$extractor = new TwigExtractor($twig);
Expand Down
Expand Up @@ -25,7 +25,7 @@ class FormThemeTokenParser extends \Twig_TokenParser
*
* @param \Twig_Token $token A Twig_Token instance
*
* @return \Twig_NodeInterface A Twig_NodeInterface instance
* @return \Twig_Node A Twig_Node instance
*/
public function parse(\Twig_Token $token)
{
Expand Down
Expand Up @@ -25,7 +25,7 @@ class TransChoiceTokenParser extends TransTokenParser
*
* @param \Twig_Token $token A Twig_Token instance
*
* @return \Twig_NodeInterface A Twig_NodeInterface instance
* @return \Twig_Node A Twig_Node instance
*
* @throws \Twig_Error_Syntax
*/
Expand Down
Expand Up @@ -25,7 +25,7 @@ class TransDefaultDomainTokenParser extends \Twig_TokenParser
*
* @param \Twig_Token $token A Twig_Token instance
*
* @return \Twig_NodeInterface A Twig_NodeInterface instance
* @return \Twig_Node A Twig_Node instance
*/
public function parse(\Twig_Token $token)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Twig/TokenParser/TransTokenParser.php
Expand Up @@ -25,7 +25,7 @@ class TransTokenParser extends \Twig_TokenParser
*
* @param \Twig_Token $token A Twig_Token instance
*
* @return \Twig_NodeInterface A Twig_NodeInterface instance
* @return \Twig_Node A Twig_Node instance
*
* @throws \Twig_Error_Syntax
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Twig/composer.json
Expand Up @@ -17,7 +17,7 @@
],
"require": {
"php": ">=5.3.3",
"twig/twig": "~1.12,>=1.12.3"
"twig/twig": "~1.20|~2.0"
},
"require-dev": {
"symfony/phpunit-bridge": "~2.7",
Expand Down
Expand Up @@ -22,7 +22,7 @@ public function testRender()
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$container->expects($this->once())
->method('get')
->will($this->returnValue($this->getMock('\Twig_Environment')))
->will($this->returnValue($this->getMockBuilder('\Twig_Environment')->disableOriginalConstructor()->getMock()))
;
$renderer = new ContainerAwareHIncludeFragmentRenderer($container);
$renderer->render('/', Request::create('/'));
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/SecurityBundle/composer.json
Expand Up @@ -36,7 +36,7 @@
"symfony/process": "~2.0,>=2.0.5",
"symfony/validator": "~2.2",
"symfony/yaml": "~2.0,>=2.0.5",
"twig/twig": "~1.12"
"twig/twig": "~1.20|~2.0"
},
"autoload": {
"psr-0": { "Symfony\\Bundle\\SecurityBundle\\": "" }
Expand Down
Expand Up @@ -30,7 +30,7 @@ protected function setUp()
*/
public function testCompile($source, $expected)
{
$env = new \Twig_Environment(new \Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
$env = new \Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
$env->addTokenParser(new RenderTokenParser());
$stream = $env->tokenize($source);
$parser = new \Twig_Parser($env);
Expand Down
Expand Up @@ -23,7 +23,7 @@ class ProfilerControllerTest extends \PHPUnit_Framework_TestCase
public function testEmptyToken($token)
{
$urlGenerator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface');
$twig = $this->getMock('Twig_Environment');
$twig = $this->getMockBuilder('Twig_Environment')->disableOriginalConstructor()->getMock();
$profiler = $this
->getMockBuilder('Symfony\Component\HttpKernel\Profiler\Profiler')
->disableOriginalConstructor()
Expand All @@ -47,7 +47,7 @@ public function getEmptyTokenCases()
public function testReturns404onTokenNotFound()
{
$urlGenerator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface');
$twig = $this->getMock('Twig_Environment');
$twig = $this->getMockBuilder('Twig_Environment')->disableOriginalConstructor()->getMock();
$profiler = $this
->getMockBuilder('Symfony\Component\HttpKernel\Profiler\Profiler')
->disableOriginalConstructor()
Expand Down
Expand Up @@ -55,7 +55,8 @@ protected function setUp()
$this->container->register('templating.helper.assets', $this->getMockClass('Symfony\\Component\\Templating\\Helper\\AssetsHelper'));
$this->container->register('templating.helper.router', $this->getMockClass('Symfony\\Bundle\\FrameworkBundle\\Templating\\Helper\\RouterHelper'))
->addArgument(new Reference('router'));
$this->container->register('twig', 'Twig_Environment');
$this->container->register('twig_loader', 'Twig_Loader_Array')->addArgument(array());
$this->container->register('twig', 'Twig_Environment')->addArgument(new Reference('twig_loader'));
$this->container->setParameter('kernel.bundles', array());
$this->container->setParameter('kernel.cache_dir', __DIR__);
$this->container->setParameter('kernel.debug', false);
Expand Down
Expand Up @@ -138,7 +138,7 @@ protected function mockProfile()

protected function mockTwigEnvironment()
{
$this->twigEnvironment = $this->getMockBuilder('Twig_Environment')->getMock();
$this->twigEnvironment = $this->getMockBuilder('Twig_Environment')->disableOriginalConstructor()->getMock();

$this->twigEnvironment->expects($this->any())
->method('loadTemplate')
Expand Down

0 comments on commit a5c7a85

Please sign in to comment.