|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Micro framework package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Stanislau Komar <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Micro\Plugin\Redis\Configuration; |
|
15
|
|
|
|
|
16
|
|
|
use Micro\Framework\Kernel\Configuration\Exception\InvalidConfigurationException; |
|
17
|
|
|
use Micro\Framework\Kernel\Configuration\PluginRoutingKeyConfiguration; |
|
18
|
|
|
|
|
19
|
|
|
class RedisClientConfiguration extends PluginRoutingKeyConfiguration implements RedisClientConfigurationInterface |
|
20
|
|
|
{ |
|
21
|
|
|
public const CONNECTION_TYPE_DEFAULT = RedisClientConfigurationInterface::CONNECTION_TYPE_NET; |
|
22
|
|
|
public const HOST_DEFAULT = 'localhost'; |
|
23
|
|
|
public const PORT_DEFAULT = 6379; |
|
24
|
|
|
public const CONNECTION_TIMEOUT_DEFAULT = 0.0; |
|
25
|
|
|
public const READ_TIMEOUT_DEFAULT = 0.0; |
|
26
|
|
|
public const CONNECTION_RETRY_INTERNAL_DEFAULT = 0; |
|
27
|
|
|
public const CONNECTION_REUSE_DEFAULT = false; |
|
28
|
|
|
|
|
29
|
|
|
protected const CFG_CONNECTION_TYPE = 'REDIS_%s_CONNECTION_TYPE'; |
|
30
|
|
|
protected const CFG_CONNECTION_REUSE = 'REDIS_%s_CONNECTION_REUSE'; |
|
31
|
|
|
protected const CFG_CONNECTION_HOST = 'REDIS_%s_HOST'; |
|
32
|
|
|
protected const CFG_CONNECTION_PORT = 'REDIS_%s_PORT'; |
|
33
|
|
|
protected const CFG_CONNECTION_TIMEOUT = 'REDIS_%s_TIMEOUT'; |
|
34
|
|
|
protected const CFG_CONNECTION_RETRY_INTERVAL = 'REDIS_%s_RETRY_INTERVAL'; |
|
35
|
|
|
protected const CFG_READ_TIMEOUT = 'REDIS_%s_READ_TIMEOUT'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritDoc} |
|
39
|
|
|
*/ |
|
40
|
2 |
|
public function name(): string |
|
41
|
|
|
{ |
|
42
|
2 |
|
return $this->configRoutingKey; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritDoc} |
|
47
|
|
|
*/ |
|
48
|
4 |
|
public function reuseConnection(): bool |
|
49
|
|
|
{ |
|
50
|
4 |
|
return $this->get(self::CFG_CONNECTION_REUSE, self::CONNECTION_REUSE_DEFAULT); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritDoc} |
|
55
|
|
|
*/ |
|
56
|
4 |
|
public function connectionType(): string |
|
57
|
|
|
{ |
|
58
|
4 |
|
$connectionType = $this->get(self::CFG_CONNECTION_TYPE, self::CONNECTION_TYPE_DEFAULT); |
|
59
|
4 |
|
if (!\in_array($connectionType, $this->getAvailableConnectionTypes(), true)) { |
|
60
|
1 |
|
throw new InvalidConfigurationException(sprintf('Configuration key "%s" has invalid value. Redis: connection type %s is not available', $this->cfg(self::CFG_CONNECTION_TYPE), $connectionType)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
3 |
|
return $connectionType; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritDoc} |
|
68
|
|
|
*/ |
|
69
|
4 |
|
public function host(): string |
|
70
|
|
|
{ |
|
71
|
4 |
|
return $this->get(self::CFG_CONNECTION_HOST, self::HOST_DEFAULT); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritDoc} |
|
76
|
|
|
*/ |
|
77
|
4 |
|
public function port(): int |
|
78
|
|
|
{ |
|
79
|
4 |
|
return (int) $this->get(self::CFG_CONNECTION_PORT, self::PORT_DEFAULT); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritDoc} |
|
84
|
|
|
*/ |
|
85
|
4 |
|
public function connectionTimeout(): float |
|
86
|
|
|
{ |
|
87
|
4 |
|
return (float) $this->get(self::CFG_CONNECTION_TIMEOUT, self::CONNECTION_TIMEOUT_DEFAULT); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
4 |
|
public function readTimeout(): float |
|
91
|
|
|
{ |
|
92
|
4 |
|
return (float) $this->get(self::CFG_READ_TIMEOUT, self::READ_TIMEOUT_DEFAULT); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* {@inheritDoc} |
|
97
|
|
|
*/ |
|
98
|
2 |
|
public function sslConfiguration(): SslConfigurationInterface |
|
99
|
|
|
{ |
|
100
|
2 |
|
return new SslConfiguration($this->configuration, $this->configRoutingKey); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* {@inheritDoc} |
|
105
|
|
|
*/ |
|
106
|
2 |
|
public function authorizationConfiguration(): AuthorizationConfigurationInterface |
|
107
|
|
|
{ |
|
108
|
2 |
|
return new AuthorizationConfiguration($this->configuration, $this->configRoutingKey); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* {@inheritDoc} |
|
113
|
|
|
*/ |
|
114
|
2 |
|
public function clientOptionsConfiguration(): ClientOptionsConfigurationInterface |
|
115
|
|
|
{ |
|
116
|
2 |
|
return new ClientOptionsConfiguration($this->configuration, $this->configRoutingKey); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* {@inheritDoc} |
|
121
|
|
|
*/ |
|
122
|
4 |
|
public function retryInterval(): int |
|
123
|
|
|
{ |
|
124
|
4 |
|
return (int) $this->get(self::CFG_CONNECTION_RETRY_INTERVAL, self::CONNECTION_RETRY_INTERNAL_DEFAULT); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return string[] |
|
129
|
|
|
*/ |
|
130
|
4 |
|
protected function getAvailableConnectionTypes(): array |
|
131
|
|
|
{ |
|
132
|
4 |
|
return [ |
|
133
|
4 |
|
RedisClientConfigurationInterface::CONNECTION_TYPE_NET, |
|
134
|
4 |
|
RedisClientConfigurationInterface::CONNECTION_TYPE_UNIX, |
|
135
|
4 |
|
]; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|