Passed
Push — develop ( 067c9c...bf1d25 )
by nguereza
04:45
created

Document::assertMissingDelimiter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * Platine Template
5
 *
6
 * Platine Template is a template engine that has taken a lot of inspiration from Django.
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine Template
11
 * Copyright (c) 2014 Guz Alexander, http://guzalexander.com
12
 * Copyright (c) 2011, 2012 Harald Hanek, http://www.delacap.com
13
 * Copyright (c) 2006 Mateo Murphy
14
 *
15
 * Permission is hereby granted, free of charge, to any person obtaining a copy
16
 * of this software and associated documentation files (the "Software"), to deal
17
 * in the Software without restriction, including without limitation the rights
18
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19
 * copies of the Software, and to permit persons to whom the Software is
20
 * furnished to do so, subject to the following conditions:
21
 *
22
 * The above copyright notice and this permission notice shall be included in all
23
 * copies or substantial portions of the Software.
24
 *
25
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31
 * SOFTWARE.
32
 */
33
34
/**
35
 *  @file Document.php
36
 *
37
 *  The Template Document class
38
 *
39
 *  @package    Platine\Template\Parser
40
 *  @author Platine Developers Team
41
 *  @copyright  Copyright (c) 2020
42
 *  @license    http://opensource.org/licenses/MIT  MIT License
43
 *  @link   http://www.iacademy.cf
44
 *  @version 1.0.0
45
 *  @filesource
46
 */
47
48
declare(strict_types=1);
49
50
namespace Platine\Template\Parser;
51
52
use Platine\Template\Tag\BlockTag;
53
use Platine\Template\Tag\ExtendsTag;
54
use Platine\Template\Tag\IncludeTag;
55
56
/**
57
 * Class Document
58
 * @package Platine\Template\Parser
59
 */
60
class Document extends AbstractBlock
61
{
62
63
    /**
64
     * Create new instance
65
     * @param array<int, string> $tokens
66
     * @param Parser $parser
67
     */
68
    public function __construct(array &$tokens, Parser $parser)
69
    {
70
        $this->parser = $parser;
71
72
        $this->parse($tokens);
73
    }
74
75
    /**
76
     * Check for cached includes; if there are - do not use cache
77
     * @return bool
78
     */
79
    public function hasIncludes(): bool
80
    {
81
        $seenExtends = false;
82
        $seenBlock = false;
83
84
        foreach ($this->nodeList as $token) {
85
            if ($token instanceof ExtendsTag) {
86
                $seenExtends = true;
87
            } elseif ($token instanceof BlockTag) {
88
                $seenBlock = true;
89
            }
90
        }
91
92
        /*
93
        * We try to keep the base templates in cache (that not extend anything).
94
        *
95
        * At the same time if we re-render all other blocks we see, we avoid most
96
        * if not all related caching quirks. This may be suboptimal.
97
        */
98
        if ($seenBlock && !$seenExtends) {
99
            return true;
100
        }
101
102
        foreach ($this->nodeList as $token) {
103
            // check any of the tokens for includes
104
            if ($token instanceof IncludeTag && $token->hasIncludes()) {
105
                return true;
106
            }
107
108
            if ($token instanceof ExtendsTag && $token->hasIncludes()) {
109
                return true;
110
            }
111
        }
112
113
        return false;
114
    }
115
116
    /**
117
    * {@inheritdoc}
118
    */
119
    protected function blockDelimiter(): string
120
    {
121
        //There isn't a real delimiter
122
        return '';
123
    }
124
125
    /**
126
    * {@inheritdoc}
127
    */
128
    protected function assertMissingDelimiter(): void
129
    {
130
        // Document blocks don't need to be
131
        // terminated since they are not actually opened
132
    }
133
}
134