Completed
Push — master ( afd294...a9b633 )
by Romain
02:36
created

CacheService::getCacheManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/*
3
 * 2017 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 Configuration Object project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\ConfigurationObject\Core\Service;
15
16
use TYPO3\CMS\Core\Cache\Backend\FileBackend;
17
use TYPO3\CMS\Core\Cache\CacheManager;
18
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
19
use TYPO3\CMS\Core\Cache\Frontend\VariableFrontend;
20
use TYPO3\CMS\Core\SingletonInterface;
21
use TYPO3\CMS\Core\Utility\GeneralUtility;
22
23
class CacheService implements SingletonInterface
24
{
25
    const CACHE_IDENTIFIER = 'cache_configuration_object';
26
    const CACHE_TAG_DYNAMIC_CACHE = 'dynamic-cache';
27
28
    /**
29
     * Options for the internal cache.
30
     *
31
     * @var array
32
     */
33
    protected $cacheOptions = [
34
        'backend'  => FileBackend::class,
35
        'frontend' => VariableFrontend::class,
36
        'groups'   => ['all', 'system']
37
    ];
38
39
    /**
40
     * Function called from `ext_localconf` file which will register the
41
     * internal cache earlier.
42
     *
43
     * @internal
44
     */
45
    public function registerInternalCache()
46
    {
47
        $this->getCacheManager()
48
            ->setCacheConfigurations([self::CACHE_IDENTIFIER => $this->cacheOptions]);
49
    }
50
51
    /**
52
     * This function will take care of initializing all caches that were defined
53
     * previously  by the `CacheService` which allows dynamic caches to be used
54
     * for every configuration object type.
55
     *
56
     * @see \Romm\ConfigurationObject\Service\Items\Cache\CacheService::initialize()
57
     * @internal
58
     */
59
    public function registerDynamicCaches()
60
    {
61
        $dynamicCaches = $this->getCache()->getByTag(self::CACHE_TAG_DYNAMIC_CACHE);
62
63
        foreach ($dynamicCaches as $cacheData) {
64
            $identifier = $cacheData['identifier'];
65
            $options = $cacheData['options'];
66
67
            $this->registerCacheInternal($identifier, $options);
68
        }
69
    }
70
71
    /**
72
     * Registers a new dynamic cache: the cache will be added to the cache
73
     * manager after this function is executed. Its configuration will also be
74
     * remembered so the cache will be registered properly during the cache
75
     * framework initialization in further requests.
76
     *
77
     * @param string $identifier
78
     * @param array  $options
79
     */
80
    public function registerDynamicCache($identifier, array $options)
81
    {
82
        if (false === $this->getCache()->has($identifier)) {
83
            $this->getCache()->set(
84
                $identifier,
85
                [
86
                    'identifier' => $identifier,
87
                    'options'    => $options
88
                ],
89
                [self::CACHE_TAG_DYNAMIC_CACHE]
90
            );
91
        }
92
93
        $this->registerCacheInternal($identifier, $options);
94
    }
95
96
    /**
97
     * @param string $identifier
98
     * @param array  $options
99
     */
100
    protected function registerCacheInternal($identifier, array $options)
101
    {
102
        $cacheManager = $this->getCacheManager();
103
104
        if (false === $cacheManager->hasCache($identifier)) {
105
            $cacheManager->setCacheConfigurations([$identifier => $options]);
106
        }
107
    }
108
109
    /**
110
     * @return FrontendInterface
111
     */
112
    protected function getCache()
113
    {
114
        return $this->getCacheManager()->getCache(self::CACHE_IDENTIFIER);
115
    }
116
117
    /**
118
     * @return CacheManager
119
     */
120
    protected function getCacheManager()
121
    {
122
        /** @var CacheManager $cacheManager */
123
        $cacheManager = GeneralUtility::makeInstance(CacheManager::class);
124
125
        return $cacheManager;
126
    }
127
}
128