Skip to content

Commit bcf63b9

Browse files
committed
Use the RuntimeReflectionService to obtain the same ReflectionProperty subclasses as used by ORM internals
1 parent af2c29f commit bcf63b9

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

src/Doctrine/PolyglotListener.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Doctrine\ORM\Event\PostFlushEventArgs;
1515
use Doctrine\ORM\Event\PreFlushEventArgs;
1616
use Doctrine\Persistence\Event\LifecycleEventArgs;
17+
use Doctrine\Persistence\Mapping\RuntimeReflectionService;
1718
use Psr\Log\LoggerInterface;
1819
use Psr\Log\NullLogger;
1920
use WeakReference;
@@ -51,6 +52,7 @@ final class PolyglotListener implements EventSubscriber
5152
public function __construct(
5253
private readonly DefaultLocaleProvider $defaultLocaleProvider,
5354
private readonly LoggerInterface $logger = null ?? new NullLogger(),
55+
private readonly RuntimeReflectionService $reflectionService = new RuntimeReflectionService(),
5456
) {
5557
}
5658

@@ -154,7 +156,7 @@ private function loadTranslationMetadataForClass($className, EntityManager $em):
154156

155157
return null;
156158
} else {
157-
$wakeup = TranslatableClassMetadata::wakeup($data);
159+
$wakeup = TranslatableClassMetadata::wakeup($data, $this->reflectionService);
158160
$wakeup->setLogger($this->logger);
159161
$this->translatedClasses[$className] = $wakeup;
160162

src/Doctrine/TranslatableClassMetadata.php

+9-8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Doctrine\ORM\EntityManager;
1313
use Doctrine\ORM\Mapping\ClassMetadataFactory;
1414
use Doctrine\ORM\Mapping\ClassMetadataInfo;
15+
use Doctrine\Persistence\Mapping\RuntimeReflectionService;
1516
use Psr\Log\LoggerInterface;
1617
use ReflectionClass;
1718
use ReflectionProperty;
@@ -126,23 +127,23 @@ public function sleep(): SerializedTranslatableClassMetadata
126127
return $sleep;
127128
}
128129

129-
public static function wakeup(SerializedTranslatableClassMetadata $data): self
130+
public static function wakeup(SerializedTranslatableClassMetadata $data, RuntimeReflectionService $reflectionService): self
130131
{
131132
$self = new self($data->class);
132133
$self->primaryLocale = $data->primaryLocale;
133-
$self->translationClass = new ReflectionClass($data->translationClass);
134+
$self->translationClass = $reflectionService->getClass($data->translationClass);
134135

135136
foreach ($data->translationFieldMapping as $fieldname => $property) {
136-
$self->translationFieldMapping[$fieldname] = new ReflectionProperty(...$property);
137+
$self->translationFieldMapping[$fieldname] = $reflectionService->getAccessibleProperty(...$property);
137138
}
138139

139140
foreach ($data->translatedProperties as $fieldname => $property) {
140-
$self->translatedProperties[$fieldname] = new ReflectionProperty(...$property);
141+
$self->translatedProperties[$fieldname] = $reflectionService->getAccessibleProperty(...$property);
141142
}
142143

143-
$self->translationsCollectionProperty = new ReflectionProperty(...$data->translationsCollectionProperty);
144-
$self->translationMappingProperty = new ReflectionProperty(...$data->translationMappingProperty);
145-
$self->translationLocaleProperty = new ReflectionProperty(...$data->translationLocaleProperty);
144+
$self->translationsCollectionProperty = $reflectionService->getAccessibleProperty(...$data->translationsCollectionProperty);
145+
$self->translationMappingProperty = $reflectionService->getAccessibleProperty(...$data->translationMappingProperty);
146+
$self->translationLocaleProperty = $reflectionService->getAccessibleProperty(...$data->translationLocaleProperty);
146147

147148
return $self;
148149
}
@@ -192,7 +193,7 @@ private function findTranslatedProperties(ClassMetadataInfo $cm, ClassMetadataFa
192193
continue;
193194
}
194195

195-
$reflectionProperty = $cm->getReflectionClass()->getProperty($fieldName);
196+
$reflectionProperty = $cm->getReflectionProperty($fieldName);
196197
$attributes = $reflectionProperty->getAttributes(Attribute\Translatable::class);
197198

198199
if (!$attributes) {

tests/Doctrine/TranslatableClassMetadataTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Webfactory\Bundle\PolyglotBundle\Tests\Doctrine;
44

55
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
6+
use Doctrine\Persistence\Mapping\RuntimeReflectionService;
67
use PHPUnit\Framework\TestCase;
78
use Webfactory\Bundle\PolyglotBundle\Doctrine\TranslatableClassMetadata;
89
use Webfactory\Bundle\PolyglotBundle\Tests\TestEntity;
@@ -19,7 +20,7 @@ public function can_be_serialized_and_retrieved(): void
1920
$metadata = $this->createMetadata();
2021

2122
$serialize = serialize($metadata->sleep());
22-
$unserialized = TranslatableClassMetadata::wakeup(unserialize($serialize));
23+
$unserialized = TranslatableClassMetadata::wakeup(unserialize($serialize), new RuntimeReflectionService());
2324

2425
self::assertEquals($metadata, $unserialized);
2526
}

0 commit comments

Comments
 (0)