comparison core/lib/Drupal/Core/Template/TwigTransTokenParser.php @ 0:4c8ae668cc8c

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