PartialCacheCollector::getTemplateCache()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace LeKoala\DebugBar\Collector;
4
5
use LeKoala\DebugBar\DebugBar;
6
use SilverStripe\Control\Director;
7
use DebugBar\DataCollector\Renderable;
8
use DebugBar\DataCollector\DataCollector;
9
use DebugBar\DataCollector\AssetProvider;
10
11
/**
12
 * Collects data about the partial cache hits and misses during a SilverStripe request
13
 */
14
class PartialCacheCollector extends DataCollector implements Renderable, AssetProvider
15
{
16
    /**
17
     * Contains a list of all partial caches found.
18
     * @var array
19
     */
20
    protected static $templateCache = [];
21
22
    public function getName()
23
    {
24
        return 'partial-cache';
25
    }
26
27
    public function getAssets()
28
    {
29
        // This depends on ConfigCollector assets
30
        return [
31
            'base_path' => '/' . DebugBar::moduleResource('javascript')->getRelativePath(),
32
            'base_url' => Director::makeRelative(DebugBar::moduleResource('javascript')->getURL()),
33
            'css' => 'config/widget.css',
34
            'js' => 'config/widget.js'
35
        ];
36
    }
37
38
    public function collect()
39
    {
40
        $result = self::getTemplateCache();
41
        return [
42
            'count' => count($result),
43
            'calls' => $result
44
        ];
45
    }
46
47
    public function getWidgets()
48
    {
49
        $widgets = [
50
            'Partial Cache' => [
51
                'icon' => 'cube',
52
                'widget' => 'PhpDebugBar.Widgets.ConfigWidget',
53
                'map' => 'partial-cache.calls',
54
                'default' => '{}'
55
            ]
56
        ];
57
        if (count(self::getTemplateCache()) > 0) {
58
            $widgets['Partial Cache:badge'] = [
59
                'map' => 'partial-cache.count',
60
                'default' => 0
61
            ];
62
        }
63
64
        return $widgets;
65
    }
66
67
    /**
68
     * @return array
69
     */
70
    public static function getTemplateCache()
71
    {
72
        return (self::$templateCache) ?: [];
73
    }
74
75
    /**
76
     * Adds an item to the templateCache array
77
     * @param string $key
78
     * @param array $item
79
     */
80
    public static function addTemplateCache($key, $item)
81
    {
82
        self::$templateCache[$key] = $item;
83
    }
84
85
    /**
86
     * @param array $templateCache
87
     */
88
    public static function setTemplateCache($templateCache)
89
    {
90
        self::$templateCache = $templateCache;
91
    }
92
}
93