CacheCollector::getCoreConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
declare(strict_types=1);
17
18
namespace Phpfastcache\Bundle\DataCollector;
19
20
use Phpfastcache\Api as PhpfastcacheApi;
21
use Phpfastcache\Bundle\PhpfastcacheBundle;
22
use Phpfastcache\Bundle\Service\Phpfastcache;
23
use Phpfastcache\CacheManager;
24
use Symfony\Component\HttpFoundation\Request;
25
use Symfony\Component\HttpFoundation\Response;
26
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
27
28
class CacheCollector extends DataCollector
29
{
30
    /**
31
     * @var \Phpfastcache\Bundle\Service\Phpfastcache
32
     */
33
    private $phpfastcache;
34
35
    /**
36
     * @var array
37
     */
38
    private $twig_cache_blocks = [];
39
40
    /**
41
     * CacheCollector constructor.
42
     *
43
     * @param \Phpfastcache\Bundle\Service\Phpfastcache $phpfastcache
44
     */
45
    public function __construct(Phpfastcache $phpfastcache)
46
    {
47
        $this->phpfastcache = $phpfastcache;
48
    }
49
50
    /**
51
     * @param \Symfony\Component\HttpFoundation\Request $request
52
     * @param \Symfony\Component\HttpFoundation\Response $response
53
     * @param \Exception|null $exception
54
     */
55
    public function collect(Request $request, Response $response, \Exception $exception = null)
56
    {
57
        $size = 0;
58
        $stats = [];
59
        $instances = [];
60
        $driverUsed = [];
61
62
        /** @var  $cache */
63
        foreach ($this->phpfastcache->getInstances() as $instanceName => $cache) {
64
            if ($cache->getStats()->getSize()) {
65
                $size += $cache->getStats()->getSize();
66
            }
67
            $stats[ $instanceName ] = $cache->getStats();
68
            $instances[ $instanceName ] = [
69
              'driverName' => $cache->getDriverName(),
70
              'configClassName' => \get_class( $cache->getConfig()),
71
              'driverConfig' => $cache->getConfig()->toArray()
72
            ];
73
            $driverUsed[ $cache->getDriverName() ] = \get_class($cache);
74
        }
75
76
        $this->data = [
77
          'twigCacheBlocks' => $this->twig_cache_blocks,
78
          'apiVersion' => PhpfastcacheApi::getVersion(),
79
          'pfcVersion' => PhpfastcacheApi::getPhpFastCacheVersion(),
80
          'bundleVersion' => phpFastCacheBundle::VERSION,
81
          'apiChangelog' => PhpfastcacheApi::getChangelog(),
82
          'driverUsed' => $driverUsed,
83
          'instances' => $instances,
84
          'stats' => $stats,
85
          'size' => $size,
86
          'hits' => [
87
            'read' => (int) CacheManager::$ReadHits,
88
            'write' => (int) CacheManager::$WriteHits,
89
          ],
90
          'coreConfig' => [
91
            'driverList' => CacheManager::getDriverList(true),
92
            'namespacePath (deprecated)' => CacheManager::getNamespacePath(),
93
          ],
94
          'projectConfig' => [
95
            'twig_driver' => $this->phpfastcache->getConfig()['twig_driver'],
96
            'twig_block_debug' => $this->phpfastcache->getConfig()['twig_block_debug'],
97
          ],
98
        ];
99
    }
100
101
    /**
102
     * @return array
103
     */
104
    public function getStats(): array
105
    {
106
        return $this->data[ 'stats' ] ?? [];
107
    }
108
109
    /**
110
     * @return array
111
     */
112
    public function getInstances(): array
113
    {
114
        return $this->data[ 'instances' ];
115
    }
116
117
    /**
118
     * @return array
119
     */
120
    public function getDriverUsed(): array
121
    {
122
        return $this->data[ 'driverUsed' ] ?? [];
123
    }
124
125
    /**
126
     * @return array
127
     */
128
    public function getHits(): array
129
    {
130
        return $this->data[ 'hits' ] ?? [];
131
    }
132
133
    /**
134
     * @return int
135
     */
136
    public function getSize(): int
137
    {
138
        return $this->data[ 'size' ] ?? 0;
139
    }
140
141
    /**
142
     * @return array
143
     */
144
    public function getCoreConfig(): array
145
    {
146
        return $this->data[ 'coreConfig' ] ?? [];
147
    }
148
149
    /**
150
     * @return array
151
     */
152
    public function getProjectConfig(): array
153
    {
154
        return $this->data[ 'projectConfig' ] ?? [];
155
    }
156
157
    /**
158
     * @return string
159
     */
160
    public function getApiVersion(): string
161
    {
162
        return $this->data[ 'apiVersion' ];
163
    }
164
165
    /**
166
     * @return string
167
     */
168
    public function getPfcVersion(): string
169
    {
170
        return $this->data[ 'pfcVersion' ];
171
    }
172
173
    /**
174
     * @return string
175
     */
176
    public function getBundleVersion(): string
177
    {
178
        return $this->data[ 'bundleVersion' ];
179
    }
180
181
    /**
182
     * @return string
183
     */
184
    public function getApiChangelog(): string
185
    {
186
        return $this->data[ 'apiChangelog' ] ?? '';
187
    }
188
189
    /**
190
     * @param string $blockName
191
     * @param array $cacheBlock
192
     * @return $this
193
     */
194
    public function setTwigCacheBlock($blockName, array $cacheBlock): self
195
    {
196
        if(isset($this->twig_cache_blocks[$blockName])){
197
            $this->twig_cache_blocks[$blockName] = \array_merge($this->twig_cache_blocks[$blockName], $cacheBlock);
198
        }else{
199
            $this->twig_cache_blocks[$blockName] = $cacheBlock;
200
        }
201
202
203
        return $this;
204
    }
205
206
    /**
207
     * @return array
208
     */
209
    public function getTwigCacheBlocks(): array
210
    {
211
        return $this->data[ 'twigCacheBlocks' ] ?? [];
212
    }
213
214
    /**
215
     * @inheritdoc
216
     */
217
    public function getName(): string
218
    {
219
        return 'phpfastcache';
220
    }
221
222
    /**
223
     * @inheritdoc
224
     */
225
    public function reset()
226
    {
227
        $this->data = [];
228
        $this->twig_cache_blocks = [];
229
    }
230
}