1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Shippinno\DoctrineToolbelt; |
5
|
|
|
|
6
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
7
|
|
|
use Doctrine\DBAL\Connection; |
8
|
|
|
use Doctrine\DBAL\ConnectionException; |
9
|
|
|
use Doctrine\ORM\EntityManager; |
10
|
|
|
use Doctrine\ORM\ORMException; |
11
|
|
|
use Throwable; |
12
|
|
|
|
13
|
|
|
trait HandlesMultipleEntityManagers |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var ManagerRegistry |
17
|
|
|
*/ |
18
|
|
|
protected $managerRegistry; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param ManagerRegistry $managerRegistry |
22
|
|
|
*/ |
23
|
5 |
|
protected function setManagerRegistry(ManagerRegistry $managerRegistry): void |
24
|
|
|
{ |
25
|
5 |
|
$this->managerRegistry = $managerRegistry; |
26
|
5 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string[] $names |
30
|
|
|
* @throws RollbackException |
31
|
|
|
*/ |
32
|
4 |
|
public function flushManagersAtomically(array $names): void |
33
|
|
|
{ |
34
|
4 |
|
$this->beginTransactions($names); |
35
|
|
|
try { |
36
|
|
|
try { |
37
|
4 |
|
$this->flush($names); |
38
|
3 |
|
$this->commit($names); |
39
|
2 |
|
return; |
40
|
2 |
|
} catch (Throwable $e) { |
41
|
2 |
|
$this->rollback($names); |
42
|
1 |
|
throw new RollbackException($e); |
43
|
|
|
} |
44
|
2 |
|
} catch (ConnectionException $e) { |
45
|
1 |
|
throw new RollbackFailedException($e); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @throws RollbackException |
51
|
|
|
*/ |
52
|
4 |
|
public function flushAllManagersAtomically(): void |
53
|
|
|
{ |
54
|
4 |
|
$this->flushManagersAtomically($this->managerRegistry->getManagerNames()); |
55
|
2 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string[] $names |
59
|
|
|
*/ |
60
|
1 |
|
public function clearManagers(array $names): void |
61
|
|
|
{ |
62
|
1 |
|
foreach ($names as $name) { |
63
|
1 |
|
$this->getEntityManager($name)->clear(); |
64
|
|
|
} |
65
|
1 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
1 |
|
public function clearAllManagers(): void |
71
|
|
|
{ |
72
|
1 |
|
$this->clearManagers($this->managerRegistry->getManagerNames()); |
73
|
1 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string[] $names |
77
|
|
|
* @throws ORMException |
78
|
|
|
*/ |
79
|
4 |
|
protected function flush(array $names): void |
80
|
|
|
{ |
81
|
4 |
|
foreach ($names as $name) { |
82
|
4 |
|
$this->getEntityManager($name)->flush(); |
83
|
|
|
} |
84
|
3 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string[] $names |
88
|
|
|
*/ |
89
|
4 |
|
protected function beginTransactions(array $names): void |
90
|
|
|
{ |
91
|
4 |
|
foreach ($names as $name) { |
92
|
4 |
|
$this->getConnection($name)->beginTransaction(); |
93
|
|
|
} |
94
|
4 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string[] $names |
98
|
|
|
* @throws ConnectionException |
99
|
|
|
*/ |
100
|
3 |
|
protected function commit(array $names): void |
101
|
|
|
{ |
102
|
3 |
|
foreach ($names as $name) { |
103
|
3 |
|
$this->getConnection($name)->commit(); |
104
|
|
|
} |
105
|
2 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string[] $names |
109
|
|
|
* @throws ConnectionException |
110
|
|
|
*/ |
111
|
2 |
|
protected function rollback(array $names): void |
112
|
|
|
{ |
113
|
2 |
|
foreach ($names as $name) { |
114
|
2 |
|
$this->getConnection($name)->rollBack(); |
115
|
|
|
} |
116
|
1 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $name |
120
|
|
|
* @return EntityManager |
121
|
|
|
*/ |
122
|
5 |
|
protected function getEntityManager(string $name): EntityManager |
123
|
|
|
{ |
124
|
5 |
|
return $this->managerRegistry->getManager($name); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $name |
129
|
|
|
* @return Connection |
130
|
|
|
*/ |
131
|
4 |
|
protected function getConnection(string $name): Connection |
132
|
|
|
{ |
133
|
4 |
|
return $this->getEntityManager($name)->getConnection(); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|