-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathAbstractPipeline.php
158 lines (141 loc) · 4.33 KB
/
AbstractPipeline.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
<?php
/**
* This file is part of RedisClient.
* git: https://github.com/cheprasov/php-redis-client
*
* (C) Alexander Cheprasov <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace RedisClient\Pipeline;
use RedisClient\Client\AbstractRedisClient;
use RedisClient\Command\Response\ResponseParser;
use RedisClient\Exception\CommandNotFoundException;
use RedisClient\Exception\ErrorException;
abstract class AbstractPipeline implements PipelineInterface {
/**
* @var array[]
*/
protected $commandLines = [];
/**
* @var string
*/
protected $keys = [];
/**
* @var int
*/
protected $transactionMode = AbstractRedisClient::TRANSACTION_MODE_NONE;
/**
* @param \Closure|null $Closure
*/
public function __construct(\Closure $Closure = null) {
if ($Closure) {
$Closure($this);
}
}
/**
* @param string|string[] $keys
*/
protected function addKeys($keys) {
if (is_array($keys)) {
foreach ($keys as $key) {
$this->keys[$key] = $key;
}
} else {
$this->keys[$keys] = $keys;
}
}
/**
* @return string[]
*/
public function getKeys() {
return array_values($this->keys);
}
/**
* @inheritdoc
* @return $this
*/
protected function returnCommand(array $command, $keys = null, array $params = null, $parserId = null) {
$this->commandLines[] = [$command, $params, $parserId];
if ($keys) {
$this->addKeys($keys);
}
return $this;
}
/**
* @inheritdoc
*/
protected function subscribeCommand(array $subCommand, array $unsubCommand, ?array $params, $callback) {
throw new ErrorException('Do not use subscribe in pipeline');
}
/**
* @param int $transactionMode
*/
protected function setTransactionMode($transactionMode) {
$this->transactionMode = $transactionMode;
}
/**
* @return array[]
*/
public function getStructure() {
$structures = [];
foreach ($this->commandLines as $commandLine) {
$structure = $commandLine[0];
if (isset($commandLine[1])) {
foreach ($commandLine[1] as $params) {
if (is_array($params)) {
foreach ($params as $param) {
$structure[] = $param;
}
} else {
$structure[] = $params;
}
}
}
$structures[] = $structure;
}
return $structures;
}
/**
* @param array $responses
* @return mixed
*/
public function parseResponse($responses) {
if ($this->transactionMode === AbstractRedisClient::TRANSACTION_MODE_NONE) {
foreach ($responses as $n => $response) {
if (empty($this->commandLines[$n][2])) {
// todo: check
continue;
}
$responses[$n] = ResponseParser::parse($this->commandLines[$n][2], $response);
}
return $responses;
}
if ($this->transactionMode === AbstractRedisClient::TRANSACTION_MODE_EXECUTED) {
$this->transactionMode = AbstractRedisClient::TRANSACTION_MODE_NONE;
$execResponses = array_pop($responses);
foreach ($execResponses as $n => $response) {
// MULTI command is ignored
if (empty($this->commandLines[$n + 1][2])) {
continue;
}
$execResponses[$n] = ResponseParser::parse($this->commandLines[$n + 1][2], $response);
}
$responses[] = $execResponses;
}
return $responses;
}
/**
* @param string $name
* @param array $arguments
* @return mixed
* @throws \Exception
*/
public function __call($name , array $arguments) {
if ($method = $this->getMethodNameForReservedWord($name)) {
return call_user_func_array([$this, $method], $arguments);
}
throw new CommandNotFoundException('Call to undefined method '. static::class. '::'. $name);
}
}