Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Template;
|
Chris@0
|
4
|
Chris@0
|
5 /**
|
Chris@0
|
6 * A class that defines the Twig 'trans' token parser for Drupal.
|
Chris@0
|
7 *
|
Chris@0
|
8 * The token parser converts a token stream created from template source
|
Chris@0
|
9 * code into an Abstract Syntax Tree (AST). The AST will later be compiled
|
Chris@0
|
10 * into PHP code usable for runtime execution of the template.
|
Chris@0
|
11 *
|
Chris@0
|
12 * @see \Twig_TokenParser
|
Chris@0
|
13 * @see http://twig.sensiolabs.org/doc/extensions/i18n.html
|
Chris@0
|
14 * @see https://github.com/fabpot/Twig-extensions
|
Chris@0
|
15 */
|
Chris@0
|
16 class TwigTransTokenParser extends \Twig_TokenParser {
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * {@inheritdoc}
|
Chris@0
|
20 */
|
Chris@0
|
21 public function parse(\Twig_Token $token) {
|
Chris@0
|
22 $lineno = $token->getLine();
|
Chris@0
|
23 $stream = $this->parser->getStream();
|
Chris@0
|
24 $body = NULL;
|
Chris@0
|
25 $options = NULL;
|
Chris@0
|
26 $count = NULL;
|
Chris@0
|
27 $plural = NULL;
|
Chris@0
|
28
|
Chris@0
|
29 if (!$stream->test(\Twig_Token::BLOCK_END_TYPE) && $stream->test(\Twig_Token::STRING_TYPE)) {
|
Chris@0
|
30 $body = $this->parser->getExpressionParser()->parseExpression();
|
Chris@0
|
31 }
|
Chris@0
|
32 if (!$stream->test(\Twig_Token::BLOCK_END_TYPE) && $stream->test(\Twig_Token::NAME_TYPE, 'with')) {
|
Chris@0
|
33 $stream->next();
|
Chris@0
|
34 $options = $this->parser->getExpressionParser()->parseExpression();
|
Chris@0
|
35 }
|
Chris@0
|
36 if (!$body) {
|
Chris@0
|
37 $stream->expect(\Twig_Token::BLOCK_END_TYPE);
|
Chris@0
|
38 $body = $this->parser->subparse([$this, 'decideForFork']);
|
Chris@0
|
39 if ('plural' === $stream->next()->getValue()) {
|
Chris@0
|
40 $count = $this->parser->getExpressionParser()->parseExpression();
|
Chris@0
|
41 $stream->expect(\Twig_Token::BLOCK_END_TYPE);
|
Chris@0
|
42 $plural = $this->parser->subparse([$this, 'decideForEnd'], TRUE);
|
Chris@0
|
43 }
|
Chris@0
|
44 }
|
Chris@0
|
45
|
Chris@0
|
46 $stream->expect(\Twig_Token::BLOCK_END_TYPE);
|
Chris@0
|
47
|
Chris@0
|
48 $this->checkTransString($body, $lineno);
|
Chris@0
|
49
|
Chris@0
|
50 $node = new TwigNodeTrans($body, $plural, $count, $options, $lineno, $this->getTag());
|
Chris@0
|
51
|
Chris@0
|
52 return $node;
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@0
|
55 /**
|
Chris@0
|
56 * Detect a 'plural' switch or the end of a 'trans' tag.
|
Chris@0
|
57 */
|
Chris@0
|
58 public function decideForFork($token) {
|
Chris@0
|
59 return $token->test(['plural', 'endtrans']);
|
Chris@0
|
60 }
|
Chris@0
|
61
|
Chris@0
|
62 /**
|
Chris@0
|
63 * Detect the end of a 'trans' tag.
|
Chris@0
|
64 */
|
Chris@0
|
65 public function decideForEnd($token) {
|
Chris@0
|
66 return $token->test('endtrans');
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 /**
|
Chris@0
|
70 * {@inheritdoc}
|
Chris@0
|
71 */
|
Chris@0
|
72 public function getTag() {
|
Chris@0
|
73 return 'trans';
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 /**
|
Chris@0
|
77 * Ensure that any nodes that are parsed are only of allowed types.
|
Chris@0
|
78 *
|
Chris@0
|
79 * @param \Twig_Node $body
|
Chris@0
|
80 * The expression to check.
|
Chris@0
|
81 * @param int $lineno
|
Chris@0
|
82 * The source line.
|
Chris@0
|
83 *
|
Chris@0
|
84 * @throws \Twig_Error_Syntax
|
Chris@0
|
85 */
|
Chris@0
|
86 protected function checkTransString(\Twig_Node $body, $lineno) {
|
Chris@0
|
87 foreach ($body as $node) {
|
Chris@0
|
88 if (
|
Chris@0
|
89 $node instanceof \Twig_Node_Text
|
Chris@0
|
90 ||
|
Chris@0
|
91 ($node instanceof \Twig_Node_Print && $node->getNode('expr') instanceof \Twig_Node_Expression_Name)
|
Chris@0
|
92 ||
|
Chris@0
|
93 ($node instanceof \Twig_Node_Print && $node->getNode('expr') instanceof \Twig_Node_Expression_GetAttr)
|
Chris@0
|
94 ||
|
Chris@0
|
95 ($node instanceof \Twig_Node_Print && $node->getNode('expr') instanceof \Twig_Node_Expression_Filter)
|
Chris@0
|
96 ) {
|
Chris@0
|
97 continue;
|
Chris@0
|
98 }
|
Chris@0
|
99 throw new \Twig_Error_Syntax(sprintf('The text to be translated with "trans" can only contain references to simple variables'), $lineno);
|
Chris@0
|
100 }
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 }
|