Completed
Pull Request — master (#47)
by Robbie
01:30
created

TemplateParserProxy   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 63
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A compileString() 0 10 2
A getTemplatesUsed() 0 4 1
A getCached() 0 4 1
A trackTemplateUsed() 0 4 1
1
<?php
2
3
namespace LeKoala\DebugBar\Proxy;
4
5
use LeKoala\DebugBar\DebugBar;
6
use SilverStripe\View\SSTemplateParser;
7
8
/**
9
 * The template parser proxy will monitor the templates that are used during a page request. Since the
10
 * use of the template parser is behind cache checks, this will only execute during a cache flush.
11
 */
12
class TemplateParserProxy extends SSTemplateParser
13
{
14
    /**
15
     * Tracks all templates used in the current request
16
     *
17
     * @var array
18
     */
19
    protected static $allTemplates = array();
20
21
    /**
22
     * Whether the class has been used, meaning whether the page has been cached
23
     *
24
     * @var boolean
25
     */
26
    protected static $cached = true;
27
28
    /**
29
     * Overloaded to track all templates used in the current request
30
     *
31
     * {@inheritDoc}
32
     */
33
    public function compileString($string, $templateName = '', $includeDebuggingComments = false, $topTemplate = true)
34
    {
35
        static::$cached = false;
36
37
        if (DebugBar::config()->force_proxy) {
38
            static::trackTemplateUsed($templateName);
39
        }
40
41
        return parent::compileString($string, $templateName, $includeDebuggingComments, $topTemplate);
42
    }
43
44
    /**
45
     * Get the templates used in the current request and the number of times they were called
46
     *
47
     * @return array
48
     */
49
    public static function getTemplatesUsed()
50
    {
51
        return static::$allTemplates;
52
    }
53
54
    /**
55
     * Determines whether the template rendering is cached or not based on whether the compileString method has been
56
     * called at any point.
57
     *
58
     * @return bool
59
     */
60
    public static function getCached()
61
    {
62
        return static::$cached;
63
    }
64
65
    /**
66
     * Remove the base path from a template file path and track its use
67
     *
68
     * @param string $templateName
69
     */
70
    protected static function trackTemplateUsed($templateName)
71
    {
72
        static::$allTemplates[] = str_ireplace(BASE_PATH, '', $templateName);
73
    }
74
}
75