1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kazuakim\Reddish; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Clients. |
7
|
|
|
* |
8
|
|
|
* @copyright KazuakiM <[email protected]> |
9
|
|
|
* @author KazuakiM <[email protected]> |
10
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License |
11
|
|
|
* |
12
|
|
|
* @link https://github.com/KazuakiM/reddish |
13
|
|
|
*/ |
14
|
|
|
class Clients extends \Redis //{{{ |
15
|
|
|
{ |
16
|
|
|
protected $config; |
17
|
|
|
private static $_defaultConfig = [ |
18
|
|
|
'host' => null, //can be a host, or the path to a unix domain socket |
19
|
|
|
'port' => 6379, |
20
|
|
|
'timeout' => 0.0, //value in seconds (optional, default is 0 meaning unlimited) |
21
|
|
|
'reserved' => null, //should be NULL if retry_interval is specified |
22
|
|
|
'retry_interval' => null, //value in milliseconds |
23
|
|
|
'persistent_id' => '', //identity for the requested persistent connection |
24
|
|
|
'password' => null, |
25
|
|
|
'serializer' => \Redis::SERIALIZER_NONE, |
26
|
|
|
'persistent' => false, //default is connect |
27
|
|
|
]; |
28
|
|
|
|
29
|
5 |
|
public function __construct(array $config) //{{{ |
30
|
|
|
{ |
31
|
5 |
|
$this->config = array_merge(self::$_defaultConfig, $config); |
32
|
|
|
|
33
|
5 |
|
$this->connection(); |
34
|
3 |
|
} //}}} |
35
|
|
|
|
36
|
5 |
|
public function connection() //{{{ |
37
|
|
|
{ |
38
|
|
|
// connect |
39
|
5 |
|
if (!$this->config['persistent'] && !@$this->connect($this->config['host'], $this->config['port'], $this->config['timeout'], $this->config['reserved'], $this->config['retry_interval'])) { |
|
|
|
|
40
|
1 |
|
throw new ReddishException('connect errored.'); |
41
|
4 |
|
} elseif (!@$this->pconnect($this->config['host'], $this->config['port'], $this->config['timeout'], $this->config['persistent_id'], $this->config['retry_interval'])) { |
|
|
|
|
42
|
1 |
|
throw new ReddishException('pconnect errored.'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// auth |
46
|
3 |
|
if (0 < strlen($this->config['password']) && !$this->auth($this->config['password'])) { |
47
|
|
|
throw new ReddishException('auth errored.'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// serializer |
51
|
3 |
|
$this->setSerializer($this->config['serializer']); |
52
|
3 |
|
} //}}} |
53
|
|
|
|
54
|
3 |
|
public function setSerializer($serializer) //{{{ |
55
|
|
|
{ |
56
|
3 |
|
assert(in_array($serializer, self::_getSerializerArray(), true), 'serializer setting errored.'); |
57
|
|
|
|
58
|
3 |
|
if (!$this->setOption(\Redis::OPT_SERIALIZER, $serializer)) { |
59
|
|
|
throw new ReddishException('serializer errored.'); |
60
|
|
|
} |
61
|
3 |
|
} //}}} |
62
|
|
|
|
63
|
2 |
|
public function close() //{{{ |
64
|
|
|
{ |
65
|
2 |
|
if ($this->config['persistent']) { |
66
|
2 |
|
parent::close(); |
67
|
|
|
} |
68
|
2 |
|
} //}}} |
69
|
|
|
|
70
|
2 |
|
public function ping() //{{{ |
71
|
|
|
{ |
72
|
|
|
try { |
73
|
2 |
|
parent::ping(); |
74
|
1 |
|
} catch (\RedisException $e) { |
75
|
1 |
|
throw new ReddishException($e->getMessage()); |
76
|
|
|
} |
77
|
1 |
|
} //}}} |
78
|
|
|
|
79
|
3 |
|
private static function _getSerializerArray(): array //{{{ |
80
|
|
|
{ |
81
|
3 |
|
if (extension_loaded('igbinary') === false) { |
82
|
|
|
return [ |
83
|
3 |
|
\Redis::SERIALIZER_NONE, |
84
|
|
|
\Redis::SERIALIZER_PHP, |
85
|
|
|
]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return [ |
89
|
|
|
\Redis::SERIALIZER_NONE, |
90
|
|
|
\Redis::SERIALIZER_PHP, |
91
|
|
|
\Redis::SERIALIZER_IGBINARY, |
92
|
|
|
]; |
93
|
|
|
} //}}} |
94
|
|
|
} //}}} |
95
|
|
|
|
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.