forked from shumkov/rediska
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Commands.php
195 lines (171 loc) · 7.37 KB
/
Commands.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
* Rediska commands
*
* @author Ivan Shumkov
* @package Rediska
* @subpackage Commands
* @version @package_version@
* @link http://rediska.geometria-lab.net
* @license http://www.opensource.org/licenses/bsd-license.php
*/
class Rediska_Commands
{
/**
* Redis commands
*
* @var array
*/
protected static $_commands = array(
// Connection handling
'quit' => 'Rediska_Command_Quit',
// Commands operating on all value types
'exists' => 'Rediska_Command_Exists',
'delete' => 'Rediska_Command_Delete',
'gettype' => 'Rediska_Command_GetType',
'getkeysbypattern' => 'Rediska_Command_GetKeysByPattern',
'getrandomkey' => 'Rediska_Command_GetRandomKey',
'rename' => 'Rediska_Command_Rename',
'getkeyscount' => 'Rediska_Command_GetKeysCount',
'expire' => 'Rediska_Command_Expire',
'getlifetime' => 'Rediska_Command_GetLifetime',
'selectdb' => 'Rediska_Command_SelectDb',
'movetodb' => 'Rediska_Command_MoveToDb',
'flushdb' => 'Rediska_Command_FlushDb',
// Commands operating on string values
'set' => 'Rediska_Command_Set',
'setandget' => 'Rediska_Command_SetAndGet',
'get' => 'Rediska_Command_Get',
'setandexpire' => 'Rediska_Command_SetAndExpire',
'increment' => 'Rediska_Command_Increment',
'decrement' => 'Rediska_Command_Decrement',
'substring' => 'Rediska_Command_Substring',
'append' => 'Rediska_Command_Append',
// Commands operating on lists
'appendtolist' => 'Rediska_Command_AppendToList',
'prependtolist' => 'Rediska_Command_PrependToList',
'getlistlength' => 'Rediska_Command_GetListLength',
'getlist' => 'Rediska_Command_GetList',
'truncatelist' => 'Rediska_Command_TruncateList',
'getfromlist' => 'Rediska_Command_GetFromList',
'settolist' => 'Rediska_Command_SetToList',
'deletefromlist' => 'Rediska_Command_DeleteFromList',
'shiftfromlist' => 'Rediska_Command_ShiftFromList',
'shiftfromlistblocking' => 'Rediska_Command_ShiftFromListBlocking',
'popfromlist' => 'Rediska_Command_PopFromList',
'popfromlistblocking' => 'Rediska_Command_PopFromListBlocking',
// Commands operating on sets
'addtoset' => 'Rediska_Command_AddToSet',
'deletefromset' => 'Rediska_Command_DeleteFromSet',
'getrandomfromset' => 'Rediska_Command_GetRandomFromSet',
'getsetlength' => 'Rediska_Command_GetSetLength',
'existsinset' => 'Rediska_Command_ExistsInSet',
'intersectsets' => 'Rediska_Command_IntersectSets',
'unionsets' => 'Rediska_Command_UnionSets',
'diffsets' => 'Rediska_Command_DiffSets',
'getset' => 'Rediska_Command_GetSet',
'movetoset' => 'Rediska_Command_MoveToSet',
// Commands operating on sorted sets
'addtosortedset' => 'Rediska_Command_AddToSortedSet',
'deletefromsortedset' => 'Rediska_Command_DeleteFromSortedSet',
'getsortedset' => 'Rediska_Command_GetSortedSet',
'getfromsortedsetbyscore' => 'Rediska_Command_GetFromSortedSetByScore',
'getsortedsetlength' => 'Rediska_Command_GetSortedSetLength',
'getsortedsetlengthbyscore' => 'Rediska_Command_GetSortedSetLengthByScore',
'incrementscoreinsortedset' => 'Rediska_Command_IncrementScoreInSortedSet',
'deletefromsortedsetbyscore' => 'Rediska_Command_DeleteFromSortedSetByScore',
'deletefromsortedsetbyrank' => 'Rediska_Command_DeleteFromSortedSetByRank',
'getscorefromsortedset' => 'Rediska_Command_GetScoreFromSortedSet',
'getrankfromsortedset' => 'Rediska_Command_GetRankFromSortedSet',
'unionsortedsets' => 'Rediska_Command_UnionSortedSets',
'intersectsortedsets' => 'Rediska_Command_IntersectSortedSets',
// Commands operating on hashes
'settohash' => 'Rediska_Command_SetToHash',
'getfromhash' => 'Rediska_Command_GetFromHash',
'incrementinhash' => 'Rediska_Command_IncrementInHash',
'existsinhash' => 'Rediska_Command_ExistsInHash',
'deletefromhash' => 'Rediska_Command_DeleteFromHash',
'gethashlength' => 'Rediska_Command_GetHashLength',
'gethash' => 'Rediska_Command_GetHash',
'gethashfields' => 'Rediska_Command_GetHashFields',
'gethashvalues' => 'Rediska_Command_GetHashValues',
// Sorting
'sort' => 'Rediska_Command_Sort',
// Publish/Subscribe
'publish' => 'Rediska_Command_Publish',
// Persistence control commands
'save' => 'Rediska_Command_Save',
'getlastsavetime' => 'Rediska_Command_GetLastSaveTime',
'shutdown' => 'Rediska_Command_Shutdown',
'rewriteappendonlyfile' => 'Rediska_Command_RewriteAppendOnlyFile',
// Remote server control commands
'info' => 'Rediska_Command_Info',
'slaveof' => 'Rediska_Command_SlaveOf'
);
/**
* Add command
*
* @param string $name Command name
* @param string $className Name of class
*/
public static function add($name, $className)
{
if (!class_exists($className)) {
throw new Rediska_Exception("Class '$className' not found. You must include before or setup autoload");
}
$classReflection = new ReflectionClass($className);
if (!in_array('Rediska_Command_Interface', $classReflection->getInterfaceNames())) {
throw new Rediska_Exception("Class '$className' must implement Rediska_Command_Interface interface");
}
$lowerName = strtolower($name);
self::$_commands[$lowerName] = $className;
return true;
}
/**
* Remove command
*
* @param string $name Command name
*/
public static function remove($name)
{
$lowerName = self::_getCommandLowerNameAndThrowIfNotPresent($name);
unset(self::$_commands[$lowerName]);
return true;
}
/**
* Get command instance
*
* @param Rediska $rediska Rediska instance
* @param string $name Command name
* @param array $arguments Command arguments
* @return Rediska_Command_Abstract
*/
public static function get(Rediska $rediska, $name, $arguments)
{
$lowerName = self::_getCommandLowerNameAndThrowIfNotPresent($name);
return new self::$_commands[$lowerName]($rediska, $name, $arguments);
}
/**
* Get command list
*
* @return array
*/
public static function getList()
{
return self::$_commands;
}
/**
* Get command lower name and throw exception if command not present
*
* @param <type> $name
* @return <type>
*/
protected static function _getCommandLowerNameAndThrowIfNotPresent($name)
{
$lowerName = strtolower($name);
if (!isset(self::$_commands[$lowerName])) {
throw new Rediska_Exception("Command '$name' not found");
}
return $lowerName;
}
}