|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ArangoClient\Transactions; |
|
6
|
|
|
|
|
7
|
|
|
use ArangoClient\ArangoClient; |
|
8
|
|
|
use ArangoClient\Exceptions\ArangoException; |
|
9
|
|
|
use ArangoClient\Manager; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class TransactionManager |
|
13
|
|
|
* Begins transactions and manages those transactions. |
|
14
|
|
|
* |
|
15
|
|
|
* @see https://www.arangodb.com/docs/stable/http/transaction-stream-transaction.html |
|
16
|
|
|
*/ |
|
17
|
|
|
class TransactionManager extends Manager |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var array<string> |
|
21
|
|
|
*/ |
|
22
|
|
|
protected array $transactions = []; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(protected ArangoClient $arangoClient) |
|
25
|
|
|
{ |
|
26
|
|
|
register_shutdown_function([$this, 'abortRunningTransactions']); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @return string[] |
|
31
|
|
|
*/ |
|
32
|
|
|
public function getTransactions(): array |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->transactions; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @throws ArangoException |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getTransaction(?string $id = null): string |
|
41
|
|
|
{ |
|
42
|
|
|
$this->validateId($id); |
|
43
|
|
|
|
|
44
|
|
|
if ($id === null) { |
|
45
|
|
|
return (string) end($this->transactions); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $this->transactions[$id]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Begin a transactions and return its id. |
|
53
|
|
|
* |
|
54
|
|
|
* @param array<string, array<string>> $collections |
|
55
|
|
|
* @param array<mixed> $options |
|
56
|
|
|
* |
|
57
|
|
|
* @throws ArangoException |
|
58
|
|
|
*/ |
|
59
|
|
|
public function begin(array $collections = [], array $options = []): string |
|
60
|
|
|
{ |
|
61
|
|
|
$options['collections'] = $this->prepareCollections($collections); |
|
62
|
|
|
|
|
63
|
|
|
$config = ['body' => $options]; |
|
64
|
|
|
$result = (object) $this->arangoClient->request('post', '/_api/transaction/begin', $config)->result; |
|
65
|
|
|
|
|
66
|
|
|
$id = (string) $result->id; |
|
67
|
|
|
$this->transactions[$id] = $id; |
|
68
|
|
|
|
|
69
|
|
|
return $id; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @throws ArangoException |
|
74
|
|
|
*/ |
|
75
|
|
|
public function commit(?string $id = null): bool |
|
76
|
|
|
{ |
|
77
|
|
|
$id = $this->getTransaction($id); |
|
78
|
|
|
|
|
79
|
|
|
$uri = '/_api/transaction/' . $id; |
|
80
|
|
|
|
|
81
|
|
|
$this->arangoClient->request('put', $uri); |
|
82
|
|
|
unset($this->transactions[$id]); |
|
83
|
|
|
|
|
84
|
|
|
return true; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @throws ArangoException |
|
89
|
|
|
*/ |
|
90
|
|
|
public function abort(?string $id = null): bool |
|
91
|
|
|
{ |
|
92
|
|
|
$id = $this->getTransaction($id); |
|
93
|
|
|
|
|
94
|
|
|
$uri = '/_api/transaction/' . $id; |
|
95
|
|
|
|
|
96
|
|
|
$this->arangoClient->request('delete', $uri); |
|
97
|
|
|
unset($this->transactions[$id]); |
|
98
|
|
|
|
|
99
|
|
|
return true; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @throws ArangoException |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function validateId(?string $id = null): bool |
|
106
|
|
|
{ |
|
107
|
|
|
if ( |
|
108
|
|
|
empty($this->transactions) |
|
109
|
|
|
|| ( |
|
110
|
|
|
$id !== null |
|
111
|
|
|
&& !isset($this->transactions[$id]) |
|
112
|
|
|
) |
|
113
|
|
|
) { |
|
114
|
|
|
throw new ArangoException('Transaction not found.', 404); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return true; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param array<string, array<string>> $collections |
|
122
|
|
|
* @return array<string, array<string>> |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function prepareCollections(array $collections): array |
|
125
|
|
|
{ |
|
126
|
|
|
$collectionTemplate = [ |
|
127
|
|
|
'read' => [], |
|
128
|
|
|
'write' => [], |
|
129
|
|
|
'exclusive' => [], |
|
130
|
|
|
]; |
|
131
|
|
|
|
|
132
|
|
|
return array_intersect_key(array_merge($collectionTemplate, $collections), $collectionTemplate); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Cleanup any open transactions on shutdown as registered in the constructor. |
|
137
|
|
|
* |
|
138
|
|
|
* @see TransactionManager::__construct |
|
139
|
|
|
* |
|
140
|
|
|
* @throws ArangoException |
|
141
|
|
|
*/ |
|
142
|
|
|
public function abortRunningTransactions(): void |
|
143
|
|
|
{ |
|
144
|
|
|
foreach ($this->transactions as $id) { |
|
145
|
|
|
try { |
|
146
|
|
|
$this->abort($id); |
|
147
|
|
|
} finally { |
|
148
|
|
|
// Ignore any errors, this is just for clean up. |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|