Skip to content

Commit

Permalink
Allow doctrine/persistence ^3.1 (backport to 3.x) (#67)
Browse files Browse the repository at this point in the history
This backports #38 to the 3.x branch.
  • Loading branch information
mpdude authored Apr 24, 2024
1 parent 8efa14e commit edb88a6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion src/Doctrine/PolyglotListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
) {
}

Expand Down Expand Up @@ -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;

Expand Down
17 changes: 9 additions & 8 deletions src/Doctrine/TranslatableClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion tests/Doctrine/TranslatableClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down

0 comments on commit edb88a6

Please sign in to comment.