From bd38aff4727bf901c03c169f1a8cd57ef2d3d220 Mon Sep 17 00:00:00 2001 From: Mark Ogilvie Date: Tue, 4 Jun 2024 10:25:57 +0100 Subject: [PATCH] Fix for getting properties from proxy classes --- src/Subscribers/DoctrineEncryptSubscriber.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Subscribers/DoctrineEncryptSubscriber.php b/src/Subscribers/DoctrineEncryptSubscriber.php index 09cbef5..b35b3b6 100644 --- a/src/Subscribers/DoctrineEncryptSubscriber.php +++ b/src/Subscribers/DoctrineEncryptSubscriber.php @@ -5,6 +5,7 @@ use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface; use Doctrine\Common\Annotations\Reader; use Doctrine\Common\EventSubscriber; +use Doctrine\Common\Util\ClassUtils; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Event\OnFlushEventArgs; use Doctrine\ORM\Events; @@ -114,6 +115,7 @@ public function onFlush(OnFlushEventArgs $args): void public function postLoad(LifecycleEventArgs $args): void { $entity = $args->getObject(); + // Decrypt the entity fields. $this->processFields($entity, $args->getObjectManager(), false, false); } @@ -235,8 +237,7 @@ public function postUpdate(LifecycleEventArgs $args): void */ protected function getEncryptedFields(object $entity): array { - // Create a ReflectionClass instance - $reflectionClass = new \ReflectionClass($entity); + $reflectionClass = $this->getOriginalEntityReflection($entity); $className = $reflectionClass->getName(); @@ -249,6 +250,7 @@ protected function getEncryptedFields(object $entity): array $encryptedFields = []; foreach ($properties as $key => $refProperty) { + if ($this->isEncryptedProperty($refProperty)) { $encryptedFields[$key] = $refProperty; } @@ -261,8 +263,10 @@ protected function getEncryptedFields(object $entity): array private function isEncryptedProperty(ReflectionProperty $refProperty) { + // If PHP8, and has attributes. if(method_exists($refProperty, 'getAttributes')) { + foreach ($refProperty->getAttributes() as $refAttribute) { if (in_array($refAttribute->getName(), $this->annotationArray)) { return true; @@ -285,4 +289,10 @@ private function isEncryptedProperty(ReflectionProperty $refProperty) return false; } + + protected function getOriginalEntityReflection($entity): \ReflectionClass + { + $realClassName = ClassUtils::getClass($entity); + return new \ReflectionClass($realClassName); + } }