@@ -92,6 +92,11 @@ class CacheManager
9292 */
9393 protected static $ driverCustoms = [];
9494
95+ /**
96+ * @var array
97+ */
98+ protected static $ badPracticeOmeter = [];
99+
95100 /**
96101 * @param string $driver
97102 * @param array|ConfigurationOption $config
@@ -105,14 +110,8 @@ class CacheManager
105110 * @throws PhpfastcacheInvalidArgumentException
106111 * @throws PhpfastcacheDriverException
107112 */
108- public static function getInstance ($ driver = 'auto ' , $ config = null , $ instanceId = null ): ExtendedCacheItemPoolInterface
113+ public static function getInstance (string $ driver = 'auto ' , $ config = null , string $ instanceId = null ): ExtendedCacheItemPoolInterface
109114 {
110- static $ badPracticeOmeter = [];
111-
112- if ($ instanceId !== null && !\is_string ($ instanceId )) {
113- throw new PhpfastcacheInvalidArgumentException ('The Instance ID must be a string ' );
114- }
115-
116115 if (\is_array ($ config )) {
117116 $ config = new ConfigurationOption ($ config );
118117 trigger_error (
@@ -134,7 +133,7 @@ public static function getInstance($driver = 'auto', $config = null, $instanceId
134133 $ instance = $ instanceId ?: md5 ($ driver . \serialize ($ config ->toArray ()));
135134
136135 if (!isset (self ::$ instances [ $ instance ])) {
137- $ badPracticeOmeter [ $ driver ] = 1 ;
136+ self :: $ badPracticeOmeter [ $ driver ] = 1 ;
138137 $ driverClass = self ::getDriverClass ($ driver );
139138
140139 if (!is_a ($ driverClass , ExtendedCacheItemPoolInterface::class, true )){
@@ -166,12 +165,12 @@ public static function getInstance($driver = 'auto', $config = null, $instanceId
166165 throw new PhpfastcacheDriverCheckException ($ e ->getMessage (), $ e ->getCode (), $ e );
167166 }
168167 }
169- } else if ($ badPracticeOmeter [ $ driver ] >= 2 ) {
168+ } else if (self :: $ badPracticeOmeter [ $ driver ] >= 2 ) {
170169 trigger_error ('[ ' . $ driver . '] Calling many times CacheManager::getInstance() for already instanced drivers is a bad practice and have a significant impact on performances.
171170 See https://github.com/PHPSocialNetwork/phpfastcache/wiki/[V5]-Why-calling-getInstance%28%29-each-time-is-a-bad-practice-%3F ' );
172171 }
173172
174- $ badPracticeOmeter [ $ driver ]++;
173+ self :: $ badPracticeOmeter [ $ driver ]++;
175174
176175 return self ::$ instances [ $ instance ];
177176 }
@@ -184,7 +183,7 @@ public static function getInstance($driver = 'auto', $config = null, $instanceId
184183 * @throws PhpfastcacheInvalidArgumentException
185184 * @throws PhpfastcacheInstanceNotFoundException
186185 */
187- public static function getInstanceById ($ instanceId ): ExtendedCacheItemPoolInterface
186+ public static function getInstanceById (string $ instanceId ): ExtendedCacheItemPoolInterface
188187 {
189188 if ($ instanceId !== null && !\is_string ($ instanceId )) {
190189 throw new PhpfastcacheInvalidArgumentException ('The Instance ID must be a string ' );
@@ -238,12 +237,15 @@ public static function getAutoClass(ConfigurationOption $config): string
238237
239238 if ($ autoDriver === null ) {
240239 foreach (self ::getDriverList () as $ driver ) {
241- try {
242- self ::getInstance ($ driver , $ config );
243- $ autoDriver = $ driver ;
244- break ;
245- } catch (PhpfastcacheDriverCheckException $ e ) {
246- continue ;
240+ /** @var ExtendedCacheItemPoolInterface $driver */
241+ if ((self ::CORE_DRIVER_NAMESPACE . $ driver . '\Driver ' )::isUsableInAutoContext ()){
242+ try {
243+ self ::getInstance ($ driver , $ config );
244+ $ autoDriver = $ driver ;
245+ break ;
246+ } catch (PhpfastcacheDriverCheckException $ e ) {
247+ continue ;
248+ }
247249 }
248250 }
249251 }
@@ -252,6 +254,8 @@ public static function getAutoClass(ConfigurationOption $config): string
252254 throw new PhpfastcacheLogicException ('Unable to find out a valid driver automatically ' );
253255 }
254256
257+ self ::$ badPracticeOmeter [ $ autoDriver ]--;
258+
255259 return $ autoDriver ;
256260 }
257261
0 commit comments