@@ -31,89 +31,89 @@ |
||
31 | 31 | */ |
32 | 32 | class Driver implements AggregatablePoolInterface |
33 | 33 | { |
34 | - use TaggableCacheItemPoolTrait; |
|
35 | - |
|
36 | - /** |
|
37 | - * @return bool |
|
38 | - */ |
|
39 | - public function driverCheck(): bool |
|
40 | - { |
|
41 | - return extension_loaded('wincache') && function_exists('wincache_ucache_set'); |
|
42 | - } |
|
43 | - |
|
44 | - public function getHelp(): string |
|
45 | - { |
|
46 | - return <<<HELP |
|
34 | + use TaggableCacheItemPoolTrait; |
|
35 | + |
|
36 | + /** |
|
37 | + * @return bool |
|
38 | + */ |
|
39 | + public function driverCheck(): bool |
|
40 | + { |
|
41 | + return extension_loaded('wincache') && function_exists('wincache_ucache_set'); |
|
42 | + } |
|
43 | + |
|
44 | + public function getHelp(): string |
|
45 | + { |
|
46 | + return <<<HELP |
|
47 | 47 | Wincache PECL extension is not maintained anymore and has been superseded. Check sourceforge for more informations: https://sourceforge.net/projects/wincache/' |
48 | 48 | HELP; |
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * @return DriverStatistic |
|
53 | - */ |
|
54 | - public function getStats(): DriverStatistic |
|
55 | - { |
|
56 | - $memInfo = wincache_ucache_meminfo(); |
|
57 | - $info = wincache_ucache_info(); |
|
58 | - $date = (new DateTime())->setTimestamp(time() - $info['total_cache_uptime']); |
|
59 | - |
|
60 | - return (new DriverStatistic()) |
|
61 | - ->setInfo(sprintf("The Wincache daemon is up since %s.\n For more information see RawData.", $date->format(DATE_RFC2822))) |
|
62 | - ->setSize($memInfo['memory_free'] - $memInfo['memory_total']) |
|
63 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
64 | - ->setRawData($memInfo); |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * @return bool |
|
69 | - */ |
|
70 | - protected function driverConnect(): bool |
|
71 | - { |
|
72 | - return true; |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * @param ExtendedCacheItemInterface $item |
|
77 | - * @return ?array<string, mixed> |
|
78 | - */ |
|
79 | - protected function driverRead(ExtendedCacheItemInterface $item): ?array |
|
80 | - { |
|
81 | - $val = wincache_ucache_get($item->getKey(), $suc); |
|
82 | - |
|
83 | - if ($suc === false || empty($val)) { |
|
84 | - return null; |
|
85 | - } |
|
86 | - |
|
87 | - return $val; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * @param ExtendedCacheItemInterface $item |
|
92 | - * @return mixed |
|
93 | - * @throws PhpfastcacheInvalidArgumentException |
|
94 | - * @throws PhpfastcacheLogicException |
|
95 | - */ |
|
96 | - protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
97 | - { |
|
98 | - |
|
99 | - return wincache_ucache_set($item->getKey(), $this->driverPreWrap($item), $item->getTtl()); |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * @param string $key |
|
104 | - * @param string $encodedKey |
|
105 | - * @return bool |
|
106 | - */ |
|
107 | - protected function driverDelete(string $key, string $encodedKey): bool |
|
108 | - { |
|
109 | - return wincache_ucache_delete($key); |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * @return bool |
|
114 | - */ |
|
115 | - protected function driverClear(): bool |
|
116 | - { |
|
117 | - return wincache_ucache_clear(); |
|
118 | - } |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * @return DriverStatistic |
|
53 | + */ |
|
54 | + public function getStats(): DriverStatistic |
|
55 | + { |
|
56 | + $memInfo = wincache_ucache_meminfo(); |
|
57 | + $info = wincache_ucache_info(); |
|
58 | + $date = (new DateTime())->setTimestamp(time() - $info['total_cache_uptime']); |
|
59 | + |
|
60 | + return (new DriverStatistic()) |
|
61 | + ->setInfo(sprintf("The Wincache daemon is up since %s.\n For more information see RawData.", $date->format(DATE_RFC2822))) |
|
62 | + ->setSize($memInfo['memory_free'] - $memInfo['memory_total']) |
|
63 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
64 | + ->setRawData($memInfo); |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * @return bool |
|
69 | + */ |
|
70 | + protected function driverConnect(): bool |
|
71 | + { |
|
72 | + return true; |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * @param ExtendedCacheItemInterface $item |
|
77 | + * @return ?array<string, mixed> |
|
78 | + */ |
|
79 | + protected function driverRead(ExtendedCacheItemInterface $item): ?array |
|
80 | + { |
|
81 | + $val = wincache_ucache_get($item->getKey(), $suc); |
|
82 | + |
|
83 | + if ($suc === false || empty($val)) { |
|
84 | + return null; |
|
85 | + } |
|
86 | + |
|
87 | + return $val; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * @param ExtendedCacheItemInterface $item |
|
92 | + * @return mixed |
|
93 | + * @throws PhpfastcacheInvalidArgumentException |
|
94 | + * @throws PhpfastcacheLogicException |
|
95 | + */ |
|
96 | + protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
97 | + { |
|
98 | + |
|
99 | + return wincache_ucache_set($item->getKey(), $this->driverPreWrap($item), $item->getTtl()); |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * @param string $key |
|
104 | + * @param string $encodedKey |
|
105 | + * @return bool |
|
106 | + */ |
|
107 | + protected function driverDelete(string $key, string $encodedKey): bool |
|
108 | + { |
|
109 | + return wincache_ucache_delete($key); |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * @return bool |
|
114 | + */ |
|
115 | + protected function driverClear(): bool |
|
116 | + { |
|
117 | + return wincache_ucache_clear(); |
|
118 | + } |
|
119 | 119 | } |
@@ -23,13 +23,13 @@ |
||
23 | 23 | */ |
24 | 24 | class Config extends ConfigurationOption |
25 | 25 | { |
26 | - public function __construct(array $parameters = []) |
|
27 | - { |
|
28 | - trigger_error( |
|
29 | - 'Wincache is now deprecated and will be removed as of v10 due to the lack of updates to PHP8 as officially stated by PHP: |
|
26 | + public function __construct(array $parameters = []) |
|
27 | + { |
|
28 | + trigger_error( |
|
29 | + 'Wincache is now deprecated and will be removed as of v10 due to the lack of updates to PHP8 as officially stated by PHP: |
|
30 | 30 | https://www.php.net/manual/en/install.windows.recommended.php', |
31 | - E_USER_DEPRECATED |
|
32 | - ); |
|
33 | - parent::__construct($parameters); |
|
34 | - } |
|
31 | + E_USER_DEPRECATED |
|
32 | + ); |
|
33 | + parent::__construct($parameters); |
|
34 | + } |
|
35 | 35 | } |
@@ -23,8 +23,7 @@ |
||
23 | 23 | */ |
24 | 24 | class Config extends ConfigurationOption |
25 | 25 | { |
26 | - public function __construct(array $parameters = []) |
|
27 | - { |
|
26 | + public function __construct(array $parameters = []) { |
|
28 | 27 | trigger_error( |
29 | 28 | 'Wincache is now deprecated and will be removed as of v10 due to the lack of updates to PHP8 as officially stated by PHP: |
30 | 29 | https://www.php.net/manual/en/install.windows.recommended.php', |
@@ -29,73 +29,73 @@ |
||
29 | 29 | */ |
30 | 30 | class Driver implements ExtendedCacheItemPoolInterface |
31 | 31 | { |
32 | - use TaggableCacheItemPoolTrait; |
|
32 | + use TaggableCacheItemPoolTrait; |
|
33 | 33 | |
34 | - /** |
|
35 | - * @return bool |
|
36 | - */ |
|
37 | - public function driverCheck(): bool |
|
38 | - { |
|
39 | - return true; |
|
40 | - } |
|
34 | + /** |
|
35 | + * @return bool |
|
36 | + */ |
|
37 | + public function driverCheck(): bool |
|
38 | + { |
|
39 | + return true; |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * @return DriverStatistic |
|
44 | - */ |
|
45 | - public function getStats(): DriverStatistic |
|
46 | - { |
|
47 | - $stat = new DriverStatistic(); |
|
48 | - $stat->setInfo('[Devnull] A void info string') |
|
49 | - ->setSize(0) |
|
50 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
51 | - ->setRawData(null); |
|
42 | + /** |
|
43 | + * @return DriverStatistic |
|
44 | + */ |
|
45 | + public function getStats(): DriverStatistic |
|
46 | + { |
|
47 | + $stat = new DriverStatistic(); |
|
48 | + $stat->setInfo('[Devnull] A void info string') |
|
49 | + ->setSize(0) |
|
50 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
51 | + ->setRawData(null); |
|
52 | 52 | |
53 | - return $stat; |
|
54 | - } |
|
53 | + return $stat; |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @param ExtendedCacheItemInterface $item |
|
58 | - * @return mixed |
|
59 | - * @throws PhpfastcacheInvalidArgumentException |
|
60 | - */ |
|
61 | - protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
62 | - { |
|
56 | + /** |
|
57 | + * @param ExtendedCacheItemInterface $item |
|
58 | + * @return mixed |
|
59 | + * @throws PhpfastcacheInvalidArgumentException |
|
60 | + */ |
|
61 | + protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
62 | + { |
|
63 | 63 | |
64 | - return true; |
|
65 | - } |
|
64 | + return true; |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * @param ExtendedCacheItemInterface $item |
|
69 | - * @return ?array<string, mixed> |
|
70 | - */ |
|
71 | - protected function driverRead(CacheItemInterface $item): ?array |
|
72 | - { |
|
73 | - return null; |
|
74 | - } |
|
67 | + /** |
|
68 | + * @param ExtendedCacheItemInterface $item |
|
69 | + * @return ?array<string, mixed> |
|
70 | + */ |
|
71 | + protected function driverRead(CacheItemInterface $item): ?array |
|
72 | + { |
|
73 | + return null; |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * @param string $key |
|
78 | - * @param string $encodedKey |
|
79 | - * @return bool |
|
80 | - */ |
|
81 | - protected function driverDelete(string $key, string $encodedKey): bool |
|
82 | - { |
|
83 | - return true; |
|
84 | - } |
|
76 | + /** |
|
77 | + * @param string $key |
|
78 | + * @param string $encodedKey |
|
79 | + * @return bool |
|
80 | + */ |
|
81 | + protected function driverDelete(string $key, string $encodedKey): bool |
|
82 | + { |
|
83 | + return true; |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * @return bool |
|
88 | - */ |
|
89 | - protected function driverClear(): bool |
|
90 | - { |
|
91 | - return true; |
|
92 | - } |
|
86 | + /** |
|
87 | + * @return bool |
|
88 | + */ |
|
89 | + protected function driverClear(): bool |
|
90 | + { |
|
91 | + return true; |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * @return bool |
|
96 | - */ |
|
97 | - protected function driverConnect(): bool |
|
98 | - { |
|
99 | - return true; |
|
100 | - } |
|
94 | + /** |
|
95 | + * @return bool |
|
96 | + */ |
|
97 | + protected function driverConnect(): bool |
|
98 | + { |
|
99 | + return true; |
|
100 | + } |
|
101 | 101 | } |
@@ -31,98 +31,98 @@ |
||
31 | 31 | */ |
32 | 32 | class Driver implements ExtendedCacheItemPoolInterface |
33 | 33 | { |
34 | - use TaggableCacheItemPoolTrait; |
|
35 | - |
|
36 | - /** |
|
37 | - * @var array<string, array<string, mixed>> |
|
38 | - */ |
|
39 | - protected array $staticStack = []; |
|
40 | - |
|
41 | - /** |
|
42 | - * @return bool |
|
43 | - */ |
|
44 | - public function driverCheck(): bool |
|
45 | - { |
|
46 | - return true; |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * @return bool |
|
51 | - */ |
|
52 | - protected function driverConnect(): bool |
|
53 | - { |
|
54 | - return true; |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * @param ExtendedCacheItemInterface $item |
|
59 | - * @return ?array<string, mixed> |
|
60 | - */ |
|
61 | - protected function driverRead(ExtendedCacheItemInterface $item): ?array |
|
62 | - { |
|
63 | - return $this->staticStack[$item->getKey()] ?? null; |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * @return array<int, string> |
|
68 | - */ |
|
69 | - protected function driverReadAllKeys(string $pattern = ''): iterable |
|
70 | - { |
|
71 | - return array_filter(array_keys($this->staticStack), function (string $key) use ($pattern) { |
|
72 | - return $pattern |
|
73 | - ? preg_match('/' . str_replace('*', '(.*)', $pattern) . '/', $key) |
|
74 | - : true; |
|
75 | - }); |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * @param ExtendedCacheItemInterface $item |
|
80 | - * @return bool |
|
81 | - * @throws PhpfastcacheInvalidArgumentException |
|
82 | - * @throws PhpfastcacheLogicException |
|
83 | - */ |
|
84 | - protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
85 | - { |
|
86 | - |
|
87 | - $this->staticStack[$item->getKey()] = $this->driverPreWrap($item); |
|
88 | - return true; |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * @param string $key |
|
93 | - * @param string $encodedKey |
|
94 | - * @return bool |
|
95 | - */ |
|
96 | - protected function driverDelete(string $key, string $encodedKey): bool |
|
97 | - { |
|
98 | - if (isset($this->staticStack[$key])) { |
|
99 | - unset($this->staticStack[$key]); |
|
100 | - return true; |
|
101 | - } |
|
102 | - return false; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * @return bool |
|
107 | - */ |
|
108 | - protected function driverClear(): bool |
|
109 | - { |
|
110 | - unset($this->staticStack); |
|
111 | - $this->staticStack = []; |
|
112 | - return true; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * @return DriverStatistic |
|
117 | - */ |
|
118 | - public function getStats(): DriverStatistic |
|
119 | - { |
|
120 | - $stat = new DriverStatistic(); |
|
121 | - $stat->setInfo('[Memstatic] A memory static driver') |
|
122 | - ->setSize(mb_strlen(serialize($this->staticStack))) |
|
123 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
124 | - ->setRawData($this->staticStack); |
|
125 | - |
|
126 | - return $stat; |
|
127 | - } |
|
34 | + use TaggableCacheItemPoolTrait; |
|
35 | + |
|
36 | + /** |
|
37 | + * @var array<string, array<string, mixed>> |
|
38 | + */ |
|
39 | + protected array $staticStack = []; |
|
40 | + |
|
41 | + /** |
|
42 | + * @return bool |
|
43 | + */ |
|
44 | + public function driverCheck(): bool |
|
45 | + { |
|
46 | + return true; |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * @return bool |
|
51 | + */ |
|
52 | + protected function driverConnect(): bool |
|
53 | + { |
|
54 | + return true; |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * @param ExtendedCacheItemInterface $item |
|
59 | + * @return ?array<string, mixed> |
|
60 | + */ |
|
61 | + protected function driverRead(ExtendedCacheItemInterface $item): ?array |
|
62 | + { |
|
63 | + return $this->staticStack[$item->getKey()] ?? null; |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * @return array<int, string> |
|
68 | + */ |
|
69 | + protected function driverReadAllKeys(string $pattern = ''): iterable |
|
70 | + { |
|
71 | + return array_filter(array_keys($this->staticStack), function (string $key) use ($pattern) { |
|
72 | + return $pattern |
|
73 | + ? preg_match('/' . str_replace('*', '(.*)', $pattern) . '/', $key) |
|
74 | + : true; |
|
75 | + }); |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * @param ExtendedCacheItemInterface $item |
|
80 | + * @return bool |
|
81 | + * @throws PhpfastcacheInvalidArgumentException |
|
82 | + * @throws PhpfastcacheLogicException |
|
83 | + */ |
|
84 | + protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
85 | + { |
|
86 | + |
|
87 | + $this->staticStack[$item->getKey()] = $this->driverPreWrap($item); |
|
88 | + return true; |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * @param string $key |
|
93 | + * @param string $encodedKey |
|
94 | + * @return bool |
|
95 | + */ |
|
96 | + protected function driverDelete(string $key, string $encodedKey): bool |
|
97 | + { |
|
98 | + if (isset($this->staticStack[$key])) { |
|
99 | + unset($this->staticStack[$key]); |
|
100 | + return true; |
|
101 | + } |
|
102 | + return false; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * @return bool |
|
107 | + */ |
|
108 | + protected function driverClear(): bool |
|
109 | + { |
|
110 | + unset($this->staticStack); |
|
111 | + $this->staticStack = []; |
|
112 | + return true; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * @return DriverStatistic |
|
117 | + */ |
|
118 | + public function getStats(): DriverStatistic |
|
119 | + { |
|
120 | + $stat = new DriverStatistic(); |
|
121 | + $stat->setInfo('[Memstatic] A memory static driver') |
|
122 | + ->setSize(mb_strlen(serialize($this->staticStack))) |
|
123 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
124 | + ->setRawData($this->staticStack); |
|
125 | + |
|
126 | + return $stat; |
|
127 | + } |
|
128 | 128 | } |
@@ -68,7 +68,7 @@ |
||
68 | 68 | */ |
69 | 69 | protected function driverReadAllKeys(string $pattern = ''): iterable |
70 | 70 | { |
71 | - return array_filter(array_keys($this->staticStack), function (string $key) use ($pattern) { |
|
71 | + return array_filter(array_keys($this->staticStack), function(string $key) use ($pattern) { |
|
72 | 72 | return $pattern |
73 | 73 | ? preg_match('/' . str_replace('*', '(.*)', $pattern) . '/', $key) |
74 | 74 | : true; |
@@ -30,115 +30,115 @@ |
||
30 | 30 | */ |
31 | 31 | class Driver implements AggregatablePoolInterface |
32 | 32 | { |
33 | - use RedisDriverTrait, TaggableCacheItemPoolTrait { |
|
34 | - RedisDriverTrait::driverReadMultiple insteadof TaggableCacheItemPoolTrait; |
|
35 | - RedisDriverTrait::driverDeleteMultiple insteadof TaggableCacheItemPoolTrait; |
|
36 | - } |
|
37 | - |
|
38 | - |
|
39 | - /** |
|
40 | - * @return bool |
|
41 | - */ |
|
42 | - public function driverCheck(): bool |
|
43 | - { |
|
44 | - return extension_loaded('Redis') && class_exists(RedisClient::class); |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * @return DriverStatistic |
|
49 | - */ |
|
50 | - public function getStats(): DriverStatistic |
|
51 | - { |
|
52 | - // used_memory |
|
53 | - $info = $this->instance->info(); |
|
54 | - $date = (new DateTime())->setTimestamp(time() - $info['uptime_in_seconds']); |
|
55 | - |
|
56 | - return (new DriverStatistic()) |
|
57 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
58 | - ->setRawData($info) |
|
59 | - ->setSize((int)$info['used_memory_dataset']) |
|
60 | - ->setInfo( |
|
61 | - sprintf( |
|
62 | - "The Redis daemon v%s, php-ext v%s, is up since %s.\n For more information see RawData.", |
|
63 | - $info['redis_version'], |
|
64 | - \phpversion("redis"), |
|
65 | - $date->format(DATE_RFC2822) |
|
66 | - ) |
|
67 | - ); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * @return bool |
|
72 | - * @throws PhpfastcacheLogicException |
|
73 | - */ |
|
74 | - protected function driverConnect(): bool |
|
75 | - { |
|
76 | - if (isset($this->instance) && $this->instance instanceof RedisClient) { |
|
77 | - throw new PhpfastcacheLogicException('Already connected to Redis server'); |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * In case of an user-provided |
|
82 | - * Redis client just return here |
|
83 | - */ |
|
84 | - if ($this->getConfig()->getRedisClient() instanceof RedisClient) { |
|
85 | - /** |
|
86 | - * Unlike Predis, we can't test if we're connected |
|
87 | - * or not, so let's just assume that we are |
|
88 | - */ |
|
89 | - $this->instance = $this->getConfig()->getRedisClient(); |
|
90 | - return true; |
|
91 | - } |
|
92 | - |
|
93 | - $this->instance = $this->instance ?? new RedisClient(); |
|
94 | - |
|
95 | - /** |
|
96 | - * If path is provided we consider it as a UNIX Socket |
|
97 | - */ |
|
98 | - if ($this->getConfig()->getPath()) { |
|
99 | - $isConnected = $this->instance->connect($this->getConfig()->getPath()); |
|
100 | - } else { |
|
101 | - $isConnected = $this->instance->connect($this->getConfig()->getHost(), $this->getConfig()->getPort(), $this->getConfig()->getTimeout()); |
|
102 | - } |
|
103 | - |
|
104 | - if (!$isConnected && $this->getConfig()->getPath()) { |
|
105 | - return false; |
|
106 | - } |
|
107 | - |
|
108 | - if ($this->getConfig()->getOptPrefix()) { |
|
109 | - $this->instance->setOption(RedisClient::OPT_PREFIX, $this->getConfig()->getOptPrefix()); |
|
110 | - } |
|
111 | - |
|
112 | - if ($this->getConfig()->getPassword() && !$this->instance->auth($this->getConfig()->getPassword())) { |
|
113 | - return false; |
|
114 | - } |
|
115 | - |
|
116 | - if ($this->getConfig()->getDatabase() !== null) { |
|
117 | - $this->instance->select($this->getConfig()->getDatabase()); |
|
118 | - } |
|
119 | - return true; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * @return array<int, string> |
|
124 | - * @throws \RedisException |
|
125 | - */ |
|
126 | - protected function driverReadAllKeys(string $pattern = '*'): iterable |
|
127 | - { |
|
128 | - $i = -1; |
|
129 | - $keys = $this->instance->scan($i, $pattern === '' ? '*' : $pattern, ExtendedCacheItemPoolInterface::MAX_ALL_KEYS_COUNT); |
|
130 | - if (is_iterable($keys)) { |
|
131 | - return $keys; |
|
132 | - } else { |
|
133 | - return []; |
|
134 | - } |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * @return bool |
|
139 | - */ |
|
140 | - protected function driverClear(): bool |
|
141 | - { |
|
142 | - return $this->instance->flushDB(); |
|
143 | - } |
|
33 | + use RedisDriverTrait, TaggableCacheItemPoolTrait { |
|
34 | + RedisDriverTrait::driverReadMultiple insteadof TaggableCacheItemPoolTrait; |
|
35 | + RedisDriverTrait::driverDeleteMultiple insteadof TaggableCacheItemPoolTrait; |
|
36 | + } |
|
37 | + |
|
38 | + |
|
39 | + /** |
|
40 | + * @return bool |
|
41 | + */ |
|
42 | + public function driverCheck(): bool |
|
43 | + { |
|
44 | + return extension_loaded('Redis') && class_exists(RedisClient::class); |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * @return DriverStatistic |
|
49 | + */ |
|
50 | + public function getStats(): DriverStatistic |
|
51 | + { |
|
52 | + // used_memory |
|
53 | + $info = $this->instance->info(); |
|
54 | + $date = (new DateTime())->setTimestamp(time() - $info['uptime_in_seconds']); |
|
55 | + |
|
56 | + return (new DriverStatistic()) |
|
57 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
58 | + ->setRawData($info) |
|
59 | + ->setSize((int)$info['used_memory_dataset']) |
|
60 | + ->setInfo( |
|
61 | + sprintf( |
|
62 | + "The Redis daemon v%s, php-ext v%s, is up since %s.\n For more information see RawData.", |
|
63 | + $info['redis_version'], |
|
64 | + \phpversion("redis"), |
|
65 | + $date->format(DATE_RFC2822) |
|
66 | + ) |
|
67 | + ); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * @return bool |
|
72 | + * @throws PhpfastcacheLogicException |
|
73 | + */ |
|
74 | + protected function driverConnect(): bool |
|
75 | + { |
|
76 | + if (isset($this->instance) && $this->instance instanceof RedisClient) { |
|
77 | + throw new PhpfastcacheLogicException('Already connected to Redis server'); |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * In case of an user-provided |
|
82 | + * Redis client just return here |
|
83 | + */ |
|
84 | + if ($this->getConfig()->getRedisClient() instanceof RedisClient) { |
|
85 | + /** |
|
86 | + * Unlike Predis, we can't test if we're connected |
|
87 | + * or not, so let's just assume that we are |
|
88 | + */ |
|
89 | + $this->instance = $this->getConfig()->getRedisClient(); |
|
90 | + return true; |
|
91 | + } |
|
92 | + |
|
93 | + $this->instance = $this->instance ?? new RedisClient(); |
|
94 | + |
|
95 | + /** |
|
96 | + * If path is provided we consider it as a UNIX Socket |
|
97 | + */ |
|
98 | + if ($this->getConfig()->getPath()) { |
|
99 | + $isConnected = $this->instance->connect($this->getConfig()->getPath()); |
|
100 | + } else { |
|
101 | + $isConnected = $this->instance->connect($this->getConfig()->getHost(), $this->getConfig()->getPort(), $this->getConfig()->getTimeout()); |
|
102 | + } |
|
103 | + |
|
104 | + if (!$isConnected && $this->getConfig()->getPath()) { |
|
105 | + return false; |
|
106 | + } |
|
107 | + |
|
108 | + if ($this->getConfig()->getOptPrefix()) { |
|
109 | + $this->instance->setOption(RedisClient::OPT_PREFIX, $this->getConfig()->getOptPrefix()); |
|
110 | + } |
|
111 | + |
|
112 | + if ($this->getConfig()->getPassword() && !$this->instance->auth($this->getConfig()->getPassword())) { |
|
113 | + return false; |
|
114 | + } |
|
115 | + |
|
116 | + if ($this->getConfig()->getDatabase() !== null) { |
|
117 | + $this->instance->select($this->getConfig()->getDatabase()); |
|
118 | + } |
|
119 | + return true; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * @return array<int, string> |
|
124 | + * @throws \RedisException |
|
125 | + */ |
|
126 | + protected function driverReadAllKeys(string $pattern = '*'): iterable |
|
127 | + { |
|
128 | + $i = -1; |
|
129 | + $keys = $this->instance->scan($i, $pattern === '' ? '*' : $pattern, ExtendedCacheItemPoolInterface::MAX_ALL_KEYS_COUNT); |
|
130 | + if (is_iterable($keys)) { |
|
131 | + return $keys; |
|
132 | + } else { |
|
133 | + return []; |
|
134 | + } |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * @return bool |
|
139 | + */ |
|
140 | + protected function driverClear(): bool |
|
141 | + { |
|
142 | + return $this->instance->flushDB(); |
|
143 | + } |
|
144 | 144 | } |
@@ -56,7 +56,7 @@ |
||
56 | 56 | return (new DriverStatistic()) |
57 | 57 | ->setData(implode(', ', array_keys($this->itemInstances))) |
58 | 58 | ->setRawData($info) |
59 | - ->setSize((int)$info['used_memory_dataset']) |
|
59 | + ->setSize((int) $info['used_memory_dataset']) |
|
60 | 60 | ->setInfo( |
61 | 61 | sprintf( |
62 | 62 | "The Redis daemon v%s, php-ext v%s, is up since %s.\n For more information see RawData.", |
@@ -97,7 +97,8 @@ discard block |
||
97 | 97 | */ |
98 | 98 | if ($this->getConfig()->getPath()) { |
99 | 99 | $isConnected = $this->instance->connect($this->getConfig()->getPath()); |
100 | - } else { |
|
100 | + } |
|
101 | + else { |
|
101 | 102 | $isConnected = $this->instance->connect($this->getConfig()->getHost(), $this->getConfig()->getPort(), $this->getConfig()->getTimeout()); |
102 | 103 | } |
103 | 104 | |
@@ -129,7 +130,8 @@ discard block |
||
129 | 130 | $keys = $this->instance->scan($i, $pattern === '' ? '*' : $pattern, ExtendedCacheItemPoolInterface::MAX_ALL_KEYS_COUNT); |
130 | 131 | if (is_iterable($keys)) { |
131 | 132 | return $keys; |
132 | - } else { |
|
133 | + } |
|
134 | + else { |
|
133 | 135 | return []; |
134 | 136 | } |
135 | 137 | } |
@@ -28,76 +28,76 @@ |
||
28 | 28 | */ |
29 | 29 | trait RedisDriverTrait |
30 | 30 | { |
31 | - /** |
|
32 | - * @param ExtendedCacheItemInterface $item |
|
33 | - * @return ?array<string, mixed> |
|
34 | - */ |
|
35 | - protected function driverRead(ExtendedCacheItemInterface $item): ?array |
|
36 | - { |
|
37 | - $val = $this->instance->get($item->getKey()); |
|
38 | - if (!$val) { |
|
39 | - return null; |
|
40 | - } |
|
31 | + /** |
|
32 | + * @param ExtendedCacheItemInterface $item |
|
33 | + * @return ?array<string, mixed> |
|
34 | + */ |
|
35 | + protected function driverRead(ExtendedCacheItemInterface $item): ?array |
|
36 | + { |
|
37 | + $val = $this->instance->get($item->getKey()); |
|
38 | + if (!$val) { |
|
39 | + return null; |
|
40 | + } |
|
41 | 41 | |
42 | - return $this->decode($val); |
|
43 | - } |
|
42 | + return $this->decode($val); |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * @param ExtendedCacheItemInterface ...$items |
|
47 | - * @return array<array<string, mixed>> |
|
48 | - * @throws \Phpfastcache\Exceptions\PhpfastcacheDriverException |
|
49 | - * @throws \RedisException |
|
50 | - */ |
|
51 | - protected function driverReadMultiple(ExtendedCacheItemInterface ...$items): array |
|
52 | - { |
|
53 | - $keys = $this->getKeys($items); |
|
45 | + /** |
|
46 | + * @param ExtendedCacheItemInterface ...$items |
|
47 | + * @return array<array<string, mixed>> |
|
48 | + * @throws \Phpfastcache\Exceptions\PhpfastcacheDriverException |
|
49 | + * @throws \RedisException |
|
50 | + */ |
|
51 | + protected function driverReadMultiple(ExtendedCacheItemInterface ...$items): array |
|
52 | + { |
|
53 | + $keys = $this->getKeys($items); |
|
54 | 54 | |
55 | - return array_combine($keys, array_map( |
|
56 | - fn($val) => $val ? $this->decode($val) : null, |
|
57 | - $this->instance->mGet($keys) |
|
58 | - )); |
|
59 | - } |
|
55 | + return array_combine($keys, array_map( |
|
56 | + fn($val) => $val ? $this->decode($val) : null, |
|
57 | + $this->instance->mGet($keys) |
|
58 | + )); |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * @param ExtendedCacheItemInterface $item |
|
63 | - * @return mixed |
|
64 | - * @throws PhpfastcacheInvalidArgumentException |
|
65 | - * @throws PhpfastcacheLogicException |
|
66 | - */ |
|
67 | - protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
68 | - { |
|
69 | - $this->assertCacheItemType($item, self::getItemClass()); |
|
61 | + /** |
|
62 | + * @param ExtendedCacheItemInterface $item |
|
63 | + * @return mixed |
|
64 | + * @throws PhpfastcacheInvalidArgumentException |
|
65 | + * @throws PhpfastcacheLogicException |
|
66 | + */ |
|
67 | + protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
68 | + { |
|
69 | + $this->assertCacheItemType($item, self::getItemClass()); |
|
70 | 70 | |
71 | - $ttl = $item->getExpirationDate()->getTimestamp() - time(); |
|
71 | + $ttl = $item->getExpirationDate()->getTimestamp() - time(); |
|
72 | 72 | |
73 | - /** |
|
74 | - * @see https://redis.io/commands/setex |
|
75 | - * @see https://redis.io/commands/expire |
|
76 | - */ |
|
77 | - if ($ttl <= 0) { |
|
78 | - return $this->instance->expire($item->getKey(), 0); |
|
79 | - } |
|
73 | + /** |
|
74 | + * @see https://redis.io/commands/setex |
|
75 | + * @see https://redis.io/commands/expire |
|
76 | + */ |
|
77 | + if ($ttl <= 0) { |
|
78 | + return $this->instance->expire($item->getKey(), 0); |
|
79 | + } |
|
80 | 80 | |
81 | - return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item))); |
|
82 | - } |
|
81 | + return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item))); |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * @param string $key |
|
86 | - * @param string $encodedKey |
|
87 | - * @return bool |
|
88 | - * @throws \RedisException |
|
89 | - */ |
|
90 | - protected function driverDelete(string $key, string $encodedKey): bool |
|
91 | - { |
|
92 | - return (bool) $this->instance->del($key); |
|
93 | - } |
|
84 | + /** |
|
85 | + * @param string $key |
|
86 | + * @param string $encodedKey |
|
87 | + * @return bool |
|
88 | + * @throws \RedisException |
|
89 | + */ |
|
90 | + protected function driverDelete(string $key, string $encodedKey): bool |
|
91 | + { |
|
92 | + return (bool) $this->instance->del($key); |
|
93 | + } |
|
94 | 94 | |
95 | - /** |
|
96 | - * @param string[] $keys |
|
97 | - * @return bool |
|
98 | - */ |
|
99 | - protected function driverDeleteMultiple(array $keys): bool |
|
100 | - { |
|
101 | - return (bool) $this->instance->del(...$keys); |
|
102 | - } |
|
95 | + /** |
|
96 | + * @param string[] $keys |
|
97 | + * @return bool |
|
98 | + */ |
|
99 | + protected function driverDeleteMultiple(array $keys): bool |
|
100 | + { |
|
101 | + return (bool) $this->instance->del(...$keys); |
|
102 | + } |
|
103 | 103 | } |
@@ -22,140 +22,140 @@ |
||
22 | 22 | |
23 | 23 | class Config extends ConfigurationOption |
24 | 24 | { |
25 | - protected string $host = '127.0.0.1'; |
|
26 | - protected int $port = 6379; |
|
27 | - protected string $password = ''; |
|
28 | - protected int $database = 0; |
|
29 | - protected int $timeout = 5; |
|
30 | - protected ?RedisClient $redisClient = null; |
|
31 | - protected string $optPrefix = ''; |
|
25 | + protected string $host = '127.0.0.1'; |
|
26 | + protected int $port = 6379; |
|
27 | + protected string $password = ''; |
|
28 | + protected int $database = 0; |
|
29 | + protected int $timeout = 5; |
|
30 | + protected ?RedisClient $redisClient = null; |
|
31 | + protected string $optPrefix = ''; |
|
32 | 32 | /** |
33 | - * @return string |
|
34 | - */ |
|
35 | - public function getHost(): string |
|
36 | - { |
|
37 | - return $this->host; |
|
38 | - } |
|
39 | - |
|
40 | - /** |
|
41 | - * @param string $host |
|
42 | - * @return static |
|
43 | - * @throws PhpfastcacheLogicException |
|
44 | - */ |
|
45 | - public function setHost(string $host): static |
|
46 | - { |
|
47 | - return $this->setProperty('host', $host); |
|
48 | - } |
|
49 | - |
|
50 | - /** |
|
51 | - * @return int |
|
52 | - */ |
|
53 | - public function getPort(): int |
|
54 | - { |
|
55 | - return $this->port; |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * @param int $port |
|
60 | - * @return static |
|
61 | - * @throws PhpfastcacheLogicException |
|
62 | - */ |
|
63 | - public function setPort(int $port): static |
|
64 | - { |
|
65 | - return $this->setProperty('port', $port); |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * @return string |
|
70 | - */ |
|
71 | - public function getPassword(): string |
|
72 | - { |
|
73 | - return $this->password; |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * @param string $password |
|
78 | - * |
|
79 | - * @return static |
|
80 | - * @throws PhpfastcacheLogicException |
|
81 | - */ |
|
82 | - public function setPassword(string $password): static |
|
83 | - { |
|
84 | - return $this->setProperty('password', $password); |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * @return null|int |
|
89 | - */ |
|
90 | - public function getDatabase(): ?int |
|
91 | - { |
|
92 | - return $this->database; |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * @param int|null $database |
|
97 | - * |
|
98 | - * @return static |
|
99 | - * @throws PhpfastcacheLogicException |
|
100 | - */ |
|
101 | - public function setDatabase(int $database = null): static |
|
102 | - { |
|
103 | - return $this->setProperty('database', $database); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * @return int |
|
108 | - */ |
|
109 | - public function getTimeout(): int |
|
110 | - { |
|
111 | - return $this->timeout; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @param int $timeout |
|
116 | - * @return static |
|
117 | - * @throws PhpfastcacheLogicException |
|
118 | - */ |
|
119 | - public function setTimeout(int $timeout): static |
|
120 | - { |
|
121 | - return $this->setProperty('timeout', $timeout); |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * @return RedisClient|null |
|
126 | - */ |
|
127 | - public function getRedisClient(): ?RedisClient |
|
128 | - { |
|
129 | - return $this->redisClient; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * @param RedisClient|null $redisClient |
|
134 | - * @return Config |
|
135 | - * @throws PhpfastcacheLogicException |
|
136 | - */ |
|
137 | - public function setRedisClient(?RedisClient $redisClient): Config |
|
138 | - { |
|
139 | - return $this->setProperty('redisClient', $redisClient); |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * @return string |
|
144 | - * @since 7.0.2 |
|
145 | - */ |
|
146 | - public function getOptPrefix(): string |
|
147 | - { |
|
148 | - return $this->optPrefix; |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * @param string $optPrefix |
|
153 | - * @return Config |
|
154 | - * @throws PhpfastcacheLogicException |
|
155 | - * @since 7.0.2 |
|
156 | - */ |
|
157 | - public function setOptPrefix(string $optPrefix): Config |
|
158 | - { |
|
159 | - return $this->setProperty('optPrefix', trim($optPrefix)); |
|
160 | - } |
|
33 | + * @return string |
|
34 | + */ |
|
35 | + public function getHost(): string |
|
36 | + { |
|
37 | + return $this->host; |
|
38 | + } |
|
39 | + |
|
40 | + /** |
|
41 | + * @param string $host |
|
42 | + * @return static |
|
43 | + * @throws PhpfastcacheLogicException |
|
44 | + */ |
|
45 | + public function setHost(string $host): static |
|
46 | + { |
|
47 | + return $this->setProperty('host', $host); |
|
48 | + } |
|
49 | + |
|
50 | + /** |
|
51 | + * @return int |
|
52 | + */ |
|
53 | + public function getPort(): int |
|
54 | + { |
|
55 | + return $this->port; |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * @param int $port |
|
60 | + * @return static |
|
61 | + * @throws PhpfastcacheLogicException |
|
62 | + */ |
|
63 | + public function setPort(int $port): static |
|
64 | + { |
|
65 | + return $this->setProperty('port', $port); |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * @return string |
|
70 | + */ |
|
71 | + public function getPassword(): string |
|
72 | + { |
|
73 | + return $this->password; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * @param string $password |
|
78 | + * |
|
79 | + * @return static |
|
80 | + * @throws PhpfastcacheLogicException |
|
81 | + */ |
|
82 | + public function setPassword(string $password): static |
|
83 | + { |
|
84 | + return $this->setProperty('password', $password); |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * @return null|int |
|
89 | + */ |
|
90 | + public function getDatabase(): ?int |
|
91 | + { |
|
92 | + return $this->database; |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * @param int|null $database |
|
97 | + * |
|
98 | + * @return static |
|
99 | + * @throws PhpfastcacheLogicException |
|
100 | + */ |
|
101 | + public function setDatabase(int $database = null): static |
|
102 | + { |
|
103 | + return $this->setProperty('database', $database); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * @return int |
|
108 | + */ |
|
109 | + public function getTimeout(): int |
|
110 | + { |
|
111 | + return $this->timeout; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @param int $timeout |
|
116 | + * @return static |
|
117 | + * @throws PhpfastcacheLogicException |
|
118 | + */ |
|
119 | + public function setTimeout(int $timeout): static |
|
120 | + { |
|
121 | + return $this->setProperty('timeout', $timeout); |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * @return RedisClient|null |
|
126 | + */ |
|
127 | + public function getRedisClient(): ?RedisClient |
|
128 | + { |
|
129 | + return $this->redisClient; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * @param RedisClient|null $redisClient |
|
134 | + * @return Config |
|
135 | + * @throws PhpfastcacheLogicException |
|
136 | + */ |
|
137 | + public function setRedisClient(?RedisClient $redisClient): Config |
|
138 | + { |
|
139 | + return $this->setProperty('redisClient', $redisClient); |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * @return string |
|
144 | + * @since 7.0.2 |
|
145 | + */ |
|
146 | + public function getOptPrefix(): string |
|
147 | + { |
|
148 | + return $this->optPrefix; |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * @param string $optPrefix |
|
153 | + * @return Config |
|
154 | + * @throws PhpfastcacheLogicException |
|
155 | + * @since 7.0.2 |
|
156 | + */ |
|
157 | + public function setOptPrefix(string $optPrefix): Config |
|
158 | + { |
|
159 | + return $this->setProperty('optPrefix', trim($optPrefix)); |
|
160 | + } |
|
161 | 161 | } |
@@ -24,172 +24,172 @@ |
||
24 | 24 | |
25 | 25 | class EventManager implements EventManagerInterface |
26 | 26 | { |
27 | - use UninstanciableObjectTrait; |
|
28 | - |
|
29 | - public const ON_EVERY_EVENT = '__every'; |
|
30 | - |
|
31 | - protected static EventManagerInterface $instance; |
|
32 | - |
|
33 | - protected bool $isScopedEventManager = false; |
|
34 | - |
|
35 | - protected ?ExtendedCacheItemPoolInterface $itemPoolContext = null; |
|
36 | - |
|
37 | - /** @var array<string, array<string, callable>> */ |
|
38 | - protected array $events = [ |
|
39 | - self::ON_EVERY_EVENT => [] |
|
40 | - ]; |
|
41 | - |
|
42 | - /** |
|
43 | - * @return EventManagerInterface |
|
44 | - */ |
|
45 | - public static function getInstance(): EventManagerInterface |
|
46 | - { |
|
47 | - return (self::$instance ?? self::$instance = new static()); |
|
48 | - } |
|
49 | - |
|
50 | - /** |
|
51 | - * @param EventManagerInterface $eventManagerInstance |
|
52 | - * @return void |
|
53 | - */ |
|
54 | - public static function setInstance(EventManagerInterface $eventManagerInstance): void |
|
55 | - { |
|
56 | - self::$instance = $eventManagerInstance; |
|
57 | - } |
|
58 | - |
|
59 | - public function dispatch(string $eventName, ...$args): void |
|
60 | - { |
|
61 | - /** |
|
62 | - * Replace array_key_exists by isset |
|
63 | - * due to performance issue on huge |
|
64 | - * loop dispatching operations |
|
65 | - */ |
|
66 | - if (isset($this->events[$eventName]) && $eventName !== self::ON_EVERY_EVENT) { |
|
67 | - $loopArgs = array_merge($args, [$eventName]); |
|
68 | - foreach ($this->events[$eventName] as $event) { |
|
69 | - /** |
|
70 | - * @todo V10: BC Break: Put eventName as first parameter (like self::ON_EVERY_EVENT) |
|
71 | - */ |
|
72 | - $event(...$loopArgs); |
|
73 | - } |
|
74 | - } |
|
75 | - foreach ($this->events[self::ON_EVERY_EVENT] as $event) { |
|
76 | - $event($eventName, ...$args); |
|
77 | - } |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * @inheritDoc |
|
82 | - * @throws PhpfastcacheInvalidArgumentException |
|
83 | - * @throws PhpfastcacheEventManagerException |
|
84 | - */ |
|
85 | - public function __call(string $name, array $arguments): void |
|
86 | - { |
|
87 | - if (\str_starts_with($name, 'on')) { |
|
88 | - $name = \substr($name, 2); |
|
89 | - if (\is_callable($arguments[0])) { |
|
90 | - if (isset($arguments[1]) && \is_string($arguments[0])) { |
|
91 | - $this->events[$name][$arguments[1]] = $arguments[0]; |
|
92 | - } else { |
|
93 | - $this->events[$name][] = $arguments[0]; |
|
94 | - } |
|
95 | - } else { |
|
96 | - throw new PhpfastcacheInvalidArgumentException(\sprintf('Expected Callable, got "%s"', \gettype($arguments[0]))); |
|
97 | - } |
|
98 | - } else { |
|
99 | - throw new PhpfastcacheEventManagerException('An event must start with "on" such as "onCacheGetItem"'); |
|
100 | - } |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * @param callable $callback |
|
105 | - * @param string $callbackName |
|
106 | - * @throws PhpfastcacheEventManagerException |
|
107 | - */ |
|
108 | - public function onEveryEvents(callable $callback, string $callbackName): void |
|
109 | - { |
|
110 | - if ($callbackName === '') { |
|
111 | - throw new PhpfastcacheEventManagerException('Callbackname cannot be empty'); |
|
112 | - } |
|
113 | - $this->events[self::ON_EVERY_EVENT][$callbackName] = $callback; |
|
114 | - } |
|
115 | - |
|
116 | - |
|
117 | - /** |
|
118 | - * @throws PhpfastcacheEventManagerException |
|
119 | - */ |
|
120 | - public function on(array|string $events, callable $callback): void |
|
121 | - { |
|
122 | - if (is_string($events)) { |
|
123 | - $events = [$events]; |
|
124 | - } |
|
125 | - foreach ($events as $event) { |
|
126 | - if (!\preg_match('#^([a-zA-Z])*$#', $event)) { |
|
127 | - throw new PhpfastcacheEventManagerException(\sprintf('Invalid event name "%s"', $event)); |
|
128 | - } |
|
129 | - |
|
130 | - $this->{'on' . \ucfirst($event)}($callback); |
|
131 | - } |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * @param string $eventName |
|
136 | - * @param string $callbackName |
|
137 | - * @return bool |
|
138 | - */ |
|
139 | - public function unbindEventCallback(string $eventName, string $callbackName): bool |
|
140 | - { |
|
141 | - $return = isset($this->events[$eventName][$callbackName]); |
|
142 | - unset($this->events[$eventName][$callbackName]); |
|
143 | - |
|
144 | - return $return; |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * @return bool |
|
149 | - */ |
|
150 | - public function unbindAllEventCallbacks(): bool |
|
151 | - { |
|
152 | - $this->events = [ |
|
153 | - self::ON_EVERY_EVENT => [] |
|
154 | - ]; |
|
155 | - |
|
156 | - return true; |
|
157 | - } |
|
158 | - |
|
159 | - public function __clone(): void |
|
160 | - { |
|
161 | - $this->isScopedEventManager = true; |
|
162 | - $this->unbindAllEventCallbacks(); |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * @param ExtendedCacheItemPoolInterface $pool |
|
167 | - * @return EventManagerInterface |
|
168 | - * @throws PhpfastcacheEventManagerException |
|
169 | - */ |
|
170 | - public function getScopedEventManager(ExtendedCacheItemPoolInterface $pool): EventManagerInterface |
|
171 | - { |
|
172 | - return (clone $this)->setItemPoolContext($pool); |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * @param ExtendedCacheItemPoolInterface $pool |
|
177 | - * @return EventManagerInterface |
|
178 | - * @throws PhpfastcacheEventManagerException |
|
179 | - */ |
|
180 | - public function setItemPoolContext(ExtendedCacheItemPoolInterface $pool): EventManagerInterface |
|
181 | - { |
|
182 | - if (!$this->isScopedEventManager) { |
|
183 | - throw new PhpfastcacheEventManagerException('Cannot set itemPool context on unscoped event manager instance.'); |
|
184 | - } |
|
185 | - $this->itemPoolContext = $pool; |
|
186 | - |
|
187 | - $this->onEveryEvents(function (string $eventName, ...$args) { |
|
188 | - EventManager::getInstance()->dispatch($eventName, ...$args); |
|
189 | - }, 'Scoped' . $pool->getDriverName() . spl_object_hash($this)); |
|
190 | - |
|
191 | - return $this; |
|
192 | - } |
|
27 | + use UninstanciableObjectTrait; |
|
28 | + |
|
29 | + public const ON_EVERY_EVENT = '__every'; |
|
30 | + |
|
31 | + protected static EventManagerInterface $instance; |
|
32 | + |
|
33 | + protected bool $isScopedEventManager = false; |
|
34 | + |
|
35 | + protected ?ExtendedCacheItemPoolInterface $itemPoolContext = null; |
|
36 | + |
|
37 | + /** @var array<string, array<string, callable>> */ |
|
38 | + protected array $events = [ |
|
39 | + self::ON_EVERY_EVENT => [] |
|
40 | + ]; |
|
41 | + |
|
42 | + /** |
|
43 | + * @return EventManagerInterface |
|
44 | + */ |
|
45 | + public static function getInstance(): EventManagerInterface |
|
46 | + { |
|
47 | + return (self::$instance ?? self::$instance = new static()); |
|
48 | + } |
|
49 | + |
|
50 | + /** |
|
51 | + * @param EventManagerInterface $eventManagerInstance |
|
52 | + * @return void |
|
53 | + */ |
|
54 | + public static function setInstance(EventManagerInterface $eventManagerInstance): void |
|
55 | + { |
|
56 | + self::$instance = $eventManagerInstance; |
|
57 | + } |
|
58 | + |
|
59 | + public function dispatch(string $eventName, ...$args): void |
|
60 | + { |
|
61 | + /** |
|
62 | + * Replace array_key_exists by isset |
|
63 | + * due to performance issue on huge |
|
64 | + * loop dispatching operations |
|
65 | + */ |
|
66 | + if (isset($this->events[$eventName]) && $eventName !== self::ON_EVERY_EVENT) { |
|
67 | + $loopArgs = array_merge($args, [$eventName]); |
|
68 | + foreach ($this->events[$eventName] as $event) { |
|
69 | + /** |
|
70 | + * @todo V10: BC Break: Put eventName as first parameter (like self::ON_EVERY_EVENT) |
|
71 | + */ |
|
72 | + $event(...$loopArgs); |
|
73 | + } |
|
74 | + } |
|
75 | + foreach ($this->events[self::ON_EVERY_EVENT] as $event) { |
|
76 | + $event($eventName, ...$args); |
|
77 | + } |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * @inheritDoc |
|
82 | + * @throws PhpfastcacheInvalidArgumentException |
|
83 | + * @throws PhpfastcacheEventManagerException |
|
84 | + */ |
|
85 | + public function __call(string $name, array $arguments): void |
|
86 | + { |
|
87 | + if (\str_starts_with($name, 'on')) { |
|
88 | + $name = \substr($name, 2); |
|
89 | + if (\is_callable($arguments[0])) { |
|
90 | + if (isset($arguments[1]) && \is_string($arguments[0])) { |
|
91 | + $this->events[$name][$arguments[1]] = $arguments[0]; |
|
92 | + } else { |
|
93 | + $this->events[$name][] = $arguments[0]; |
|
94 | + } |
|
95 | + } else { |
|
96 | + throw new PhpfastcacheInvalidArgumentException(\sprintf('Expected Callable, got "%s"', \gettype($arguments[0]))); |
|
97 | + } |
|
98 | + } else { |
|
99 | + throw new PhpfastcacheEventManagerException('An event must start with "on" such as "onCacheGetItem"'); |
|
100 | + } |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * @param callable $callback |
|
105 | + * @param string $callbackName |
|
106 | + * @throws PhpfastcacheEventManagerException |
|
107 | + */ |
|
108 | + public function onEveryEvents(callable $callback, string $callbackName): void |
|
109 | + { |
|
110 | + if ($callbackName === '') { |
|
111 | + throw new PhpfastcacheEventManagerException('Callbackname cannot be empty'); |
|
112 | + } |
|
113 | + $this->events[self::ON_EVERY_EVENT][$callbackName] = $callback; |
|
114 | + } |
|
115 | + |
|
116 | + |
|
117 | + /** |
|
118 | + * @throws PhpfastcacheEventManagerException |
|
119 | + */ |
|
120 | + public function on(array|string $events, callable $callback): void |
|
121 | + { |
|
122 | + if (is_string($events)) { |
|
123 | + $events = [$events]; |
|
124 | + } |
|
125 | + foreach ($events as $event) { |
|
126 | + if (!\preg_match('#^([a-zA-Z])*$#', $event)) { |
|
127 | + throw new PhpfastcacheEventManagerException(\sprintf('Invalid event name "%s"', $event)); |
|
128 | + } |
|
129 | + |
|
130 | + $this->{'on' . \ucfirst($event)}($callback); |
|
131 | + } |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * @param string $eventName |
|
136 | + * @param string $callbackName |
|
137 | + * @return bool |
|
138 | + */ |
|
139 | + public function unbindEventCallback(string $eventName, string $callbackName): bool |
|
140 | + { |
|
141 | + $return = isset($this->events[$eventName][$callbackName]); |
|
142 | + unset($this->events[$eventName][$callbackName]); |
|
143 | + |
|
144 | + return $return; |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * @return bool |
|
149 | + */ |
|
150 | + public function unbindAllEventCallbacks(): bool |
|
151 | + { |
|
152 | + $this->events = [ |
|
153 | + self::ON_EVERY_EVENT => [] |
|
154 | + ]; |
|
155 | + |
|
156 | + return true; |
|
157 | + } |
|
158 | + |
|
159 | + public function __clone(): void |
|
160 | + { |
|
161 | + $this->isScopedEventManager = true; |
|
162 | + $this->unbindAllEventCallbacks(); |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * @param ExtendedCacheItemPoolInterface $pool |
|
167 | + * @return EventManagerInterface |
|
168 | + * @throws PhpfastcacheEventManagerException |
|
169 | + */ |
|
170 | + public function getScopedEventManager(ExtendedCacheItemPoolInterface $pool): EventManagerInterface |
|
171 | + { |
|
172 | + return (clone $this)->setItemPoolContext($pool); |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * @param ExtendedCacheItemPoolInterface $pool |
|
177 | + * @return EventManagerInterface |
|
178 | + * @throws PhpfastcacheEventManagerException |
|
179 | + */ |
|
180 | + public function setItemPoolContext(ExtendedCacheItemPoolInterface $pool): EventManagerInterface |
|
181 | + { |
|
182 | + if (!$this->isScopedEventManager) { |
|
183 | + throw new PhpfastcacheEventManagerException('Cannot set itemPool context on unscoped event manager instance.'); |
|
184 | + } |
|
185 | + $this->itemPoolContext = $pool; |
|
186 | + |
|
187 | + $this->onEveryEvents(function (string $eventName, ...$args) { |
|
188 | + EventManager::getInstance()->dispatch($eventName, ...$args); |
|
189 | + }, 'Scoped' . $pool->getDriverName() . spl_object_hash($this)); |
|
190 | + |
|
191 | + return $this; |
|
192 | + } |
|
193 | 193 | } |
194 | 194 | |
195 | 195 | // phpcs:disable |
@@ -117,7 +117,7 @@ discard block |
||
117 | 117 | /** |
118 | 118 | * @throws PhpfastcacheEventManagerException |
119 | 119 | */ |
120 | - public function on(array|string $events, callable $callback): void |
|
120 | + public function on(array | string $events, callable $callback): void |
|
121 | 121 | { |
122 | 122 | if (is_string($events)) { |
123 | 123 | $events = [$events]; |
@@ -184,7 +184,7 @@ discard block |
||
184 | 184 | } |
185 | 185 | $this->itemPoolContext = $pool; |
186 | 186 | |
187 | - $this->onEveryEvents(function (string $eventName, ...$args) { |
|
187 | + $this->onEveryEvents(function(string $eventName, ...$args) { |
|
188 | 188 | EventManager::getInstance()->dispatch($eventName, ...$args); |
189 | 189 | }, 'Scoped' . $pool->getDriverName() . spl_object_hash($this)); |
190 | 190 |
@@ -89,13 +89,16 @@ |
||
89 | 89 | if (\is_callable($arguments[0])) { |
90 | 90 | if (isset($arguments[1]) && \is_string($arguments[0])) { |
91 | 91 | $this->events[$name][$arguments[1]] = $arguments[0]; |
92 | - } else { |
|
92 | + } |
|
93 | + else { |
|
93 | 94 | $this->events[$name][] = $arguments[0]; |
94 | 95 | } |
95 | - } else { |
|
96 | + } |
|
97 | + else { |
|
96 | 98 | throw new PhpfastcacheInvalidArgumentException(\sprintf('Expected Callable, got "%s"', \gettype($arguments[0]))); |
97 | 99 | } |
98 | - } else { |
|
100 | + } |
|
101 | + else { |
|
99 | 102 | throw new PhpfastcacheEventManagerException('An event must start with "on" such as "onCacheGetItem"'); |
100 | 103 | } |
101 | 104 | } |
@@ -21,102 +21,102 @@ |
||
21 | 21 | |
22 | 22 | interface TaggableCacheItemInterface |
23 | 23 | { |
24 | - /** |
|
25 | - * Allows you to get cache item(s) by at least **ONE** of the specified matching tag(s). |
|
26 | - * Default behavior |
|
27 | - */ |
|
28 | - public const TAG_STRATEGY_ONE = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE; |
|
29 | - |
|
30 | - /** |
|
31 | - * Allows you to get cache item(s) by **ALL** of the specified matching tag(s) |
|
32 | - * The cache item *CAN* have additional tag(s) |
|
33 | - */ |
|
34 | - public const TAG_STRATEGY_ALL = TaggableCacheItemPoolInterface::TAG_STRATEGY_ALL; |
|
35 | - |
|
36 | - /** |
|
37 | - * Allows you to get cache item(s) by **ONLY** the specified matching tag(s) |
|
38 | - * The cache item *CANNOT* have additional tag(s) |
|
39 | - */ |
|
40 | - public const TAG_STRATEGY_ONLY = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONLY; |
|
41 | - |
|
42 | - /** |
|
43 | - * @param string $tagName |
|
44 | - * |
|
45 | - * @return ExtendedCacheItemInterface |
|
46 | - * @throws PhpfastcacheInvalidArgumentException |
|
47 | - */ |
|
48 | - public function addTag(string $tagName): ExtendedCacheItemInterface; |
|
49 | - |
|
50 | - /** |
|
51 | - * @param string[] $tagNames |
|
52 | - * |
|
53 | - * @return ExtendedCacheItemInterface |
|
54 | - * @throws PhpfastcacheInvalidArgumentException |
|
55 | - */ |
|
56 | - public function addTags(array $tagNames): ExtendedCacheItemInterface; |
|
57 | - |
|
58 | - /** |
|
59 | - * @param string $tagName |
|
60 | - * |
|
61 | - * @return bool |
|
62 | - */ |
|
63 | - public function hasTag(string $tagName): bool; |
|
64 | - |
|
65 | - /** |
|
66 | - * @return bool |
|
67 | - */ |
|
68 | - public function isTagged(): bool; |
|
69 | - |
|
70 | - /** |
|
71 | - * @param string[] $tagNames |
|
72 | - * @param int $strategy |
|
73 | - * @return bool |
|
74 | - * @throws PhpfastcacheInvalidArgumentException |
|
75 | - */ |
|
76 | - public function hasTags(array $tagNames, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
77 | - |
|
78 | - /** |
|
79 | - * @param string[] $tags |
|
80 | - * |
|
81 | - * @return ExtendedCacheItemInterface |
|
82 | - * @throws PhpfastcacheInvalidArgumentException |
|
83 | - */ |
|
84 | - public function setTags(array $tags): ExtendedCacheItemInterface; |
|
85 | - |
|
86 | - /** |
|
87 | - * @return string[] |
|
88 | - */ |
|
89 | - public function getTags(): array; |
|
90 | - |
|
91 | - /** |
|
92 | - * @param string $separator |
|
93 | - * |
|
94 | - * @return string |
|
95 | - */ |
|
96 | - public function getTagsAsString(string $separator = ', '): string; |
|
97 | - |
|
98 | - /** |
|
99 | - * @param string $tagName |
|
100 | - * |
|
101 | - * @return ExtendedCacheItemInterface |
|
102 | - * @throws PhpfastcacheInvalidArgumentException |
|
103 | - */ |
|
104 | - public function removeTag(string $tagName): ExtendedCacheItemInterface; |
|
105 | - |
|
106 | - /** |
|
107 | - * @param string[] $tagNames |
|
108 | - * |
|
109 | - * @return ExtendedCacheItemInterface |
|
110 | - */ |
|
111 | - public function removeTags(array $tagNames): ExtendedCacheItemInterface; |
|
112 | - |
|
113 | - /** |
|
114 | - * @return string[] |
|
115 | - */ |
|
116 | - public function getRemovedTags(): array; |
|
117 | - |
|
118 | - /** |
|
119 | - * @return ExtendedCacheItemInterface |
|
120 | - */ |
|
121 | - public function clearRemovedTags(): ExtendedCacheItemInterface; |
|
24 | + /** |
|
25 | + * Allows you to get cache item(s) by at least **ONE** of the specified matching tag(s). |
|
26 | + * Default behavior |
|
27 | + */ |
|
28 | + public const TAG_STRATEGY_ONE = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE; |
|
29 | + |
|
30 | + /** |
|
31 | + * Allows you to get cache item(s) by **ALL** of the specified matching tag(s) |
|
32 | + * The cache item *CAN* have additional tag(s) |
|
33 | + */ |
|
34 | + public const TAG_STRATEGY_ALL = TaggableCacheItemPoolInterface::TAG_STRATEGY_ALL; |
|
35 | + |
|
36 | + /** |
|
37 | + * Allows you to get cache item(s) by **ONLY** the specified matching tag(s) |
|
38 | + * The cache item *CANNOT* have additional tag(s) |
|
39 | + */ |
|
40 | + public const TAG_STRATEGY_ONLY = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONLY; |
|
41 | + |
|
42 | + /** |
|
43 | + * @param string $tagName |
|
44 | + * |
|
45 | + * @return ExtendedCacheItemInterface |
|
46 | + * @throws PhpfastcacheInvalidArgumentException |
|
47 | + */ |
|
48 | + public function addTag(string $tagName): ExtendedCacheItemInterface; |
|
49 | + |
|
50 | + /** |
|
51 | + * @param string[] $tagNames |
|
52 | + * |
|
53 | + * @return ExtendedCacheItemInterface |
|
54 | + * @throws PhpfastcacheInvalidArgumentException |
|
55 | + */ |
|
56 | + public function addTags(array $tagNames): ExtendedCacheItemInterface; |
|
57 | + |
|
58 | + /** |
|
59 | + * @param string $tagName |
|
60 | + * |
|
61 | + * @return bool |
|
62 | + */ |
|
63 | + public function hasTag(string $tagName): bool; |
|
64 | + |
|
65 | + /** |
|
66 | + * @return bool |
|
67 | + */ |
|
68 | + public function isTagged(): bool; |
|
69 | + |
|
70 | + /** |
|
71 | + * @param string[] $tagNames |
|
72 | + * @param int $strategy |
|
73 | + * @return bool |
|
74 | + * @throws PhpfastcacheInvalidArgumentException |
|
75 | + */ |
|
76 | + public function hasTags(array $tagNames, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
77 | + |
|
78 | + /** |
|
79 | + * @param string[] $tags |
|
80 | + * |
|
81 | + * @return ExtendedCacheItemInterface |
|
82 | + * @throws PhpfastcacheInvalidArgumentException |
|
83 | + */ |
|
84 | + public function setTags(array $tags): ExtendedCacheItemInterface; |
|
85 | + |
|
86 | + /** |
|
87 | + * @return string[] |
|
88 | + */ |
|
89 | + public function getTags(): array; |
|
90 | + |
|
91 | + /** |
|
92 | + * @param string $separator |
|
93 | + * |
|
94 | + * @return string |
|
95 | + */ |
|
96 | + public function getTagsAsString(string $separator = ', '): string; |
|
97 | + |
|
98 | + /** |
|
99 | + * @param string $tagName |
|
100 | + * |
|
101 | + * @return ExtendedCacheItemInterface |
|
102 | + * @throws PhpfastcacheInvalidArgumentException |
|
103 | + */ |
|
104 | + public function removeTag(string $tagName): ExtendedCacheItemInterface; |
|
105 | + |
|
106 | + /** |
|
107 | + * @param string[] $tagNames |
|
108 | + * |
|
109 | + * @return ExtendedCacheItemInterface |
|
110 | + */ |
|
111 | + public function removeTags(array $tagNames): ExtendedCacheItemInterface; |
|
112 | + |
|
113 | + /** |
|
114 | + * @return string[] |
|
115 | + */ |
|
116 | + public function getRemovedTags(): array; |
|
117 | + |
|
118 | + /** |
|
119 | + * @return ExtendedCacheItemInterface |
|
120 | + */ |
|
121 | + public function clearRemovedTags(): ExtendedCacheItemInterface; |
|
122 | 122 | } |