From 892b90d5e710eaf5941d29af5feb0a6827fd6d98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20H=C3=A9lias?= Date: Tue, 9 Apr 2024 10:17:51 +0200 Subject: [PATCH] Allow doctrine/orm 3 & doctrine/bdal 4 --- composer.json | 6 +++--- config/services.php | 5 ++++- src/Services/PropertyHydratorService.php | 8 +++++-- .../DoctrineCiphersweetSubscriber.php | 21 ++++++------------- tests/App/Model/MyEntityAttributeIndexes.php | 2 +- tests/App/doctrine.yaml | 3 +++ 6 files changed, 23 insertions(+), 22 deletions(-) diff --git a/composer.json b/composer.json index d29497b..66808cf 100644 --- a/composer.json +++ b/composer.json @@ -24,8 +24,8 @@ "php": ">=8.0", "ext-mbstring": "*", "doctrine/annotations": "^1.10 || ^2.0", - "doctrine/doctrine-bundle": "^2.7.1", - "doctrine/orm": "^2.14", + "doctrine/doctrine-bundle": "^2.8.0", + "doctrine/orm": "^2.14 || ^3.0", "paragonie/ciphersweet": "^3.0 || ^4.0", "symfony/config": "^5.4 || ^6.4 || ^7.0", "symfony/console": "^5.4 || ^6.4 || ^7.0", @@ -41,7 +41,7 @@ "require-dev": { "phpunit/phpunit": "^9.5", "symfony/browser-kit": "^5.4 || ^6.4 || ^7.0", - "symfony/doctrine-bridge": "^6.4", + "symfony/doctrine-bridge": "^6.4 || ^7.0", "symfony/phpunit-bridge": "^6.4", "symfony/yaml": "^5.4 || ^6.4 || ^7.0" }, diff --git a/config/services.php b/config/services.php index 6c96592..b176018 100644 --- a/config/services.php +++ b/config/services.php @@ -106,7 +106,10 @@ service('encryption.indexable_field'), service('encryption.property_hydrator') ]) - ->tag('doctrine.event_subscriber') + ->tag('doctrine.event_listener', ['event' => 'postLoad']) + ->tag('doctrine.event_listener', ['event' => 'onFlush']) + ->tag('doctrine.event_listener', ['event' => 'postFlush']) + ->tag('doctrine.event_listener', ['event' => 'onClear']) ->set('encryption.encrypted_fields', EncryptedFieldsService::class) ->args([ diff --git a/src/Services/PropertyHydratorService.php b/src/Services/PropertyHydratorService.php index ddadaed..80f072a 100644 --- a/src/Services/PropertyHydratorService.php +++ b/src/Services/PropertyHydratorService.php @@ -40,8 +40,12 @@ public function setValueToMappedField(#[\SensitiveParameter] object $entity, #[\ return; } - $propertyInfoType = $this->propertyInfoExtractor->getTypes(get_class($entity), $propertyName)[0]; - $targetType = $propertyInfoType->getBuiltinType(); + if (property_exists($this->propertyInfoExtractor, 'getType')) { + $targetType = (string) $this->propertyInfoExtractor->getType(); + } else { + $propertyInfoType = $this->propertyInfoExtractor->getTypes(get_class($entity), $propertyName)[0]; + $targetType = $propertyInfoType->getBuiltinType(); + } if ($targetType !== Type::BUILTIN_TYPE_STRING) { settype($value, $targetType); diff --git a/src/Subscribers/DoctrineCiphersweetSubscriber.php b/src/Subscribers/DoctrineCiphersweetSubscriber.php index 55adaf8..5a0afba 100644 --- a/src/Subscribers/DoctrineCiphersweetSubscriber.php +++ b/src/Subscribers/DoctrineCiphersweetSubscriber.php @@ -4,7 +4,7 @@ namespace Odandb\DoctrineCiphersweetEncryptionBundle\Subscribers; -use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface; +use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener; use Doctrine\ORM\Event\PostLoadEventArgs; use Doctrine\Persistence\ObjectManager; use Doctrine\ORM\UnitOfWork; @@ -25,7 +25,11 @@ /** * @internal */ -class DoctrineCiphersweetSubscriber implements EventSubscriberInterface, ResetInterface +#[AsDoctrineListener(event: Events::postLoad)] +#[AsDoctrineListener(event: Events::onFlush)] +#[AsDoctrineListener(event: Events::postFlush)] +#[AsDoctrineListener(event: Events::onClear)] +class DoctrineCiphersweetSubscriber implements ResetInterface { public const ENCRYPTED_ANN_NAME = EncryptedField::class; public const INDEXABLE_ANN_NAME = IndexableField::class; @@ -91,19 +95,6 @@ public function __construct( $this->propertyHydratorService = $propertyHydratorService; } - /** - * {@inheritdoc} - */ - public function getSubscribedEvents(): array - { - return [ - Events::postLoad, - Events::onFlush, - Events::postFlush, - Events::onClear, - ]; - } - public function reset(): void { $this->_originalValues = []; diff --git a/tests/App/Model/MyEntityAttributeIndexes.php b/tests/App/Model/MyEntityAttributeIndexes.php index 1d41503..639d2db 100644 --- a/tests/App/Model/MyEntityAttributeIndexes.php +++ b/tests/App/Model/MyEntityAttributeIndexes.php @@ -17,6 +17,6 @@ class MyEntityAttributeIndexes implements IndexedEntityInterface use IndexedEntityAttributeTrait; #[ORM\ManyToOne(targetEntity: MyEntityAttribute::class)] - #[ORM\JoinColumn(name: 'target_entity_id', referencedColumnName: 'id', onDelete: 'CASCADE', nullable: false)] + #[ORM\JoinColumn(name: 'target_entity_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')] protected object $targetEntity; } diff --git a/tests/App/doctrine.yaml b/tests/App/doctrine.yaml index c745a20..f1eaf61 100644 --- a/tests/App/doctrine.yaml +++ b/tests/App/doctrine.yaml @@ -2,7 +2,10 @@ doctrine: orm: default_entity_manager: default auto_generate_proxy_classes: true + enable_lazy_ghost_objects: true naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware + controller_resolver: + auto_mapping: true auto_mapping: true mappings: Attribute: