From edb88a6c59936b5acfd898010cff85e2fc5fd79f Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Wed, 24 Apr 2024 17:46:24 +0200 Subject: [PATCH] Allow `doctrine/persistence` `^3.1` (backport to 3.x) (#67) This backports #38 to the 3.x branch. --- composer.json | 7 ++++--- src/Doctrine/PolyglotListener.php | 4 +++- src/Doctrine/TranslatableClassMetadata.php | 17 +++++++++-------- .../Doctrine/TranslatableClassMetadataTest.php | 3 ++- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/composer.json b/composer.json index 83a5781..4fc65a4 100644 --- a/composer.json +++ b/composer.json @@ -24,9 +24,9 @@ "doctrine/annotations": "^1.12", "doctrine/collections": "^1.0", "doctrine/dbal": "^2.3|^3.0", - "doctrine/event-manager": "^1.0", - "doctrine/orm": "^2.10", - "doctrine/persistence": "^1.3.8 | ^2.1", + "doctrine/event-manager": "^1.0|^2.0", + "doctrine/orm": "^2.13", + "doctrine/persistence": "^1.3.8|^2.1|^3.1", "psr/log": "^1.0", "symfony/config": "^5.4|^6.4|^7.0", "symfony/dependency-injection": "^5.4|^6.4|^7.0", @@ -40,6 +40,7 @@ "doctrine/common": "^2.0|^3.1", "doctrine/doctrine-bundle": "^2.0", "phpunit/phpunit": "^9.6.18", + "symfony/doctrine-bridge": "^5.4|^6.4", "symfony/error-handler": "^5.4|^6.4|^7.0", "symfony/phpunit-bridge": ">= 7.0", "symfony/yaml": "^5.4|^6.4|^7.0", diff --git a/src/Doctrine/PolyglotListener.php b/src/Doctrine/PolyglotListener.php index 999f6e6..d158b7d 100644 --- a/src/Doctrine/PolyglotListener.php +++ b/src/Doctrine/PolyglotListener.php @@ -14,6 +14,7 @@ use Doctrine\ORM\Event\PostFlushEventArgs; use Doctrine\ORM\Event\PreFlushEventArgs; use Doctrine\Persistence\Event\LifecycleEventArgs; +use Doctrine\Persistence\Mapping\RuntimeReflectionService; use Psr\Log\LoggerInterface; use Psr\Log\NullLogger; use WeakReference; @@ -52,6 +53,7 @@ public function __construct( private readonly Reader $annotationReader, private readonly DefaultLocaleProvider $defaultLocaleProvider, private readonly LoggerInterface $logger = null ?? new NullLogger(), + private readonly RuntimeReflectionService $reflectionService = new RuntimeReflectionService(), ) { } @@ -144,7 +146,7 @@ private function loadTranslationMetadataForClass($className, EntityManager $em): return null; } else { - $wakeup = TranslatableClassMetadata::wakeup($data); + $wakeup = TranslatableClassMetadata::wakeup($data, $this->reflectionService); $wakeup->setLogger($this->logger); $this->translatedClasses[$className] = $wakeup; diff --git a/src/Doctrine/TranslatableClassMetadata.php b/src/Doctrine/TranslatableClassMetadata.php index 7aab9fb..e943ac6 100644 --- a/src/Doctrine/TranslatableClassMetadata.php +++ b/src/Doctrine/TranslatableClassMetadata.php @@ -13,6 +13,7 @@ use Doctrine\ORM\EntityManager; use Doctrine\ORM\Mapping\ClassMetadataFactory; use Doctrine\ORM\Mapping\ClassMetadataInfo; +use Doctrine\Persistence\Mapping\RuntimeReflectionService; use Psr\Log\LoggerInterface; use ReflectionClass; use ReflectionProperty; @@ -128,23 +129,23 @@ public function sleep(): SerializedTranslatableClassMetadata return $sleep; } - public static function wakeup(SerializedTranslatableClassMetadata $data): self + public static function wakeup(SerializedTranslatableClassMetadata $data, RuntimeReflectionService $reflectionService): self { $self = new self($data->class); $self->primaryLocale = $data->primaryLocale; - $self->translationClass = new ReflectionClass($data->translationClass); + $self->translationClass = $reflectionService->getClass($data->translationClass); foreach ($data->translationFieldMapping as $fieldname => $property) { - $self->translationFieldMapping[$fieldname] = new ReflectionProperty(...$property); + $self->translationFieldMapping[$fieldname] = $reflectionService->getAccessibleProperty(...$property); } foreach ($data->translatedProperties as $fieldname => $property) { - $self->translatedProperties[$fieldname] = new ReflectionProperty(...$property); + $self->translatedProperties[$fieldname] = $reflectionService->getAccessibleProperty(...$property); } - $self->translationsCollectionProperty = new ReflectionProperty(...$data->translationsCollectionProperty); - $self->translationMappingProperty = new ReflectionProperty(...$data->translationMappingProperty); - $self->translationLocaleProperty = new ReflectionProperty(...$data->translationLocaleProperty); + $self->translationsCollectionProperty = $reflectionService->getAccessibleProperty(...$data->translationsCollectionProperty); + $self->translationMappingProperty = $reflectionService->getAccessibleProperty(...$data->translationMappingProperty); + $self->translationLocaleProperty = $reflectionService->getAccessibleProperty(...$data->translationLocaleProperty); return $self; } @@ -195,7 +196,7 @@ private function findTranslatedProperties(ClassMetadataInfo $cm, Reader $reader, } $foundAttributeOrAnnotation = null; - $reflectionProperty = $cm->getReflectionClass()->getProperty($fieldName); + $reflectionProperty = $cm->getReflectionProperty($fieldName); $attributes = $reflectionProperty->getAttributes(Attribute\Translatable::class); if ($attributes) { diff --git a/tests/Doctrine/TranslatableClassMetadataTest.php b/tests/Doctrine/TranslatableClassMetadataTest.php index 27beea9..b5d0a36 100644 --- a/tests/Doctrine/TranslatableClassMetadataTest.php +++ b/tests/Doctrine/TranslatableClassMetadataTest.php @@ -3,6 +3,7 @@ namespace Webfactory\Bundle\PolyglotBundle\Tests\Doctrine; use Doctrine\Common\Annotations\AnnotationReader; +use Doctrine\Persistence\Mapping\RuntimeReflectionService; use PHPUnit\Framework\TestCase; use Webfactory\Bundle\PolyglotBundle\Doctrine\TranslatableClassMetadata; use Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity\TestEntity; @@ -19,7 +20,7 @@ public function can_be_serialized_and_retrieved(): void $metadata = $this->createMetadata(); $serialize = serialize($metadata->sleep()); - $unserialized = TranslatableClassMetadata::wakeup(unserialize($serialize)); + $unserialized = TranslatableClassMetadata::wakeup(unserialize($serialize), new RuntimeReflectionService()); self::assertEquals($metadata, $unserialized); }