-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPersistentTranslatable.php
More file actions
301 lines (256 loc) · 11.3 KB
/
PersistentTranslatable.php
File metadata and controls
301 lines (256 loc) · 11.3 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
/*
* (c) webfactory GmbH <info@webfactory.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Webfactory\Bundle\PolyglotBundle\Doctrine;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Selectable;
use Doctrine\ORM\UnitOfWork;
use Exception;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use ReflectionClass;
use ReflectionNamedType;
use ReflectionProperty;
use Throwable;
use Webfactory\Bundle\PolyglotBundle\Exception\TranslationException;
use Webfactory\Bundle\PolyglotBundle\Locale\DefaultLocaleProvider;
use Webfactory\Bundle\PolyglotBundle\Translatable;
use Webfactory\Bundle\PolyglotBundle\TranslatableInterface;
/**
* @template T
* @implements TranslatableInterface<T>
*
* This class implements `TranslatableInterface` for entities that are managed by
* the entity manager. PolyglotListener will replace `Translatable` instances with
* instances of this class as soon as a new entity is passed to EntityManager::persist().
*/
final class PersistentTranslatable implements TranslatableInterface
{
/**
* Cache to speed up accessing translated values, indexed by entity class, entity OID and locale.
* This is static so that it can be shared by multiple PersistentTranslatable instances that
* operate for the same entity instance, but different fields.
*
* @var array<class-string, array<int, array<string, object|null>>>
*/
private static array $_translations = [];
/**
* Object id for $this->entity.
*/
private int $oid;
/**
* The "primary" (untranslated) value for the property covered by this Translatable.
*/
private mixed $primaryValue;
/**
* The UninitializedPersistentTranslatable object instance used when this PersistentTranslatable instance is removed (ejected) from
* an entity with managed translations. Needs to be kept as an object reference, so that multiple injection/ejection
* cycles will use the same object instance. This is necessary to prevent Doctrine ORM change detection from treating
* the value as changed every time.
*/
private ?UninitializedPersistentTranslatable $valueForEjection = null;
private LoggerInterface $logger;
/**
* @param UnitOfWork $unitOfWork The UoW managing the entity that contains this PersistentTranslatable
* @param class-string $class The class of the entity containing this PersistentTranslatable instance
* @param object $entity The entity containing this PersistentTranslatable instance
* @param string $primaryLocale The locale for which the translated value will be persisted in the "main" entity
* @param DefaultLocaleProvider $defaultLocaleProvider DefaultLocaleProvider that provides the locale to use when no explicit locale is passed to e. g. translate()
* @param ReflectionProperty $translationProperty ReflectionProperty pointing to the field in the translations class that holds the translated value to use
* @param ReflectionProperty $translationCollection ReflectionProperty pointing to the collection in the main class that holds translation instances
* @param ReflectionClass<object> $translationClass ReflectionClass for the class holding translated values
* @param ReflectionProperty $localeField ReflectionProperty pointing to the field in the translations class that holds a translation's locale
* @param ReflectionProperty $translationMapping ReflectionProperty pointing to the field in the translations class that refers back to the main entity (the owning side of the one-to-many translations collection).
* @param ReflectionProperty $translatedProperty ReflectionProperty pointing to the field in the main entity where this PersistentTranslatable instance will be used
*/
public function __construct(
private readonly UnitOfWork $unitOfWork,
private readonly string $class,
private readonly object $entity,
private readonly string $primaryLocale,
private readonly DefaultLocaleProvider $defaultLocaleProvider,
private readonly ReflectionProperty $translationProperty,
private readonly ReflectionProperty $translationCollection,
private readonly ReflectionClass $translationClass,
private readonly ReflectionProperty $localeField,
private readonly ReflectionProperty $translationMapping,
private readonly ReflectionProperty $translatedProperty,
?LoggerInterface $logger = null,
) {
$this->oid = spl_object_id($entity);
$this->logger = $logger ?? new NullLogger();
$currentValue = $this->translatedProperty->getValue($this->entity);
if ($currentValue instanceof UninitializedPersistentTranslatable) {
$this->primaryValue = $currentValue->getPrimaryValue();
$this->valueForEjection = $currentValue;
} elseif ($currentValue instanceof Translatable) {
$currentValue->copy($this);
} else {
$this->primaryValue = $currentValue;
}
}
public function setPrimaryValue(mixed $value): void
{
$this->primaryValue = $value;
}
/**
* @psalm-internal Webfactory\Bundle\PolyglotBundle
*/
public function eject(): void
{
$value = $this->primaryValue;
$type = $this->translatedProperty->getType();
if ($type instanceof ReflectionNamedType && TranslatableInterface::class === $type->getName() && \is_string($value)) {
if (null === $this->valueForEjection || $this->valueForEjection->getPrimaryValue() !== $value) {
$this->valueForEjection = new UninitializedPersistentTranslatable($value);
}
$value = $this->valueForEjection;
}
$this->translatedProperty->setValue($this->entity, $value);
}
/**
* @psalm-internal Webfactory\Bundle\PolyglotBundle
*/
public function inject(): void
{
$this->translatedProperty->setValue($this->entity, $this);
}
private function getTranslationEntity(string $locale): ?object
{
if (!$this->isTranslationCached($locale)) {
$this->cacheTranslation($locale);
}
return $this->getCachedTranslation($locale);
}
private function createTranslationEntity(string $locale): object
{
$className = $this->translationClass->name;
$entity = new $className();
$this->localeField->setValue($entity, $locale);
$this->translationMapping->setValue($entity, $this->entity);
/** @var Collection<array-key, object> $collection */
$collection = $this->translationCollection->getValue($this->entity);
$collection->add($entity);
self::$_translations[$this->class][$this->oid][$locale] = $entity;
$this->unitOfWork->persist($entity);
return $entity;
}
public function setTranslation(mixed $value, ?string $locale = null): void
{
$locale ??= $this->getDefaultLocale();
if ($locale === $this->primaryLocale) {
$this->setPrimaryValue($value);
} else {
$entity = $this->getTranslationEntity($locale);
if (null === $entity) {
$entity = $this->createTranslationEntity($locale);
}
$this->translationProperty->setValue($entity, $value);
}
}
/**
* @throws TranslationException
*/
public function translate(?string $locale = null): mixed
{
$locale ??= $this->getDefaultLocale();
try {
if ($locale === $this->primaryLocale) {
return $this->primaryValue;
}
$entity = $this->getTranslationEntity($locale);
if (null !== $entity) {
$translated = $this->translationProperty->getValue($entity);
if (null !== $translated) {
return $translated;
}
}
return $this->primaryValue;
} catch (Exception $e) {
$message = \sprintf(
'Cannot translate property %s::%s into locale %s',
\get_class($this->entity),
$this->translationProperty->getName(),
$locale
);
throw new TranslationException($message, $e);
}
}
public function isTranslatedInto(string $locale): bool
{
if ($locale === $this->primaryLocale) {
return '' !== $this->primaryValue && null !== $this->primaryValue;
}
$entity = $this->getTranslationEntity($locale);
return null !== $entity && null !== $this->translationProperty->getValue($entity);
}
public function __toString(): string
{
try {
return (string) $this->translate();
} catch (Throwable $e) {
$this->logger->error($this->stringifyException($e));
return '';
}
}
private function getDefaultLocale(): string
{
return $this->defaultLocaleProvider->getDefaultLocale();
}
private function isTranslationCached(string $locale): bool
{
return isset(self::$_translations[$this->class][$this->oid][$locale]);
}
/**
* The collection filtering API will issue a SQL query every time if the collection is not in memory; that is, it
* does not manage "partially initialized" collections. For this reason, we cache the lookup results on our own
* (in-memory per-request) in a static member variable, so they can be shared among all TranslationProxies.
*/
private function cacheTranslation(string $locale): void
{
/** @var Selectable<array-key, object> $translationsInAllLanguages */
$translationsInAllLanguages = $this->translationCollection->getValue($this->entity);
$criteria = $this->createLocaleCriteria($locale);
$translationsFilteredByLocale = $translationsInAllLanguages->matching($criteria);
$translationInLocale = ($translationsFilteredByLocale->count() > 0) ? $translationsFilteredByLocale->first() : null;
if (\is_bool($translationInLocale)) {
return;
}
self::$_translations[$this->class][$this->oid][$locale] = $translationInLocale;
}
private function createLocaleCriteria(string $locale): Criteria
{
return Criteria::create()
->where(
Criteria::expr()->eq($this->localeField->getName(), $locale)
);
}
private function getCachedTranslation(string $locale): ?object
{
return self::$_translations[$this->class][$this->oid][$locale];
}
private function stringifyException(Throwable $e): string
{
$exceptionAsString = '';
while (null !== $e) {
if ('' !== $exceptionAsString) {
$exceptionAsString .= \PHP_EOL.'Previous exception: '.\PHP_EOL;
}
$exceptionAsString .= \sprintf(
"Exception '%s' with message '%s' in %s:%d\n%s",
$e::class,
$e->getMessage(),
$e->getFile(),
$e->getLine(),
$e->getTraceAsString()
);
$e = $e->getPrevious();
}
return $exceptionAsString;
}
}