1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kazuakim\Reddish; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Clients. |
7
|
|
|
* |
8
|
|
|
* @property array $config |
9
|
|
|
* @property array $_defaultConfig |
10
|
|
|
* |
11
|
|
|
* @copyright KazuakiM <[email protected]> |
12
|
|
|
* @author KazuakiM <[email protected]> |
13
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License |
14
|
|
|
* |
15
|
|
|
* @link https://github.com/KazuakiM/reddish |
16
|
|
|
*/ |
17
|
|
|
class Clients extends \Redis //{{{ |
18
|
|
|
{ |
19
|
|
|
protected $config; |
20
|
|
|
private $_defaultConfig = [ |
21
|
|
|
'host' => null, //can be a host, or the path to a unix domain socket |
22
|
|
|
'port' => 6379, |
23
|
|
|
'timeout' => 0.0, //value in seconds (optional, default is 0 meaning unlimited) |
24
|
|
|
'reserved' => null, //should be NULL if retry_interval is specified |
25
|
|
|
'persistent_id' => '', //identity for the requested persistent connection |
26
|
|
|
'password' => null, |
27
|
|
|
'serializer' => \Redis::SERIALIZER_NONE, |
28
|
|
|
'persistent' => false, //default is connect |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* __construct. |
33
|
|
|
* |
34
|
|
|
* @param array $config |
35
|
|
|
* |
36
|
|
|
* @throws \RedisException |
37
|
|
|
*/ |
38
|
7 |
|
public function __construct(array $config) //{{{ |
39
|
|
|
{ |
40
|
7 |
|
$this->config = array_merge($this->_defaultConfig, $config); |
41
|
|
|
|
42
|
7 |
|
$this->connection(); |
43
|
7 |
|
} //}}} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* connection. |
47
|
|
|
* |
48
|
|
|
* @throws \RedisException |
49
|
|
|
* |
50
|
|
|
* @return \Kazuakim\Reddish\Clients |
51
|
|
|
*/ |
52
|
7 |
|
public function connection(): \Kazuakim\Reddish\Clients //{{{ |
53
|
|
|
{ |
54
|
|
|
// connect |
55
|
7 |
|
if (!$this->config['persistent'] && !@$this->connect($this->config['host'], $this->config['port'], $this->config['timeout'], $this->config['reserved'])) { |
|
|
|
|
56
|
1 |
|
throw new \RedisException('connect errored.'); |
57
|
7 |
|
} elseif (!@$this->pconnect($this->config['host'], $this->config['port'], $this->config['timeout'], $this->config['persistent_id'])) { |
|
|
|
|
58
|
1 |
|
throw new \RedisException('pconnect errored.'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// auth |
62
|
7 |
|
if (0 < strlen($this->config['password']) && !@$this->auth($this->config['password'])) { |
63
|
1 |
|
throw new \RedisException('auth errored.'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// serializer |
67
|
7 |
|
$this->setSerializer($this->config['serializer']); |
68
|
|
|
|
69
|
7 |
|
return $this; |
70
|
|
|
} //}}} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* setSerializer. |
74
|
|
|
* |
75
|
|
|
* @param int $serializer |
76
|
|
|
* |
77
|
|
|
* @throws \Expectation |
78
|
|
|
* |
79
|
|
|
* @return \Kazuakim\Reddish\Clients |
80
|
|
|
*/ |
81
|
7 |
|
public function setSerializer($serializer): \Kazuakim\Reddish\Clients //{{{ |
82
|
|
|
{ |
83
|
7 |
|
assert(in_array($serializer, self::_getSerializerArray(), true), 'serializer setting errored.'); |
84
|
|
|
|
85
|
7 |
|
$this->setOption(\Redis::OPT_SERIALIZER, $serializer); |
86
|
|
|
|
87
|
7 |
|
return $this; |
88
|
|
|
} //}}} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* close. |
92
|
|
|
*/ |
93
|
3 |
|
public function close() //{{{ |
94
|
|
|
{ |
95
|
3 |
|
if ($this->config['persistent']) { |
96
|
3 |
|
parent::close(); |
97
|
|
|
} |
98
|
|
|
|
99
|
3 |
|
return; |
100
|
|
|
} //}}} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* _getSerializerArray. |
104
|
|
|
* |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
7 |
|
private static function _getSerializerArray(): array //{{{ |
108
|
|
|
{ |
109
|
7 |
|
if (false === extension_loaded('igbinary')) { |
110
|
|
|
return [ |
111
|
7 |
|
\Redis::SERIALIZER_NONE, |
112
|
|
|
\Redis::SERIALIZER_PHP, |
113
|
|
|
]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return [ |
117
|
|
|
\Redis::SERIALIZER_NONE, |
118
|
|
|
\Redis::SERIALIZER_PHP, |
119
|
|
|
\Redis::SERIALIZER_IGBINARY, |
120
|
|
|
]; |
121
|
|
|
} //}}} |
122
|
|
|
} //}}} |
123
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.