1 | <?php |
||
10 | class Connection |
||
11 | { |
||
12 | /** |
||
13 | * @var Operation[] |
||
14 | */ |
||
15 | protected $operations = []; |
||
16 | |||
17 | /** |
||
18 | * @var int |
||
19 | */ |
||
20 | protected $idx = 0; |
||
21 | |||
22 | /** |
||
23 | * @var int[] |
||
24 | */ |
||
25 | protected $savePoints = []; |
||
26 | |||
27 | /** |
||
28 | * @var int |
||
29 | */ |
||
30 | protected $depth = 0; |
||
31 | |||
32 | /** |
||
33 | * @var null|string |
||
34 | */ |
||
35 | protected $connectionId; |
||
36 | |||
37 | /** |
||
38 | * Connection constructor. |
||
39 | * |
||
40 | * @param null|string $connectionId |
||
41 | * (optional) The id of the connection. |
||
42 | */ |
||
43 | 1 | public function __construct($connectionId = null) |
|
47 | |||
48 | /** |
||
49 | * Get connection id. |
||
50 | * |
||
51 | * @return null|string |
||
52 | */ |
||
53 | 1 | public function connectionId() |
|
57 | |||
58 | /** |
||
59 | * Remove savepoints to and acquire index of latest active savepoint. |
||
60 | * |
||
61 | * @param int $oldDepth |
||
62 | * The old depth. |
||
63 | * @param $newDepth |
||
64 | * The new depth. |
||
65 | * |
||
66 | * @return int|null |
||
67 | * The last index, if found. |
||
68 | */ |
||
69 | 2 | public function closeSavepoints($oldDepth, $newDepth) |
|
70 | { |
||
71 | 2 | $idx = null; |
|
72 | 2 | for ($depth = $newDepth + 1; $depth <= $oldDepth; $depth++) { |
|
73 | 2 | if (isset($this->savePoints[$depth])) { |
|
74 | 2 | $idx = isset($idx) ? $idx : $this->savePoints[$depth]; |
|
75 | 2 | unset($this->savePoints[$depth]); |
|
76 | 2 | } |
|
77 | 2 | } |
|
78 | 2 | return $idx; |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | 1 | public function startTransaction($newDepth = null) |
|
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | 2 | public function commitTransaction($newDepth = null) |
|
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | 2 | public function rollbackTransaction($newDepth = null) |
|
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | 1 | public function addOperation(Operation $operation) |
|
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | 2 | public function hasOperation(Operation $operation) |
|
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | */ |
||
174 | 1 | public function removeOperation(Operation $operation) |
|
178 | |||
179 | /** |
||
180 | * Short-hand notation for adding code to be run on commit. |
||
181 | * |
||
182 | * @param callable $callback |
||
183 | * The code to run. |
||
184 | * |
||
185 | * @return Operation |
||
186 | */ |
||
187 | 1 | public function onCommit(callable $callback) |
|
192 | |||
193 | /** |
||
194 | * Short-hand notation for adding code to be run on rollback. |
||
195 | * |
||
196 | * @param callable $callback |
||
197 | * The code to run. |
||
198 | * |
||
199 | * @return Operation |
||
200 | */ |
||
201 | 1 | public function onRollback(callable $callback) |
|
206 | } |
||
207 |