1 | <?php |
||
43 | class CacheManager |
||
44 | { |
||
45 | /** |
||
46 | * @var int |
||
47 | */ |
||
48 | public static $ReadHits = 0; |
||
49 | |||
50 | /** |
||
51 | * @var int |
||
52 | */ |
||
53 | public static $WriteHits = 0; |
||
54 | |||
55 | /** |
||
56 | * @var ExtendedCacheItemPoolInterface[] |
||
57 | */ |
||
58 | protected static $config = [ |
||
59 | 'itemDetailedDate' => false,// Specify if the item must provide detailed creation/modification dates |
||
60 | 'autoTmpFallback' => false,// Automatically attempt to fallback to temporary directory if the cache fails to write on the specified directory |
||
61 | 'secureFileManipulation' => false,// Provide a secure file manipulation mechanism, on intensive usage the performance can be affected. |
||
62 | 'ignoreSymfonyNotice' => false,// Ignore Symfony notice for Symfony project which do not makes use of PhpFastCache's Symfony Bundle |
||
63 | 'defaultTtl' => 900,// Default time-to-live in second |
||
64 | 'securityKey' => 'auto',// The securityKey that will be used to create sub-directory |
||
65 | 'htaccess' => true,// Auto-generate .htaccess if tit is missing |
||
66 | 'default_chmod' => 0777, // 0777 recommended |
||
67 | 'path' => '',// if not set will be the value of sys_get_temp_dir() |
||
68 | 'fallback' => false, //Fall back when old driver is not support |
||
69 | 'limited_memory_each_object' => 4096, // maximum size (bytes) of object store in memory |
||
70 | 'compress_data' => false, // compress stored data, if the backend supports it |
||
71 | ]; |
||
72 | |||
73 | /** |
||
74 | * @var string |
||
75 | */ |
||
76 | protected static $namespacePath; |
||
77 | |||
78 | /** |
||
79 | * @var ExtendedCacheItemPoolInterface[] |
||
80 | */ |
||
81 | protected static $instances = []; |
||
82 | |||
83 | /** |
||
84 | * @param string $driver |
||
85 | * @param array $config |
||
86 | * @return ExtendedCacheItemPoolInterface |
||
87 | * @throws phpFastCacheDriverCheckException |
||
88 | */ |
||
89 | public static function getInstance($driver = 'auto', $config = []) |
||
90 | { |
||
91 | static $badPracticeOmeter = []; |
||
92 | |||
93 | /** |
||
94 | * @todo: Standardize a method for driver name |
||
95 | */ |
||
96 | $driver = self::standardizeDriverName($driver); |
||
97 | $config = array_merge(self::$config, $config); |
||
98 | if (!$driver || $driver === 'Auto') { |
||
99 | $driver = self::getAutoClass($config); |
||
100 | } |
||
101 | |||
102 | $instance = crc32($driver . serialize($config)); |
||
103 | if (!isset(self::$instances[ $instance ])) { |
||
104 | $badPracticeOmeter[$driver] = 1; |
||
105 | if(!$config['ignoreSymfonyNotice'] && interface_exists('Symfony\Component\HttpKernel\KernelInterface') && !class_exists('phpFastCache\Bundle\phpFastCacheBundle')){ |
||
106 | trigger_error('A Symfony Bundle to make the PhpFastCache integration more easier is now available here: https://github.com/PHPSocialNetwork/phpfastcache-bundle', E_USER_NOTICE); |
||
107 | } |
||
108 | $class = self::getNamespacePath() . $driver . '\Driver'; |
||
109 | try{ |
||
110 | self::$instances[ $instance ] = new $class($config); |
||
111 | self::$instances[ $instance ]->setEventManager(EventManager::getInstance()); |
||
112 | }catch(phpFastCacheDriverCheckException $e){ |
||
113 | $fallback = self::standardizeDriverName($config['fallback']); |
||
114 | if($fallback && $fallback !== $driver){ |
||
115 | $class = self::getNamespacePath() . $fallback . '\Driver'; |
||
116 | self::$instances[ $instance ] = new $class($config); |
||
117 | self::$instances[ $instance ]->setEventManager(EventManager::getInstance()); |
||
118 | trigger_error(sprintf('The "%s" driver is unavailable at the moment, the fallback driver "%s" has been used instead.', $driver, $fallback), E_USER_WARNING); |
||
119 | }else{ |
||
120 | throw new phpFastCacheDriverCheckException($e->getMessage(), $e->getCode(), $e); |
||
121 | } |
||
122 | } |
||
123 | } else if(++$badPracticeOmeter[$driver] >= 5){ |
||
124 | trigger_error('[' . $driver . '] Calling many times CacheManager::getInstance() for already instanced drivers is a bad practice and have a significant impact on performances. |
||
125 | See https://github.com/PHPSocialNetwork/phpfastcache/wiki/[V5]-Why-calling-getInstance%28%29-each-time-is-a-bad-practice-%3F'); |
||
126 | } |
||
127 | |||
128 | return self::$instances[ $instance ]; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * This method is intended for internal |
||
133 | * use only and should not be used for |
||
134 | * any external development use the |
||
135 | * getInstances() method instead |
||
136 | * |
||
137 | * @internal |
||
138 | * @return ExtendedCacheItemPoolInterface[] |
||
139 | */ |
||
140 | public static function getInstances() |
||
144 | |||
145 | /** |
||
146 | * This method is intended for internal |
||
147 | * use only and should not be used for |
||
148 | * any external development use the |
||
149 | * getInstances() method instead |
||
150 | * |
||
151 | * @internal |
||
152 | * @return ExtendedCacheItemPoolInterface[] |
||
153 | */ |
||
154 | public static function &getInternalInstances() |
||
158 | |||
159 | /** |
||
160 | * @param $config |
||
161 | * @return string |
||
162 | * @throws phpFastCacheDriverCheckException |
||
163 | */ |
||
164 | public static function getAutoClass($config = []) |
||
181 | |||
182 | /** |
||
183 | * @param string $name |
||
184 | * @param array $arguments |
||
185 | * @return \Psr\Cache\CacheItemPoolInterface |
||
186 | */ |
||
187 | public static function __callStatic($name, $arguments) |
||
193 | |||
194 | /** |
||
195 | * @return bool |
||
196 | */ |
||
197 | public static function clearInstances() |
||
198 | { |
||
199 | self::$instances = []; |
||
200 | |||
201 | gc_collect_cycles(); |
||
202 | return !count(self::$instances); |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * @return string |
||
207 | */ |
||
208 | public static function getNamespacePath() |
||
212 | |||
213 | /** |
||
214 | * @param string $path |
||
215 | */ |
||
216 | public static function setNamespacePath($path) |
||
220 | |||
221 | /** |
||
222 | * @param $name string|array |
||
223 | * @param mixed $value |
||
224 | * @throws \InvalidArgumentException |
||
225 | */ |
||
226 | public static function setDefaultConfig($name, $value = null) |
||
236 | |||
237 | /** |
||
238 | * @return array |
||
239 | */ |
||
240 | public static function getDefaultConfig() |
||
244 | |||
245 | /** |
||
246 | * @return array |
||
247 | */ |
||
248 | public static function getStaticSystemDrivers() |
||
269 | |||
270 | /** |
||
271 | * @return array |
||
272 | */ |
||
273 | public static function getStaticAllDrivers() |
||
281 | |||
282 | /** |
||
283 | * @param string $driverName |
||
284 | * @return string |
||
285 | */ |
||
286 | public static function standardizeDriverName($driverName) |
||
290 | } |
||
291 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..