@@ -20,43 +20,43 @@ class Cache implements CacheInterface
20
20
/**
21
21
* @var RedisClient $redis_connector
22
22
*/
23
- private static $ redis_connector ;
23
+ private static RedisClient $ redis_connector ;
24
24
25
25
/**
26
26
* @var bool $is_redis_connected
27
27
*/
28
- public static $ is_redis_connected = false ;
28
+ public static bool $ is_redis_connected = false ;
29
29
30
30
/**
31
31
* @var LoggerInterface|null $logger
32
32
*/
33
- private static $ logger ;
33
+ private static ? LoggerInterface $ logger ;
34
34
35
35
/**
36
- * @var PDO|null $pdo
36
+ * НЕ задаем тип, поскольку может прийти как PDO, так и DBWrapper
37
+ * Тем более что UNION Types возможны с PHP 8.1+
38
+ *
39
+ * @var $pdo
37
40
*/
38
41
private static $ pdo ;
39
42
40
- /**
41
- * @var
42
- */
43
- public static $ log_rules_define = [];
44
-
45
43
/**
46
44
* @var array
47
45
*/
46
+ public static array $ log_rules_define = [];
47
+
48
48
/*private static $connection_options = [];*/
49
49
50
50
/*private static $connectors = [];*/
51
51
52
52
/**
53
53
* @var array
54
54
*/
55
- public static $ repository = [];
55
+ public static array $ repository = [];
56
56
57
- public static function init (array $ credentials = [], array $ rules = [], $ PDO = null , LoggerInterface $ logger = null )
57
+ public static function init (array $ credentials = [], array $ rules = [], $ PDO = null , ? LoggerInterface $ logger = null )
58
58
{
59
- self ::$ logger = is_null ($ logger ) ? new NullLogger () : $ logger ;
59
+ self ::$ logger = \ is_null ($ logger ) ? new NullLogger () : $ logger ;
60
60
self ::$ is_redis_connected = false ;
61
61
self ::$ pdo = $ PDO ;
62
62
@@ -81,7 +81,7 @@ public static function init(array $credentials = [], array $rules = [], $PDO = n
81
81
}
82
82
}
83
83
84
- if (!is_null ($ PDO )) {
84
+ if (!\ is_null ($ PDO )) {
85
85
$ PDO ->setAttribute (PDO ::ATTR_ERRMODE , PDO ::ERRMODE_EXCEPTION );
86
86
}
87
87
@@ -97,7 +97,7 @@ public static function init(array $credentials = [], array $rules = [], $PDO = n
97
97
98
98
} // init
99
99
100
- public static function getConnector ()
100
+ public static function getConnector (): RedisClient
101
101
{
102
102
return self ::$ redis_connector ;
103
103
}
@@ -116,6 +116,13 @@ public static function getConnector()
116
116
return $connector;
117
117
}*/
118
118
119
+ public static function addRules (array $ rules )
120
+ {
121
+ foreach ($ rules as $ name => $ rule ) {
122
+ self ::addRule ($ name , $ rule );
123
+ }
124
+ }
125
+
119
126
public static function addRule (string $ rule_name , $ rule_definition ):string
120
127
{
121
128
$ message = self ::defineRule ($ rule_name , $ rule_definition );
@@ -129,7 +136,7 @@ public static function addRule(string $rule_name, $rule_definition):string
129
136
130
137
public static function getAllLocalKeys ():array
131
138
{
132
- return array_keys (self ::$ repository );
139
+ return \ array_keys (self ::$ repository );
133
140
}
134
141
135
142
public static function getAllRedisKeys ():array
@@ -140,7 +147,7 @@ public static function getAllRedisKeys():array
140
147
}
141
148
142
149
$ keys = self ::$ redis_connector ->keys ('* ' );
143
- $ keys_count = count ($ keys );
150
+ $ keys_count = \ count ($ keys );
144
151
145
152
self ::$ logger ->debug ("[getAllRedisKeys] Returned {$ keys_count } key(s) " );
146
153
@@ -154,8 +161,12 @@ public static function getAllKeys(bool $use_keys_from_redis):array
154
161
return [];
155
162
}
156
163
157
- $ keys = $ use_keys_from_redis ? self ::$ redis_connector ->keys ('* ' ) : array_keys (self ::$ repository );
158
- $ keys_count = count ($ keys );
164
+ $ keys
165
+ = $ use_keys_from_redis
166
+ ? self ::$ redis_connector ->keys ('* ' )
167
+ : \array_keys (self ::$ repository );
168
+
169
+ $ keys_count = \count ($ keys );
159
170
160
171
self ::$ logger ->debug ("[getAllKeys] Returned {$ keys_count } key(s) " );
161
172
@@ -179,13 +190,13 @@ public static function set(string $key, $data)
179
190
180
191
public static function check (string $ key ): bool
181
192
{
182
- return array_key_exists ($ key , self ::$ repository );
193
+ return \ array_key_exists ($ key , self ::$ repository );
183
194
}
184
195
185
196
public static function unset (string $ key )
186
197
{
187
198
if (self ::check ($ key )) {
188
- unset( self ::$ repository [$ key ]);
199
+ unset(self ::$ repository [$ key ]);
189
200
}
190
201
}
191
202
@@ -198,7 +209,7 @@ public static function flushAll(string $mask = '*')
198
209
199
210
public static function flush (string $ key , bool $ clean_redis = true ):string
200
211
{
201
- if (strpos ($ key , '* ' ) === false ) {
212
+ if (\ strpos ($ key , '* ' ) === false ) {
202
213
self ::unset ($ key );
203
214
if (self ::$ redis_connector && $ clean_redis ) {
204
215
self ::$ redis_connector ->del ($ key );
@@ -207,7 +218,7 @@ public static function flush(string $key, bool $clean_redis = true):string
207
218
} else {
208
219
$ custom_mask = self ::createMask ($ key );
209
220
$ all_keys = $ clean_redis ? self ::getAllRedisKeys () : self ::getAllLocalKeys ();
210
- $ custom_list = preg_grep ($ custom_mask , $ all_keys );
221
+ $ custom_list = \ preg_grep ($ custom_mask , $ all_keys );
211
222
foreach ($ custom_list as $ k ) {
212
223
self ::flush ($ k , $ clean_redis );
213
224
}
@@ -224,7 +235,7 @@ public static function redisFetch(string $key_name, bool $use_json_decode = true
224
235
$ value = self ::$ redis_connector ->get ($ key_name );
225
236
226
237
if ($ use_json_decode && !empty ($ value )) {
227
- $ value = json_decode ($ value , true , 512 , JSON_THROW_ON_ERROR );
238
+ $ value = \ json_decode ($ value , true , 512 , JSON_THROW_ON_ERROR );
228
239
}
229
240
230
241
return $ value ;
@@ -260,12 +271,12 @@ public static function redisDel(string $key_name)
260
271
if ($ key_name === '* ' ) {
261
272
self ::$ redis_connector ->flushDb ();
262
273
$ deleted = [ '* ' ];
263
- } elseif (strpos ($ key_name , '* ' ) === false ) {
274
+ } elseif (\ strpos ($ key_name , '* ' ) === false ) {
264
275
self ::$ redis_connector ->del ($ key_name );
265
276
$ deleted = [ $ key_name ];
266
277
} else {
267
278
$ custom_mask = self ::createMask ($ key_name );
268
- $ custom_list = preg_grep ($ custom_mask , self ::$ redis_connector ->keys ('* ' ));
279
+ $ custom_list = \ preg_grep ($ custom_mask , self ::$ redis_connector ->keys ('* ' ));
269
280
270
281
foreach ($ custom_list as $ k ) {
271
282
$ deleted [] = self ::redisDel ($ k );
@@ -311,7 +322,7 @@ public static function addCounter(string $key, int $initial = 0, int $ttl = 0):i
311
322
312
323
public static function incrCounter (string $ key , int $ diff = 1 ):int
313
324
{
314
- if (!array_key_exists ($ key , self ::$ repository )) {
325
+ if (!\ array_key_exists ($ key , self ::$ repository )) {
315
326
self ::set ($ key , 0 );
316
327
}
317
328
@@ -325,7 +336,7 @@ public static function incrCounter(string $key, int $diff = 1):int
325
336
326
337
public static function decrCounter (string $ key , int $ diff = 1 ):int
327
338
{
328
- if (!array_key_exists ($ key , self ::$ repository )) {
339
+ if (!\ array_key_exists ($ key , self ::$ repository )) {
329
340
self ::set ($ key , 0 );
330
341
}
331
342
@@ -370,15 +381,15 @@ private static function defineRule($rule_name, $rule_definition)
370
381
$ rule_value = self ::$ redis_connector ->get ($ rule_name );
371
382
372
383
if ($ rule_value !== false ) {
373
- self ::set ($ rule_name , json_decode ($ rule_value , true , 512 , JSON_THROW_ON_ERROR ));
384
+ self ::set ($ rule_name , \ json_decode ($ rule_value , true , 512 , JSON_THROW_ON_ERROR ));
374
385
return "[INFO] Loaded ` {$ rule_name }` from redis, stored to cache " ;
375
386
}
376
387
}
377
388
378
389
// определяем action и TTL
379
- $ enabled = array_key_exists ('enabled ' , $ rule_definition ) ? $ rule_definition ['enabled ' ] : true ;
380
- $ ttl = array_key_exists ('ttl ' , $ rule_definition ) ? $ rule_definition ['ttl ' ] : 0 ;
381
- $ action = array_key_exists ('action ' , $ rule_definition ) ? $ rule_definition ['action ' ] : null ; // оператор `??` менее нагляден, поэтому оставлено так
390
+ $ enabled = \ array_key_exists ('enabled ' , $ rule_definition ) ? $ rule_definition ['enabled ' ] : true ;
391
+ $ ttl = \ array_key_exists ('ttl ' , $ rule_definition ) ? $ rule_definition ['ttl ' ] : 0 ;
392
+ $ action = \ array_key_exists ('action ' , $ rule_definition ) ? $ rule_definition ['action ' ] : null ; // оператор `??` менее нагляден, поэтому оставлено так
382
393
383
394
if ($ enabled === false ) {
384
395
return "Rule ` {$ rule_name }` disabled " ;
@@ -395,7 +406,7 @@ private static function defineRule($rule_name, $rule_definition)
395
406
396
407
case self ::RULE_SOURCE_SQL : {
397
408
// коннекта к БД нет: кладем в репозиторий null и продолжаем
398
- if (is_null (self ::$ pdo )) {
409
+ if (\ is_null (self ::$ pdo )) {
399
410
$ message = '[ERROR] Key not found, Action is SQL, but PDO not connected ' ;
400
411
self ::set ($ rule_name , null );
401
412
return $ message ;
@@ -424,7 +435,7 @@ private static function defineRule($rule_name, $rule_definition)
424
435
[$ actor , $ params ] = self ::compileCallbackHandler ($ rule_definition ['action ' ]);
425
436
426
437
try {
427
- $ data = call_user_func_array ($ actor , $ params );
438
+ $ data = \ call_user_func_array ($ actor , $ params );
428
439
$ message = "Data for ` {$ rule_name }` fetched from callback " ;
429
440
430
441
} catch (\PDOException $ e ) {
@@ -458,7 +469,7 @@ public static function isRedisConnected():bool
458
469
459
470
/**
460
471
* "Компилирует" параметры коллбэка
461
- * (@todo: обновить в Arris\Router)
472
+ * (@todo: обновить в Arris\Router ? )
462
473
*
463
474
* @param $actor
464
475
* @return array
@@ -480,36 +491,39 @@ private static function compileCallbackHandler($actor)
480
491
// 1 - массив параметров
481
492
if (is_array ($ actor )) {
482
493
$ handler = $ actor [0 ];
483
- $ params = (count ($ actor ) > 1 ) ? $ actor [1 ] : [];
494
+ $ params
495
+ = \count ($ actor ) > 1
496
+ ? $ actor [1 ]
497
+ : [];
484
498
485
- if (!is_string ($ handler )) {
499
+ if (!\ is_string ($ handler )) {
486
500
throw new CacheCallbackException ("First argument of callback array is NOT a string " );
487
501
}
488
502
489
- if (strpos ($ handler , '@ ' ) > 0 ) {
503
+ if (\ strpos ($ handler , '@ ' ) > 0 ) {
490
504
// dynamic class
491
- [$ class , $ method ] = explode ('@ ' , $ handler , 2 );
505
+ [$ class , $ method ] = \ explode ('@ ' , $ handler , 2 );
492
506
493
- if (!class_exists ($ class )) {
507
+ if (!\ class_exists ($ class )) {
494
508
self ::$ logger ->error ("Class {$ class } not defined. " , [ $ class ]);
495
509
throw new CacheCallbackException ("Class {$ class } not defined. " , 500 );
496
510
}
497
511
498
- if (!method_exists ($ class , $ method )) {
512
+ if (!\ method_exists ($ class , $ method )) {
499
513
self ::$ logger ->error ("Method {$ method } not declared at {$ class } class. " , [ $ class , $ method ]);
500
514
throw new CacheCallbackException ("Method {$ method } not declared at {$ class } class " , 500 );
501
515
}
502
516
503
517
$ actor = [ new $ class , $ method ];
504
- } elseif (strpos ($ handler , ':: ' ) > 0 ) {
518
+ } elseif (\ strpos ($ handler , ':: ' ) > 0 ) {
505
519
[$ class , $ method ] = explode (':: ' , $ handler , 2 );
506
520
507
- if (!class_exists ($ class )) {
521
+ if (!\ class_exists ($ class )) {
508
522
self ::$ logger ->error ("Class {$ class } not defined. " , [ $ class ]);
509
523
throw new CacheCallbackException ("Class {$ class } not defined. " , 500 );
510
524
}
511
525
512
- if (!method_exists ($ class , $ method )) {
526
+ if (!\ method_exists ($ class , $ method )) {
513
527
self ::$ logger ->error ("Static method {$ method } not declared at {$ class } class. " , [ $ class , $ method ]);
514
528
throw new CacheCallbackException ("Static method {$ method } not declared at {$ class } class " , 500 );
515
529
}
@@ -537,13 +551,13 @@ private static function compileCallbackHandler($actor)
537
551
*/
538
552
private static function createMask ($ mask )
539
553
{
540
- $ mask = str_replace ('** ' , '* ' , $ mask );
541
- $ mask = str_replace ("\\" , '\\\\' , $ mask ); // должно быть первым
542
- $ mask = str_replace ('/ ' , '\/ ' , $ mask );
543
- $ mask = str_replace ('. ' , '\. ' , $ mask );
544
- $ mask = str_replace ('* ' , '.* ' , $ mask );
545
- $ mask = str_replace ('[ ' , '\[ ' , $ mask );
546
- $ mask = str_replace ('] ' , '\] ' , $ mask );
554
+ $ mask = \ str_replace ('** ' , '* ' , $ mask );
555
+ $ mask = \ str_replace ("\\" , '\\\\' , $ mask ); // должно быть первым
556
+ $ mask = \ str_replace ('/ ' , '\/ ' , $ mask );
557
+ $ mask = \ str_replace ('. ' , '\. ' , $ mask );
558
+ $ mask = \ str_replace ('* ' , '.* ' , $ mask );
559
+ $ mask = \ str_replace ('[ ' , '\[ ' , $ mask );
560
+ $ mask = \ str_replace ('] ' , '\] ' , $ mask );
547
561
548
562
return '/^ ' . $ mask . '/m ' ;
549
563
}
0 commit comments