Passed
Push — master ( 6afa72...f96468 )
by M. Mikkel
04:08
created

CacheFlagTokenParser::decideCacheEnd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: mmikkel
5
 * Date: 14/07/2018
6
 * Time: 15:43
7
 */
8
9
namespace mmikkel\cacheflag\twigextensions;
10
11
use mmikkel\cacheflag\twigextensions\CacheFlagNode;
12
13
use Craft;
14
15
use Twig\Parser;
16
use Twig\Token;
17
use Twig\TokenParser\AbstractTokenParser;
18
19
/**
20
 * Class CacheFlagTokenParser
21
 * @package mmikkel\cacheflag\twigextensions
22
 */
23
class CacheFlagTokenParser extends AbstractTokenParser
24
{
25
    // Public Methods
26
    // =========================================================================
27
28
    /**
29
     * @return string
30
     */
31
    public function getTag(): string
32
    {
33
        return 'cacheflag';
34
    }
35
36
    /**
37
     * @inheritdoc
38
     */
39
    public function parse(Token $token)
40
    {
41
        $lineno = $token->getLine();
42
        /** @var Parser $parser */
43
        $parser = $this->parser;
44
        $stream = $parser->getStream();
45
46
        $nodes = [];
47
48
        $attributes = [
49
            'global' => false,
50
            'durationNum' => null,
51
            'durationUnit' => null,
52
            'elements' => false,
53
        ];
54
55
        if ($stream->test(Token::NAME_TYPE, 'flagged')) {
56
            $stream->next();
57
            $nodes['flags'] = $parser->getExpressionParser()->parseExpression();
58
        }
59
60
        if ($stream->test(Token::NAME_TYPE, 'with')) {
61
            $stream->next();
62
            $stream->expect(Token::NAME_TYPE, 'elements');
63
            $attributes['elements'] = true;
64
        }
65
66
        if ($stream->test(Token::NAME_TYPE, 'globally')) {
67
            $attributes['global'] = true;
68
            $stream->next();
69
        }
70
71
        if ($stream->test(Token::NAME_TYPE, 'using')) {
72
            $stream->next();
73
            $stream->expect(Token::NAME_TYPE, 'key');
74
            $nodes['key'] = $parser->getExpressionParser()->parseExpression();
75
        }
76
77
        if ($stream->test(Token::NAME_TYPE, 'for')) {
78
            $stream->next();
79
            $attributes['durationNum'] = $stream->expect(Token::NUMBER_TYPE)->getValue();
80
            $attributes['durationUnit'] = $stream->expect(Token::NAME_TYPE,
81
                [
82
                    'sec',
83
                    'secs',
84
                    'second',
85
                    'seconds',
86
                    'min',
87
                    'mins',
88
                    'minute',
89
                    'minutes',
90
                    'hour',
91
                    'hours',
92
                    'day',
93
                    'days',
94
                    'fortnight',
95
                    'fortnights',
96
                    'forthnight',
97
                    'forthnights',
98
                    'month',
99
                    'months',
100
                    'year',
101
                    'years',
102
                    'week',
103
                    'weeks'
104
                ])->getValue();
105
        } else if ($stream->test(Token::NAME_TYPE, 'until')) {
106
            $stream->next();
107
            $nodes['expiration'] = $parser->getExpressionParser()->parseExpression();
108
        }
109
110
        if ($stream->test(Token::NAME_TYPE, 'if')) {
111
            $stream->next();
112
            $nodes['conditions'] = $parser->getExpressionParser()->parseExpression();
113
        } else if ($stream->test(Token::NAME_TYPE, 'unless')) {
114
            $stream->next();
115
            $nodes['ignoreConditions'] = $parser->getExpressionParser()->parseExpression();
116
        }
117
118
        $stream->expect(Token::BLOCK_END_TYPE);
119
        $nodes['body'] = $parser->subparse([
120
            $this,
121
            'decideCacheEnd'
122
        ], true);
123
        $stream->expect(Token::BLOCK_END_TYPE);
124
125
        return new CacheFlagNode($nodes, $attributes, $lineno, $this->getTag());
126
    }
127
128
    /**
129
     * @param Token $token
130
     * @return bool
131
     */
132
    public function decideCacheEnd(Token $token): bool
133
    {
134
        return $token->test('endcacheflag');
135
    }
136
}
137