1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AOE\Languagevisibility; |
4
|
|
|
|
5
|
|
|
/*************************************************************** |
6
|
|
|
* Copyright notice |
7
|
|
|
* |
8
|
|
|
* (c) 2016 AOE GmbH <[email protected]> |
9
|
|
|
* |
10
|
|
|
* All rights reserved |
11
|
|
|
* |
12
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
13
|
|
|
* free software; you can redistribute it and/or modify |
14
|
|
|
* it under the terms of the GNU General Public License as published by |
15
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
16
|
|
|
* (at your option) any later version. |
17
|
|
|
* |
18
|
|
|
* The GNU General Public License can be found at |
19
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
20
|
|
|
* |
21
|
|
|
* This script is distributed in the hope that it will be useful, |
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24
|
|
|
* GNU General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
27
|
|
|
***************************************************************/ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class CacheManager |
31
|
|
|
* @package AOE\Languagevisibility |
32
|
|
|
*/ |
33
|
|
|
class CacheManager { |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var boolean |
37
|
|
|
*/ |
38
|
|
|
protected static $useCache; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var boolean |
42
|
|
|
*/ |
43
|
|
|
protected static $enableCache = TRUE; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var CacheManager |
47
|
|
|
*/ |
48
|
|
|
protected static $instance; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
protected static $confArray = array(); |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
protected $cache = array(); |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Class constructor |
62
|
|
|
* |
63
|
|
|
* @return void |
|
|
|
|
64
|
|
|
*/ |
65
|
1 |
|
protected function __construct() { |
66
|
1 |
|
if (!isset(self::$useCache)) { |
67
|
1 |
|
self::$useCache = FALSE; |
68
|
|
|
|
69
|
1 |
|
if (empty(self::$confArray)) { |
70
|
1 |
|
self::$confArray = @unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['languagevisibility']); |
71
|
1 |
|
} |
72
|
|
|
|
73
|
1 |
|
if (is_array(self::$confArray) && self::$confArray['useCache']) { |
74
|
1 |
|
self::$useCache = (1 === (int)self::$confArray['useCache']); |
75
|
1 |
|
} |
76
|
1 |
|
} |
77
|
1 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Method to determine if preCaching should be used or not. |
81
|
|
|
* |
82
|
|
|
* @return boolean |
83
|
|
|
*/ |
84
|
4 |
|
public static function isCacheEnabled() { |
85
|
4 |
|
return (self::$useCache && self::$enableCache); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Use this method to force the cache usage. |
90
|
|
|
* |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
2 |
|
public static function enableCache() { |
94
|
2 |
|
self::$enableCache = TRUE; |
95
|
2 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Use this method to unforce the cache usage. |
99
|
|
|
* |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
2 |
|
public static function disableCache() { |
103
|
2 |
|
self::$enableCache = FALSE; |
104
|
2 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Flushed all caches. |
108
|
|
|
* |
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
4 |
|
public function flushAllCaches() { |
112
|
4 |
|
$this->cache = array(); |
113
|
4 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns the cache array for a given name space. |
117
|
|
|
* |
118
|
|
|
* @param $namespace |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
3 |
|
public function get($namespace) { |
122
|
3 |
|
if (array_key_exists($namespace, $this->cache) && self::isCacheEnabled()) { |
123
|
3 |
|
return $this->cache[$namespace]; |
124
|
|
|
} else { |
125
|
2 |
|
return array(); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Method to write content into the cache. |
131
|
|
|
* |
132
|
|
|
* @param $namespace |
133
|
|
|
* @param $content |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
3 |
|
public function set($namespace, $content) { |
137
|
3 |
|
$this->cache[$namespace] = $content; |
138
|
3 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Returns an instance of the cacheManager singleton. |
142
|
|
|
* |
143
|
|
|
* @return CacheManager |
144
|
|
|
*/ |
145
|
4 |
|
public static function getInstance() { |
146
|
4 |
|
if (! self::$instance instanceof CacheManager) { |
147
|
1 |
|
self::$instance = new CacheManager(); |
148
|
1 |
|
} |
149
|
|
|
|
150
|
4 |
|
return self::$instance; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Prevent from cloning |
155
|
|
|
* |
156
|
|
|
* @param void |
157
|
|
|
* @return void |
158
|
|
|
*/ |
159
|
|
|
public final function __clone() { |
160
|
|
|
trigger_error('Clone is not allowed for ' . get_class($this) . ' (Singleton)', E_USER_ERROR); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.