1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gielfeldt\TransactionalPHP; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Operation |
7
|
|
|
* |
8
|
|
|
* @package Gielfeldt\TransactionalPHP |
9
|
|
|
*/ |
10
|
|
|
class Operation |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The index in the buffer, keyed by connection id. |
14
|
|
|
* |
15
|
|
|
* @var int[] |
16
|
|
|
*/ |
17
|
|
|
protected $idx; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Callback for commit. |
21
|
|
|
* |
22
|
|
|
* @var callable[] |
23
|
|
|
*/ |
24
|
|
|
protected $commit = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Callback for rollback. |
28
|
|
|
* |
29
|
|
|
* @var callable[] |
30
|
|
|
*/ |
31
|
|
|
protected $rollback = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Callback for buffer. |
35
|
|
|
* |
36
|
|
|
* @var callable[] |
37
|
|
|
*/ |
38
|
|
|
protected $buffer = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Callback for remove. |
42
|
|
|
* |
43
|
|
|
* @var callable[] |
44
|
|
|
*/ |
45
|
|
|
protected $remove = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Metadata for operation. |
49
|
|
|
* |
50
|
|
|
* @var mixed[] |
51
|
|
|
*/ |
52
|
|
|
protected $metadata = []; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Result of callback execution. |
56
|
|
|
* |
57
|
|
|
* @var mixed |
58
|
|
|
*/ |
59
|
|
|
protected $result; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Set commit callback. |
63
|
|
|
* |
64
|
|
|
* @param callable $callback |
65
|
|
|
* The callback when this operation is committed. |
66
|
|
|
* |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
1 |
|
public function onCommit(callable $callback) |
70
|
|
|
{ |
71
|
1 |
|
$this->commit[] = $callback; |
72
|
1 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Set rollback callback. |
77
|
|
|
* |
78
|
|
|
* @param callable $callback |
79
|
|
|
* The callback when this operation is rolled back. |
80
|
|
|
* |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
1 |
|
public function onRollback(callable $callback) |
84
|
|
|
{ |
85
|
1 |
|
$this->rollback[] = $callback; |
86
|
1 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Set rollback callback. |
91
|
|
|
* |
92
|
|
|
* @param callable $callback |
93
|
|
|
* The callback when this operation is rolled back. |
94
|
|
|
* |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
1 |
|
public function onRemove(callable $callback) |
98
|
|
|
{ |
99
|
1 |
|
$this->remove[] = $callback; |
100
|
1 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Set buffer callback. |
105
|
|
|
* |
106
|
|
|
* @param callable $callback |
107
|
|
|
* The callback when this operation is buffered. |
108
|
|
|
* |
109
|
|
|
* @return $this |
110
|
|
|
*/ |
111
|
1 |
|
public function onBuffer(callable $callback) |
112
|
|
|
{ |
113
|
1 |
|
$this->buffer[] = $callback; |
114
|
1 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get commit callbacks. |
119
|
|
|
* |
120
|
|
|
* @return callable|null |
121
|
|
|
*/ |
122
|
1 |
|
public function getCommitCallbacks() |
123
|
|
|
{ |
124
|
1 |
|
return $this->commit; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get rollback callbacks. |
129
|
|
|
* |
130
|
|
|
* @return callable|null |
131
|
|
|
*/ |
132
|
1 |
|
public function getRollbackCallbacks() |
133
|
|
|
{ |
134
|
1 |
|
return $this->rollback; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get buffer callbacks. |
139
|
|
|
* |
140
|
|
|
* @return callable|null |
141
|
|
|
*/ |
142
|
1 |
|
public function getBufferCallbacks() |
143
|
|
|
{ |
144
|
1 |
|
return $this->buffer; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get remove callbacks. |
149
|
|
|
* |
150
|
|
|
* @return callable|null |
151
|
|
|
*/ |
152
|
1 |
|
public function getRemoveCallbacks() |
153
|
|
|
{ |
154
|
1 |
|
return $this->remove; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Set metadata. |
159
|
|
|
* |
160
|
|
|
* @param string $key |
161
|
|
|
* The key of the metadata. |
162
|
|
|
* @param mixed $value |
163
|
|
|
* The value to set. |
164
|
|
|
* |
165
|
|
|
* @return $this |
166
|
|
|
*/ |
167
|
1 |
|
public function setMetadata($key, $value) |
168
|
|
|
{ |
169
|
1 |
|
$this->metadata[$key] = $value; |
170
|
1 |
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Get metadata. |
175
|
|
|
* |
176
|
|
|
* @param $key |
177
|
|
|
* The key of the metadata to get. |
178
|
|
|
* |
179
|
|
|
* @return mixed|null |
180
|
|
|
*/ |
181
|
1 |
|
public function getMetadata($key) |
182
|
|
|
{ |
183
|
1 |
|
return isset($this->metadata[$key]) ? $this->metadata[$key] : null; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Get result from callback. |
188
|
|
|
* |
189
|
|
|
* @return mixed |
190
|
|
|
*/ |
191
|
4 |
|
public function getResult() |
192
|
|
|
{ |
193
|
4 |
|
return $this->result; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param Connection $connection |
198
|
|
|
* The connection to use for this id. |
199
|
|
|
* @param string $idx |
200
|
|
|
* The id. |
201
|
|
|
* |
202
|
|
|
* @return $this |
203
|
|
|
*/ |
204
|
1 |
|
public function setIdx(Connection $connection, $idx) |
205
|
|
|
{ |
206
|
1 |
|
$this->idx[$connection->connectionId()] = $idx; |
207
|
1 |
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Get id. |
212
|
|
|
* |
213
|
|
|
* @param Connection $connection |
214
|
|
|
* The connection to get id from. |
215
|
|
|
* |
216
|
|
|
* @return string|null |
217
|
|
|
*/ |
218
|
1 |
|
public function idx(Connection $connection) |
219
|
|
|
{ |
220
|
1 |
|
$connectionId = $connection->connectionId(); |
221
|
1 |
|
return isset($this->idx[$connectionId]) ? $this->idx[$connectionId] : null; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Execute commit operation. |
226
|
|
|
* |
227
|
|
|
* @param Connection $connection |
228
|
|
|
* The connection to run this operation on. |
229
|
|
|
* |
230
|
|
|
* @return mixed |
231
|
|
|
*/ |
232
|
1 |
|
public function commit($connection = null) |
233
|
|
|
{ |
234
|
1 |
|
foreach ($this->commit as $callback) { |
235
|
1 |
|
$this->result = call_user_func($callback, $this, $connection); |
236
|
1 |
|
} |
237
|
1 |
|
return $this->result; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Execute rollback operation. |
242
|
|
|
* |
243
|
|
|
* @param Connection $connection |
244
|
|
|
* The connection to run this operation on. |
245
|
|
|
* |
246
|
|
|
* @return mixed |
247
|
|
|
*/ |
248
|
1 |
|
public function rollback($connection = null) |
249
|
|
|
{ |
250
|
1 |
|
foreach ($this->rollback as $callback) { |
251
|
1 |
|
$this->result = call_user_func($callback, $this, $connection); |
252
|
1 |
|
} |
253
|
1 |
|
return $this->result; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Execute buffer operation. |
258
|
|
|
* |
259
|
|
|
* @param Connection $connection |
260
|
|
|
* The connection to run this operation on. |
261
|
|
|
* |
262
|
|
|
* @return mixed |
263
|
|
|
*/ |
264
|
1 |
|
public function buffer($connection = null) |
265
|
|
|
{ |
266
|
1 |
|
foreach ($this->buffer as $callback) { |
267
|
1 |
|
$this->result = call_user_func($callback, $this, $connection); |
268
|
1 |
|
} |
269
|
1 |
|
return $this->result; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Execute remove operation. |
274
|
|
|
* |
275
|
|
|
* @param Connection $connection |
276
|
|
|
* The connection to run this operation on. |
277
|
|
|
* |
278
|
|
|
* @return mixed |
279
|
|
|
*/ |
280
|
1 |
|
public function remove($connection = null) |
281
|
|
|
{ |
282
|
1 |
|
foreach ($this->remove as $callback) { |
283
|
1 |
|
$this->result = call_user_func($callback, $this, $connection); |
284
|
1 |
|
} |
285
|
1 |
|
return $this->result; |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|