/
DoctrineHelper.php
237 lines (202 loc) 路 6.49 KB
/
DoctrineHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
declare(strict_types=1);
namespace Baraja\Doctrine;
use Doctrine\Persistence\Mapping\MappingException;
use Tracy\Debugger;
use Tracy\ILogger;
class DoctrineHelper
{
public function __construct(
private EntityManager $entityManager,
) {
}
/**
* Return list of class names which is variant of given entity.
* By $exclude you can define list of entities which will be skipped.
*
* @param class-string $entity
* @param array<int, class-string>|null $exclude
* @return array<class-string, string> (type => name)
*/
public function getEntityVariants(string $entity, ?array $exclude = null): array
{
$meta = $this->entityManager->getClassMetadata($entity);
$return = [];
foreach ($meta->discriminatorMap as $variant) {
if (class_exists($variant) === false) {
throw new \LogicException(sprintf('Entity class "%s" does not exist.', $variant));
}
try {
$return[$variant] = (string) Utils::reflectionClassDocComment($variant, 'name');
} catch (\ReflectionException) {
$return[$variant] = (string) preg_replace_callback(
'/([a-z0-9])([A-Z])/',
static fn(array $match) => $match[1] . ' ' . strtolower($match[2]),
(string) preg_replace('/^.*?\\\\([^\\\\]+)$/', '$1', $variant),
);
}
}
foreach ($exclude ?? [] as $excludeItem) {
unset($return[$excludeItem]);
}
return $return;
}
/**
* Return most embedded entity.
* In case of `CustomProduct` extends `Product` extends `BaseProduct`, return `CustomProduct`.
*
* @param class-string $entityClassName
* @return class-string
*/
public function getBestOfType(string $entityClassName): string
{
$variants = $this->getEntityVariants($entityClassName);
if (\in_array(\count($variants), [0, 1], true) === true) {
return $entityClassName;
}
$topLength = 0;
$topType = $entityClassName;
foreach (array_keys($variants) as $variant) {
try {
$length = $this->getParentClassLength(Utils::getReflectionClass($variant));
if ($length > $topLength) {
$topLength = $length;
$topType = $variant;
}
} catch (\ReflectionException) {
// Silence is golden.
}
}
return $topType;
}
/**
* Return real table name by entity Class name.
*
* @param class-string $entity
*/
public function getTableNameByEntity(string $entity): string
{
return $this->entityManager->getClassMetadata($entity)->table['name'];
}
/**
* If extends lot's of entities, return root entity class name.
*
* @param class-string $entity
*/
public function getRootEntityName(string $entity): string
{
return $this->entityManager->getClassMetadata($entity)->rootEntityName;
}
/**
* In case of chain inheritance Doctrine store lot's of entities in one table
* and distinguishes itself by `discriminator` column.
* This method return discriminator column name by given entity Class name.
*
* @param class-string $entity
*/
public function getDiscriminatorByEntity(string $entity): int|string
{
foreach ($this->entityManager->getClassMetadata($entity)->discriminatorMap as $discriminator => $variant) {
if ($variant === $entity) {
return $discriminator;
}
}
$entity = $this->getRootEntityName($entity);
foreach ($this->entityManager->getClassMetadata($entity)->discriminatorMap as $discriminator => $variant) {
if ($variant === $entity) {
return $discriminator;
}
}
return '';
}
/**
* Elevate the type of entity to the best possible and return as new type.
* This method can fail if you are tried save missing required columns.
* For more information please follow exception messages.
*
* @throws DatabaseException
*/
public function remapEntityToBestType(object $from): ?object
{
$fromType = $from::class;
$bestType = $this->getBestOfType($fromType);
if ($fromType === $bestType) {
return $from;
}
return $this->remapEntity($from, $bestType);
}
/**
* Remap one type of entity to new type in database.
* This feature is not supported in Doctrine therefore, the change may not be stable.
* When you remap entity type, please select all data again, because Doctrine internal memory can be damaged.
* Best practice is refresh page or break CLI process after this change.
*
* @param object $from instance of specific entity
* @param object|class-string $to instance of specific entity or class-name
* @throws DatabaseException
*/
public function remapEntity(object $from, object|string $to): ?object
{
$toType = is_object($to) ? $to::class : $to;
$toDiscriminator = $this->getDiscriminatorByEntity($toType);
if ($this->getDiscriminatorByEntity($from::class) === $toDiscriminator) {
return $from;
}
$fromTable = ($fromMetaData = $this->entityManager->getClassMetadata($from::class))->getTableName();
$toTable = $this->entityManager->getClassMetadata($toType)->getTableName();
$discriminatorColumn = $fromMetaData->discriminatorColumn['fieldName'] ?? '?';
if ($fromTable !== $toTable) {
throw new DatabaseException(sprintf('Entities for remap must be same table type, "%s" and "%s" given.',
$fromTable,
$toType,
));
}
try {
$this->entityManager->clear($this->getRootEntityName($from::class));
} catch (MappingException $e) {
Debugger::log($e, ILogger::CRITICAL);
throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
}
if (method_exists($from, 'getId')) {
$id = $from->getId();
} else {
throw new \InvalidArgumentException('Entity "' . $from::class . '" do not contain required method getId().');
}
try {
$this->entityManager->getConnection()->executeStatement(
str_replace(
['{table}', '{discriminatorColumn}', '{discriminator}', '{id}'],
[$fromTable, $discriminatorColumn, $toDiscriminator, $id],
'UPDATE `{table}` '
. 'SET `{discriminatorColumn}` = \'{discriminator}\' '
. 'WHERE `id` = \'{id}\'',
),
);
} catch (\Throwable $e) {
Debugger::log($e, ILogger::CRITICAL);
trigger_error($e->getMessage());
}
return $this->entityManager->getRepository($toType)->find($id);
}
/**
* Return relative direction between given entity and root entity by exploring parents.
*
* In case of `CustomProduct` extends `Product` extends `BaseProduct`, return:
* for CustomProduct -> 3
* for Product -> 2
* for BaseProduct -> 1
*/
private function getParentClassLength(\ReflectionClass $reflection, int $bind = 1): int
{
$length = 0;
$parent = $reflection;
do {
if ($parent === false) {
break;
}
$parent = $parent->getParentClass();
$length++;
} while (true);
return $length + max($bind, 0);
}
}