Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TwigBridge] added a way to specify a default domain for a Twig templ…
…ate (via the 'trans_default_domain' tag) Note that the tag only influences the current templates. It has no effect on included files to avoid unwanted side-effects.
- Loading branch information
Showing
7 changed files
with
219 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Twig\Node; | ||
|
||
/** | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
class TransDefaultDomainNode extends \Twig_Node | ||
{ | ||
public function __construct(\Twig_Node_Expression $expr, $lineno = 0, $tag = null) | ||
{ | ||
parent::__construct(array('expr' => $expr), array(), $lineno, $tag); | ||
} | ||
|
||
/** | ||
* Compiles the node to PHP. | ||
* | ||
* @param \Twig_Compiler $compiler A Twig_Compiler instance | ||
*/ | ||
public function compile(\Twig_Compiler $compiler) | ||
{ | ||
// noop as this node is just a marker for TranslationDefaultDomainNodeVisitor | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Twig\NodeVisitor; | ||
|
||
use Symfony\Bridge\Twig\Node\TransNode; | ||
use Symfony\Bridge\Twig\Node\TransDefaultDomainNode; | ||
|
||
/** | ||
* TranslationDefaultDomainNodeVisitor. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
class TranslationDefaultDomainNodeVisitor implements \Twig_NodeVisitorInterface | ||
{ | ||
private $domain; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env) | ||
{ | ||
if ($node instanceof \Twig_Node_Module) { | ||
$this->domain = null; | ||
} | ||
|
||
if ($node instanceof TransDefaultDomainNode) { | ||
$var = $env->getParser()->getVarName(); | ||
$name = new \Twig_Node_Expression_AssignName($var, $node->getLine()); | ||
$this->domain = new \Twig_Node_Expression_Name($var, $node->getLine()); | ||
|
||
return new \Twig_Node_Set(false, new \Twig_Node(array($name)), new \Twig_Node(array($node->getNode('expr'))), $node->getLine()); | ||
} | ||
|
||
if (null === $this->domain) { | ||
return $node; | ||
} | ||
|
||
if ($node instanceof \Twig_Node_Expression_Filter && in_array($node->getNode('filter')->getAttribute('value'), array('trans', 'transchoice'))) { | ||
$ind = 'trans' === $node->getNode('filter')->getAttribute('value') ? 1 : 2; | ||
$arguments = $node->getNode('arguments'); | ||
if (!$arguments->hasNode($ind)) { | ||
if (!$arguments->hasNode($ind - 1)) { | ||
$arguments->setNode($ind - 1, new \Twig_Node_Expression_Array(array(), $node->getLine())); | ||
} | ||
|
||
$arguments->setNode($ind, $this->domain); | ||
} | ||
} elseif ($node instanceof TransNode) { | ||
if (null === $node->getNode('domain')) { | ||
$node->setNode('domain', $this->domain); | ||
} | ||
} | ||
|
||
return $node; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env) | ||
{ | ||
return $node; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getPriority() | ||
{ | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/Symfony/Bridge/Twig/TokenParser/TransDefaultDomainTokenParser.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Twig\TokenParser; | ||
|
||
use Symfony\Bridge\Twig\Node\TransDefaultDomainNode; | ||
|
||
/** | ||
* Token Parser for the 'trans_default_domain' tag. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
class TransDefaultDomainTokenParser extends \Twig_TokenParser | ||
{ | ||
/** | ||
* Parses a token and returns a node. | ||
* | ||
* @param \Twig_Token $token A Twig_Token instance | ||
* | ||
* @return \Twig_NodeInterface A Twig_NodeInterface instance | ||
*/ | ||
public function parse(\Twig_Token $token) | ||
{ | ||
$expr = $this->parser->getExpressionParser()->parseExpression(); | ||
|
||
$this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE); | ||
|
||
return new TransDefaultDomainNode($expr, $token->getLine(), $this->getTag()); | ||
} | ||
|
||
/** | ||
* Gets the tag name associated with this token parser. | ||
* | ||
* @return string The tag name | ||
*/ | ||
public function getTag() | ||
{ | ||
return 'trans_default_domain'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters