Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[TwigBundle] fixes errors with the default domain node visitor & scope #7348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 12, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Bridge/Twig/NodeVisitor/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function __construct(Scope $parent = null)
{
$this->parent = $parent;
$this->left = false;
$this->data = array();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,10 @@ public function __construct()
*/
public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
{
if ($node instanceof \Twig_Node_Block) {
if ($node instanceof \Twig_Node_Block || $node instanceof \Twig_Node_Module) {
$this->scope = $this->scope->enter();
}

if ($node instanceof \Twig_Node_Module) {
$this->scope->set('domain', null);
}

if ($node instanceof TransDefaultDomainNode) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we enter a new scope for Twig_Node_Module as well (i.e. when starting to parse a new template, for instance with {% embed %} ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should add a testcase for this case (it may be easier and more readable to use an integration test for this in which you render a template using a custom translator using domain:key as translation to see easily if the right domain was applied rather than comparing the AST before and after the rendering)

if ($node->getNode('expr') instanceof \Twig_Node_Expression_Constant) {
$this->scope->set('domain', $node->getNode('expr'));
Expand Down Expand Up @@ -93,7 +89,7 @@ public function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env)
return false;
}

if ($node instanceof \Twig_Node_Block) {
if ($node instanceof \Twig_Node_Block || $node instanceof \Twig_Node_Module) {
$this->scope = $this->scope->leave();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ class TranslationDefaultDomainNodeVisitorTest extends TestCase
public function testDefaultDomainAssignment(\Twig_Node $node)
{
$env = new \Twig_Environment(new \Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));

$visitor = new TranslationDefaultDomainNodeVisitor();

// visit trans_default_domain tag
$defaultDomain = TwigNodeProvider::getTransDefaultDomainTag('domain');
$defaultDomain = TwigNodeProvider::getTransDefaultDomainTag(self::$domain);
$visitor->enterNode($defaultDomain, $env);
$visitor->leaveNode($defaultDomain, $env);

Expand All @@ -38,12 +37,38 @@ public function testDefaultDomainAssignment(\Twig_Node $node)
$this->assertEquals(array(array(self::$message, self::$domain)), $visitor->getMessages());
}

/** @dataProvider getDefaultDomainAssignmentTestData */
public function testNewModuleWithoutDefaultDomainTag(\Twig_Node $node)
{
$env = new \Twig_Environment(new \Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
$visitor = new TranslationDefaultDomainNodeVisitor();

// visit trans_default_domain tag
$newModule = TwigNodeProvider::getModule('test');
$visitor->enterNode($newModule, $env);
$visitor->leaveNode($newModule, $env);

// visit tested node
$enteredNode = $visitor->enterNode($node, $env);
$leavedNode = $visitor->leaveNode($node, $env);
$this->assertSame($node, $enteredNode);
$this->assertSame($node, $leavedNode);

// extracting tested node messages
$visitor = new TranslationNodeVisitor();
$visitor->enable();
$visitor->enterNode($node, $env);
$visitor->leaveNode($node, $env);

$this->assertEquals(array(array(self::$message, null)), $visitor->getMessages());
}

public function getDefaultDomainAssignmentTestData()
{
return array(
array(TwigNodeProvider::getTransFilter(self::$message, self::$domain)),
array(TwigNodeProvider::getTransChoiceFilter(self::$message, self::$domain)),
array(TwigNodeProvider::getTransTag(self::$message, self::$domain)),
array(TwigNodeProvider::getTransFilter(self::$message)),
array(TwigNodeProvider::getTransChoiceFilter(self::$message)),
array(TwigNodeProvider::getTransTag(self::$message)),
);
}
}
13 changes: 13 additions & 0 deletions src/Symfony/Bridge/Twig/Tests/NodeVisitor/TwigNodeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@

class TwigNodeProvider
{
public static function getModule($content)
{
return new \Twig_Node_Module(
new \Twig_Node_Expression_Constant($content, 0),
null,
new \Twig_Node_Expression_Array(array(), 0),
new \Twig_Node_Expression_Array(array(), 0),
new \Twig_Node_Expression_Array(array(), 0),
null,
null
);
}

public static function getTransFilter($message, $domain = null)
{
$arguments = $domain ? array(
Expand Down