From 849d80d657a5ade2f3c358c60cfccce12f9de5d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Je=CC=81re=CC=81my=20DECOOL?= Date: Thu, 29 Jan 2026 12:41:17 +0100 Subject: [PATCH 1/3] Use ObjectManager from event args instead of injected EntityManager in DoctrineEncryptListener --- src/EventListener/DoctrineEncryptListener.php | 31 +++++++++++-------- .../DoctrineEncryptListenerInterface.php | 1 + 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/EventListener/DoctrineEncryptListener.php b/src/EventListener/DoctrineEncryptListener.php index ba0bfd0..a2c6fd5 100644 --- a/src/EventListener/DoctrineEncryptListener.php +++ b/src/EventListener/DoctrineEncryptListener.php @@ -6,6 +6,7 @@ use Doctrine\ORM\Event\OnFlushEventArgs; use Doctrine\ORM\Events; use Doctrine\Persistence\Event\LifecycleEventArgs; +use Doctrine\Persistence\ObjectManager; use ReflectionProperty; use SpecShaper\EncryptBundle\Encryptors\EncryptorInterface; use SpecShaper\EncryptBundle\Exception\EncryptException; @@ -42,6 +43,7 @@ class DoctrineEncryptListener implements DoctrineEncryptListenerInterface public function __construct( private readonly EncryptorInterface $encryptor, + #[\Deprecated] private readonly EntityManagerInterface $em, array $annotationArray, bool $isDisabled @@ -77,14 +79,15 @@ public function onFlush(OnFlushEventArgs $args): void return; } - $unitOfWork = $this->em->getUnitOfWork(); + $objectManager = $args->getObjectManager(); + $unitOfWork = $objectManager->getUnitOfWork(); foreach ($unitOfWork->getScheduledEntityInsertions() as $entity) { - $this->processFields($entity, true, true); + $this->processFields($objectManager, $entity, true, true); } foreach ($unitOfWork->getScheduledEntityUpdates() as $entity) { - $this->processFields($entity, true, false); + $this->processFields($objectManager, $entity, true, false); } } @@ -99,7 +102,7 @@ public function postLoad(LifecycleEventArgs $args): void $entity = $args->getObject(); // Decrypt the entity fields. - $this->processFields($entity, false, false); + $this->processFields($args->getObjectManager(), $entity, false, false); } /** @@ -130,19 +133,19 @@ public function getEncryptionableProperties(array $allProperties): array /** * Process (encrypt/decrypt) entities fields. */ - protected function processFields(object $entity, bool $isEncryptOperation, bool $isInsert): bool + protected function processFields(ObjectManager $objectManager,object $entity, bool $isEncryptOperation, bool $isInsert): bool { // Get the encrypted properties in the entity. - $properties = $this->getEncryptedFields($entity); + $properties = $this->getEncryptedFields($objectManager, $entity); // If no encrypted properties, return false. if (empty($properties)) { return false; } - $unitOfWork = $this->em->getUnitOfWork(); + $unitOfWork = $objectManager->getUnitOfWork(); $oid = spl_object_id($entity); - $meta = $this->em->getClassMetadata(get_class($entity)); + $meta = $objectManager->getClassMetadata(get_class($entity)); foreach ($properties as $refProperty) { @@ -201,9 +204,11 @@ public function postUpdate(LifecycleEventArgs $args): void $entity = $args->getObject(); $oid = spl_object_id($entity); + $objectManager = $args->getObjectManager(); + if (isset($this->rawValues[$oid])) { $className = get_class($entity); - $meta = $this->em->getClassMetadata($className); + $meta = $objectManager->getClassMetadata($className); foreach ($this->rawValues[$oid] as $prop => $rawValue) { $refProperty = $meta->getReflectionProperty($prop); $refProperty->setValue($entity, $rawValue); @@ -216,9 +221,9 @@ public function postUpdate(LifecycleEventArgs $args): void /** * @return array */ - protected function getEncryptedFields(object $entity): array + protected function getEncryptedFields(ObjectManager $objectManager, object $entity): array { - $reflectionClass = $this->getOriginalEntityReflection($entity); + $reflectionClass = $this->getOriginalEntityReflection($objectManager, $entity); $className = $reflectionClass->getName(); @@ -257,9 +262,9 @@ private function isEncryptedProperty(ReflectionProperty $refProperty) return false; } - protected function getOriginalEntityReflection($entity): \ReflectionClass + protected function getOriginalEntityReflection(ObjectManager $objectManager, $entity): \ReflectionClass { - $realClassName = $this->em->getClassMetadata(get_class($entity))->getName(); + $realClassName = $objectManager->getClassMetadata(get_class($entity))->getName(); return new \ReflectionClass($realClassName); } } diff --git a/src/EventListener/DoctrineEncryptListenerInterface.php b/src/EventListener/DoctrineEncryptListenerInterface.php index 992f040..45b774d 100644 --- a/src/EventListener/DoctrineEncryptListenerInterface.php +++ b/src/EventListener/DoctrineEncryptListenerInterface.php @@ -16,6 +16,7 @@ interface DoctrineEncryptListenerInterface public function __construct( EncryptorInterface $encryptor, + #[\Deprecated] EntityManagerInterface $em, array $annotationArray, bool $isDisabled From e422667f7996055b51ac52336ec010218d1662fe Mon Sep 17 00:00:00 2001 From: Mark Ogilvie Date: Thu, 19 Feb 2026 10:57:30 +0000 Subject: [PATCH 2/3] Update DoctrineEncryptListener.php Move deprecation from constructor to doc block for compatibility with other php and symfony versions. --- src/EventListener/DoctrineEncryptListener.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/EventListener/DoctrineEncryptListener.php b/src/EventListener/DoctrineEncryptListener.php index a2c6fd5..45b40c0 100644 --- a/src/EventListener/DoctrineEncryptListener.php +++ b/src/EventListener/DoctrineEncryptListener.php @@ -41,9 +41,11 @@ class DoctrineEncryptListener implements DoctrineEncryptListenerInterface private bool $isDisabled; + /** + * @param EntityManagerInterface $em Deprecated in favour of fetching object manager from event args. + */ public function __construct( private readonly EncryptorInterface $encryptor, - #[\Deprecated] private readonly EntityManagerInterface $em, array $annotationArray, bool $isDisabled From 39955ca8163334df53b4d349cd28d8d96f541d53 Mon Sep 17 00:00:00 2001 From: Mark Ogilvie Date: Thu, 19 Feb 2026 10:58:59 +0000 Subject: [PATCH 3/3] Update DoctrineEncryptListenerInterface.php --- src/EventListener/DoctrineEncryptListenerInterface.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/EventListener/DoctrineEncryptListenerInterface.php b/src/EventListener/DoctrineEncryptListenerInterface.php index 45b774d..7f44788 100644 --- a/src/EventListener/DoctrineEncryptListenerInterface.php +++ b/src/EventListener/DoctrineEncryptListenerInterface.php @@ -13,10 +13,12 @@ interface DoctrineEncryptListenerInterface { public const ENCRYPTED_SUFFIX = ''; - + + /** + * @param EntityManagerInterface $em Deprecated in favour of getting object manager from event args. + */ public function __construct( EncryptorInterface $encryptor, - #[\Deprecated] EntityManagerInterface $em, array $annotationArray, bool $isDisabled