1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ArangoClient\Transactions; |
6
|
|
|
|
7
|
|
|
use ArangoClient\Exceptions\ArangoException; |
8
|
|
|
use ArangoClient\Http\HttpRequestOptions; |
9
|
|
|
use stdClass; |
10
|
|
|
|
11
|
|
|
trait SupportsTransactions |
12
|
|
|
{ |
13
|
|
|
protected ?TransactionManager $transactionManager = null; |
14
|
|
|
|
15
|
17 |
|
public function transactions(): TransactionManager |
16
|
|
|
{ |
17
|
17 |
|
if (!(property_exists($this, 'transactionManager') && $this->transactionManager !== null)) { |
18
|
17 |
|
$this->transactionManager = new TransactionManager($this); |
19
|
|
|
} |
20
|
|
|
|
21
|
17 |
|
return $this->transactionManager; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Shortcut to begin() on the transactionManager |
26
|
|
|
* |
27
|
|
|
* @param array<string, array<string>> $collections |
28
|
|
|
* @param array<mixed> $options |
29
|
|
|
* |
30
|
|
|
* @throws ArangoException |
31
|
|
|
*/ |
32
|
1 |
|
public function begin(array $collections = [], array $options = []): string |
33
|
|
|
{ |
34
|
1 |
|
return $this->transactions()->begin($collections, $options); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Shortcut to begin() on the transactionManager |
39
|
|
|
* |
40
|
|
|
* @param array<string, array<string>> $collections |
41
|
|
|
* @param array<mixed> $options |
42
|
|
|
* |
43
|
|
|
* @throws ArangoException |
44
|
|
|
*/ |
45
|
4 |
|
public function beginTransaction(array $collections = [], array $options = []): string |
46
|
|
|
{ |
47
|
4 |
|
return $this->transactions()->begin($collections, $options); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Shortcut to abort() on the transactionManager |
52
|
|
|
* |
53
|
|
|
* |
54
|
|
|
* @throws ArangoException |
55
|
|
|
*/ |
56
|
3 |
|
public function abort(?string $id = null): bool |
57
|
|
|
{ |
58
|
3 |
|
return $this->transactions()->abort($id); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Shortcut to abort() on the transactionManager |
63
|
|
|
* |
64
|
|
|
* |
65
|
|
|
* @throws ArangoException |
66
|
|
|
*/ |
67
|
1 |
|
public function rollBack(?string $id = null): bool |
68
|
|
|
{ |
69
|
1 |
|
return $this->transactions()->abort($id); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Shortcut to commit() on the transactionManager |
74
|
|
|
* |
75
|
|
|
* |
76
|
|
|
* @throws ArangoException |
77
|
|
|
*/ |
78
|
1 |
|
public function commit(?string $id = null): bool |
79
|
|
|
{ |
80
|
1 |
|
return $this->transactions()->commit($id); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param array<mixed>|HttpRequestOptions $options |
85
|
|
|
* |
86
|
|
|
* @throws ArangoException |
87
|
|
|
*/ |
88
|
12 |
|
public function transactionAwareRequest( |
89
|
|
|
string $method, |
90
|
|
|
string $uri, |
91
|
|
|
array|HttpRequestOptions $options = [], |
92
|
|
|
?string $database = null, |
93
|
|
|
?int $transactionId = null, |
94
|
|
|
): stdClass { |
95
|
12 |
|
if (is_array($options)) { |
|
|
|
|
96
|
12 |
|
$options = $this->prepareRequestOptions($options); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
try { |
99
|
12 |
|
if (!isset($transactionId)) { |
100
|
12 |
|
$transactionId = $this->transactions()->getTransaction(); |
101
|
|
|
} |
102
|
1 |
|
$options->addHeader('x-arango-trx-id', $transactionId); |
103
|
|
|
} finally { |
104
|
12 |
|
return $this->request($method, $uri, $options, $database); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
public function setTransactionManager(TransactionManager $transactionManager): void |
109
|
|
|
{ |
110
|
1 |
|
$this->transactionManager = $transactionManager; |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
public function getTransactionManager(): ?TransactionManager |
114
|
|
|
{ |
115
|
1 |
|
return $this->transactionManager; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|