|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Superdesk Web Publisher Templates System. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright 2015 Sourcefabric z.ú. and contributors. |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please see the |
|
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* @copyright 2015 Sourcefabric z.ú |
|
12
|
|
|
* @license http://www.superdesk.org/license |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace SWP\Component\TemplatesSystem\Twig\TokenParser; |
|
16
|
|
|
|
|
17
|
|
|
use SWP\Component\TemplatesSystem\Twig\Node\GimmeListNode; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Parser for gimme/endgimme blocks. |
|
21
|
|
|
*/ |
|
22
|
|
|
class GimmeListTokenParser extends \Twig\TokenParser\AbstractTokenParser |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @param \Twig\Token $token |
|
26
|
|
|
* |
|
27
|
|
|
* @return bool |
|
28
|
|
|
*/ |
|
29
|
|
|
public function decideGimmeListEnd(\Twig\Token $token) |
|
30
|
|
|
{ |
|
31
|
|
|
return $token->test('endgimmelist'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function decideGimmeListFork(\Twig\Token $token) |
|
35
|
|
|
{ |
|
36
|
|
|
return $token->test(['else', 'endgimmelist']); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getTag() |
|
43
|
|
|
{ |
|
44
|
|
|
return 'gimmelist'; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
|
|
public function parse(\Twig\Token $token) |
|
51
|
|
|
{ |
|
52
|
|
|
$lineno = $token->getLine(); |
|
53
|
|
|
$stream = $this->parser->getStream(); |
|
54
|
|
|
|
|
55
|
|
|
$variable = $this->parser->getExpressionParser()->parseAssignmentExpression(); |
|
56
|
|
|
$stream->expect(\Twig\Token::NAME_TYPE, 'from'); |
|
57
|
|
|
$collectionType = $this->parser->getExpressionParser()->parseAssignmentExpression(); |
|
58
|
|
|
|
|
59
|
|
|
$collectionFilters = null; |
|
60
|
|
|
if ($stream->test(\Twig\Token::PUNCTUATION_TYPE, '|')) { |
|
61
|
|
|
$collectionFilters = $this->parser->getExpressionParser()->parsePostfixExpression($collectionType); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$withParameters = null; |
|
65
|
|
|
if ($stream->nextIf(\Twig\Token::NAME_TYPE, 'with')) { |
|
66
|
|
|
$withParameters = $this->parser->getExpressionParser()->parseExpression(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$withoutParameters = null; |
|
70
|
|
|
if ($stream->nextIf(\Twig\Token::NAME_TYPE, 'without')) { |
|
71
|
|
|
$withoutParameters = $this->parser->getExpressionParser()->parseExpression(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$ignoreContext = null; |
|
75
|
|
View Code Duplication |
if ($stream->nextIf(\Twig\Token::NAME_TYPE, 'ignoreContext')) { |
|
76
|
|
|
if ($stream->test(\Twig\Token::PUNCTUATION_TYPE, '[')) { |
|
77
|
|
|
$ignoreContext = $this->parser->getExpressionParser()->parseExpression(); |
|
78
|
|
|
} else { |
|
79
|
|
|
$ignoreContext = new \Twig\Node\Expression\ArrayExpression([], $token->getLine()); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$ifExpression = null; |
|
84
|
|
|
if ($stream->nextIf(\Twig\Token::NAME_TYPE, 'if')) { |
|
85
|
|
|
$ifExpression = $this->parser->getExpressionParser()->parseExpression(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$stream->expect(\Twig\Token::BLOCK_END_TYPE); |
|
89
|
|
|
$body = $this->parser->subparse([$this, 'decideGimmeListFork']); |
|
90
|
|
|
if ('else' === $stream->next()->getValue()) { |
|
91
|
|
|
$stream->expect(\Twig\Token::BLOCK_END_TYPE); |
|
92
|
|
|
$else = $this->parser->subparse([$this, 'decideGimmeListEnd'], true); |
|
93
|
|
|
} else { |
|
94
|
|
|
$else = null; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$stream->expect(\Twig\Token::BLOCK_END_TYPE); |
|
98
|
|
|
|
|
99
|
|
|
return new GimmeListNode( |
|
100
|
|
|
$variable, |
|
101
|
|
|
$collectionType, |
|
102
|
|
|
$collectionFilters, |
|
103
|
|
|
$withParameters, |
|
104
|
|
|
$withoutParameters, |
|
105
|
|
|
$ignoreContext, |
|
106
|
|
|
$ifExpression, |
|
107
|
|
|
$else, |
|
108
|
|
|
$body, |
|
109
|
|
|
$lineno, |
|
110
|
|
|
$this->getTag() |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|