Completed
Push — master ( c8fd93...a9ab00 )
by Georges
01:49
created

CacheCollector::setTwigCacheBlock()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
3
/**
4
 *
5
 * This file is part of phpFastCache.
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For full copyright and license information, please see the docs/CREDITS.txt file.
10
 *
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 * @author PastisD https://github.com/PastisD
13
 * @author Khoa Bui (khoaofgod)  <[email protected]> http://www.phpfastcache.com
14
 *
15
 */
16
17
namespace phpFastCache\Bundle\DataCollector;
18
19
use phpFastCache\Api as phpFastCacheApi;
20
use phpFastCache\Bundle\phpFastCacheBundle;
21
use phpFastCache\Bundle\Service\Cache;
22
use phpFastCache\CacheManager;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\HttpFoundation\Response;
25
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
26
27
class CacheCollector extends DataCollector
28
{
29
    /**
30
     * @var \phpFastCache\Bundle\Service\Cache
31
     */
32
    private $cache;
33
34
    /**
35
     * @var array
36
     */
37
    private $twig_cache_blocks = [];
38
39
    /**
40
     * CacheCollector constructor.
41
     *
42
     * @param \phpFastCache\Bundle\Service\Cache $cache
43
     */
44
    public function __construct(Cache $cache)
45
    {
46
        $this->cache = $cache;
47
    }
48
49
    /**
50
     * @param \Symfony\Component\HttpFoundation\Request $request
51
     * @param \Symfony\Component\HttpFoundation\Response $response
52
     * @param \Exception|null $exception
53
     */
54
    public function collect(Request $request, Response $response, \Exception $exception = null)
55
    {
56
        $size = 0;
57
        $stats = [];
58
        $instances = [];
59
        $driverUsed = [];
60
61
        /** @var  $cache */
62
        foreach ($this->cache->getInstances() as $instanceName => $cache) {
63
            if ($cache->getStats()->getSize()) {
64
                $size += $cache->getStats()->getSize();
65
            }
66
            $stats[ $instanceName ] = $cache->getStats();
67
            $instances[ $instanceName ] = [
68
              'driverName' => $cache->getDriverName(),
69
              'driverConfig' => $cache->getConfig(),
70
            ];
71
            $driverUsed[ $cache->getDriverName() ] = get_class($cache);
72
        }
73
74
        $this->data = [
75
          'twigCacheBlocks' => $this->twig_cache_blocks,
76
          'apiVersion' => phpFastCacheApi::getVersion(),
77
          'bundleVersion' => phpFastCacheBundle::VERSION,
78
          'apiChangelog' => phpFastCacheApi::getChangelog(),
79
          'driverUsed' => $driverUsed,
80
          'instances' => $instances,
81
          'stats' => $stats,
82
          'size' => $size,
83
          'hits' => [
84
            'read' => (int) CacheManager::$ReadHits,
85
            'write' => (int) CacheManager::$WriteHits,
86
          ],
87
          'coreConfig' => [
88
            'namespacePath' => CacheManager::getNamespacePath(),
89
          ],
90
          'projectConfig' => [
91
            'twig_driver' => $this->cache->getConfig()['twig_driver'],
92
            'twig_block_debug' => $this->cache->getConfig()['twig_block_debug'],
93
          ],
94
        ];
95
    }
96
97
    /**
98
     * @return mixed
99
     */
100
    public function getStats()
101
    {
102
        return $this->data[ 'stats' ];
103
    }
104
105
    /**
106
     * @return mixed
107
     */
108
    public function getInstances()
109
    {
110
        return $this->data[ 'instances' ];
111
    }
112
113
    /**
114
     * @return mixed
115
     */
116
    public function getDriverUsed()
117
    {
118
        return $this->data[ 'driverUsed' ];
119
    }
120
121
    /**
122
     * @return mixed
123
     */
124
    public function getHits()
125
    {
126
        return $this->data[ 'hits' ];
127
    }
128
129
    /**
130
     * @return mixed
131
     */
132
    public function getSize()
133
    {
134
        return $this->data[ 'size' ];
135
    }
136
137
    /**
138
     * @return mixed
139
     */
140
    public function getCoreConfig()
141
    {
142
        return $this->data[ 'coreConfig' ];
143
    }
144
145
    /**
146
     * @return mixed
147
     */
148
    public function getProjectConfig()
149
    {
150
        return $this->data[ 'projectConfig' ];
151
    }
152
153
    /**
154
     * @return mixed
155
     */
156
    public function getApiVersion()
157
    {
158
        return $this->data[ 'apiVersion' ];
159
    }
160
161
    /**
162
     * @return mixed
163
     */
164
    public function getBundleVersion()
165
    {
166
        return $this->data[ 'bundleVersion' ];
167
    }
168
169
    /**
170
     * @return mixed
171
     */
172
    public function getApiChangelog()
173
    {
174
        return $this->data[ 'apiChangelog' ];
175
    }
176
177
    /**
178
     * @param string $blockName
179
     * @param array $cacheBlock
180
     * @return $this
181
     */
182
    public function setTwigCacheBlock($blockName, array $cacheBlock)
183
    {
184
        if(isset($this->twig_cache_blocks[$blockName])){
185
            $this->twig_cache_blocks[$blockName] = array_merge($this->twig_cache_blocks[$blockName], $cacheBlock);
186
        }else{
187
            $this->twig_cache_blocks[$blockName] = $cacheBlock;
188
        }
189
190
191
        return $this;
192
    }
193
194
    /**
195
     * @return array
196
     */
197
    public function getTwigCacheBlocks()
198
    {
199
        return $this->data[ 'twigCacheBlocks' ];
200
    }
201
202
    /**
203
     * @return string
204
     */
205
    public function getName()
206
    {
207
        return 'phpfastcache';
208
    }
209
}