|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Stu\Orm\Transaction; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
6
|
|
|
use Doctrine\Common\Collections\Collection; |
|
7
|
|
|
use Doctrine\DBAL\Connection; |
|
8
|
|
|
use Doctrine\ORM\Configuration; |
|
9
|
|
|
use Doctrine\ORM\Decorator\EntityManagerDecorator; |
|
10
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
11
|
|
|
use Doctrine\ORM\EntityRepository; |
|
12
|
|
|
use Override; |
|
|
|
|
|
|
13
|
|
|
use RuntimeException; |
|
14
|
|
|
use Stu\Module\Logging\LoggerTypeEnum; |
|
|
|
|
|
|
15
|
|
|
use Stu\Module\Logging\LoggerUtilFactoryInterface; |
|
16
|
|
|
use Stu\Module\Logging\RotatingLoggerInterface; |
|
|
|
|
|
|
17
|
|
|
use Stu\Orm\Transaction\EntityManagerFactoryInterface; |
|
18
|
|
|
|
|
19
|
|
|
class SwitchableEntityManager extends EntityManagerDecorator implements SwitchableEntityManagerInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var ArrayCollection<string, EntityManagerInterface> */ |
|
22
|
|
|
private Collection $entityManagers; |
|
23
|
|
|
|
|
24
|
|
|
private EntityManagerTypeEnum $currentType; |
|
|
|
|
|
|
25
|
|
|
private EntityManagerTypeEnum $lastType; |
|
26
|
|
|
private RotatingLoggerInterface $logger; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct( |
|
29
|
|
|
private EntityManagerFactoryInterface $entityManagerFactory, |
|
30
|
|
|
private Configuration $configuration, |
|
31
|
|
|
EntityManagerInterface $sessionEntityManager, |
|
32
|
|
|
LoggerUtilFactoryInterface $loggerUtilFactory |
|
33
|
|
|
) { |
|
34
|
|
|
parent::__construct($sessionEntityManager); |
|
35
|
|
|
|
|
36
|
|
|
$this->entityManagers = new ArrayCollection([EntityManagerTypeEnum::SESSION->value => $sessionEntityManager]); |
|
37
|
|
|
$this->currentType = EntityManagerTypeEnum::SESSION; |
|
38
|
|
|
$this->logger = $loggerUtilFactory->getRotatingLogger(LoggerTypeEnum::EVENT_AND_ENTITY_LOCK); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function switchType(EntityManagerTypeEnum $type): SwitchableEntityManager |
|
42
|
|
|
{ |
|
43
|
|
|
if ($this->currentType === $type) { |
|
44
|
|
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$this->lastType = $this->currentType; |
|
48
|
|
|
$this->currentType = $type; |
|
49
|
|
|
|
|
50
|
|
|
$key = $type->value; |
|
51
|
|
|
$entityManager = $this->entityManagers->get($key); |
|
52
|
|
|
if ($entityManager === null) { |
|
53
|
|
|
$entityManager = $this->setNewEntityManger($key); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$this->logger->log(sprintf( |
|
57
|
|
|
'SWITCH_TYPE: %s(%d)->%s(%d)', |
|
58
|
|
|
$this->lastType->value, |
|
59
|
|
|
$this->wrapped->getConnection()->getTransactionNestingLevel(), |
|
60
|
|
|
$key, |
|
61
|
|
|
$entityManager->getConnection()->getTransactionNestingLevel() |
|
62
|
|
|
)); |
|
63
|
|
|
|
|
64
|
|
|
$this->wrapped = $entityManager; |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function resetLastType(): SwitchableEntityManager |
|
70
|
|
|
{ |
|
71
|
|
|
$this->switchType($this->lastType); |
|
72
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
private function setNewEntityManger(string $key, ?Connection $connection = null): EntityManagerInterface |
|
77
|
|
|
{ |
|
78
|
|
|
$newManager = $this->entityManagerFactory->createEntityManager($connection); |
|
79
|
|
|
$this->entityManagers->set($key, $newManager); |
|
80
|
|
|
|
|
81
|
|
|
return $newManager; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Executes a function in a transaction. |
|
86
|
|
|
* |
|
87
|
|
|
* The function gets passed this EntityManager instance as an (optional) parameter. |
|
88
|
|
|
* |
|
89
|
|
|
* {@link flush} is invoked prior to transaction commit. |
|
90
|
|
|
* |
|
91
|
|
|
* If an exception occurs during execution of the function or flushing or transaction commit, |
|
92
|
|
|
* the transaction is rolled back, the EntityManager closed and the exception re-thrown. |
|
93
|
|
|
* |
|
94
|
|
|
* @psalm-param callable(self): T $func The function to execute transactionally. |
|
95
|
|
|
* |
|
96
|
|
|
* @return mixed The value returned from the closure. |
|
97
|
|
|
* @psalm-return T |
|
98
|
|
|
* |
|
99
|
|
|
* @template T |
|
100
|
|
|
*/ |
|
101
|
|
|
#[Override] |
|
102
|
|
|
public function wrapInTransaction(callable $func): mixed |
|
103
|
|
|
{ |
|
104
|
|
|
if (!$this->wrapped->isOpen()) { |
|
105
|
|
|
$this->renewManager(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $this->wrapped->wrapInTransaction(function (EntityManagerInterface $entityManager) use ($func) { |
|
109
|
|
|
|
|
110
|
|
|
return $func($entityManager); |
|
111
|
|
|
}); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
private function renewManager(EntityManagerInterface $entityManager = null): SwitchableEntityManager |
|
115
|
|
|
{ |
|
116
|
|
|
// remove old manager |
|
117
|
|
|
$oldManager = $entityManager ?? $this->wrapped; |
|
118
|
|
|
/** @var string|false */ |
|
119
|
|
|
$key = $this->entityManagers->indexOf($oldManager); |
|
120
|
|
|
if (!$key) { |
|
121
|
|
|
throw new RuntimeException(sprintf('no entity manager of type "%s" registered', $key)); |
|
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
$this->entityManagers->remove($key); |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
// create and register new manager |
|
126
|
|
|
$this->wrapped = $this->setNewEntityManger($key, $oldManager->getConnection()); |
|
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
return $this; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function isUninitializedObject(mixed $obj): bool |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->wrapped->isUninitializedObject($obj); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
#[Override] |
|
137
|
|
|
public function getRepository(string $className): EntityRepository |
|
138
|
|
|
{ |
|
139
|
|
|
return $this |
|
140
|
|
|
->configuration |
|
141
|
|
|
->getRepositoryFactory() |
|
142
|
|
|
->getRepository($this, $className); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function beginTransaction(): void |
|
146
|
|
|
{ |
|
147
|
|
|
$this->logger->logf('%s::BEGIN_TRANSACTION', $this->currentType->value); |
|
148
|
|
|
$this->wrapped->beginTransaction(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function flush(): void |
|
152
|
|
|
{ |
|
153
|
|
|
if ($this->wrapped->isOpen()) { |
|
154
|
|
|
$this->wrapped->flush(); |
|
155
|
|
|
} else { |
|
156
|
|
|
throw new TransactionException(sprintf('entity manager is closed. wrong type? (%s)', $this->currentType->value)); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
#[Override] |
|
161
|
|
|
public function commit(): void |
|
162
|
|
|
{ |
|
163
|
|
|
$this->logger->logf('%s::COMMIT_TRANSACTION', $this->currentType->value); |
|
164
|
|
|
|
|
165
|
|
|
if ($this->wrapped->getConnection()->isTransactionActive()) { |
|
166
|
|
|
$this->wrapped->commit(); |
|
167
|
|
|
} else { |
|
168
|
|
|
throw new TransactionException(sprintf('no active transaction. wrong type? (%s)', $this->currentType->value)); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths