Document   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 68
rs 10
c 1
b 0
f 0
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B hasIncludes() 0 35 11
A assertMissingDelimiter() 0 2 1
A blockDelimiter() 0 4 1
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   https://www.platine-php.com
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
     * Create new instance
64
     * @param array<int, string> $tokens
65
     * @param Parser $parser
66
     */
67
    public function __construct(array &$tokens, Parser $parser)
68
    {
69
        $this->parser = $parser;
70
        $this->parse($tokens);
71
    }
72
73
    /**
74
     * Check for cached includes; if there are - do not use cache
75
     * @return bool
76
     */
77
    public function hasIncludes(): bool
78
    {
79
        $seenExtends = false;
80
        $seenBlock = false;
81
82
        foreach ($this->nodeList as $token) {
83
            if ($token instanceof ExtendsTag) {
84
                $seenExtends = true;
85
            } elseif ($token instanceof BlockTag) {
86
                $seenBlock = true;
87
            }
88
        }
89
90
        /*
91
        * We try to keep the base templates in cache (that not extend anything).
92
        *
93
        * At the same time if we re-render all other blocks we see, we avoid most
94
        * if not all related caching quirks. This may be suboptimal.
95
        */
96
        if ($seenBlock && !$seenExtends) {
97
            return true;
98
        }
99
100
        foreach ($this->nodeList as $token) {
101
            // check any of the tokens for includes
102
            if ($token instanceof IncludeTag && $token->hasIncludes()) {
103
                return true;
104
            }
105
106
            if ($token instanceof ExtendsTag && $token->hasIncludes()) {
107
                return true;
108
            }
109
        }
110
111
        return false;
112
    }
113
114
    /**
115
    * {@inheritdoc}
116
    */
117
    protected function blockDelimiter(): string
118
    {
119
        //There isn't a real delimiter
120
        return '';
121
    }
122
123
    /**
124
    * {@inheritdoc}
125
    */
126
    protected function assertMissingDelimiter(): void
127
    {
128
        // Document blocks don't need to be
129
        // terminated since they are not actually opened
130
    }
131
}
132