Skip to content

Commit

Permalink
Fix for getting properties from proxy classes
Browse files Browse the repository at this point in the history
  • Loading branch information
mogilvie committed Jun 4, 2024
1 parent d19fc56 commit bd38aff
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/Subscribers/DoctrineEncryptSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();

Expand All @@ -249,6 +250,7 @@ protected function getEncryptedFields(object $entity): array
$encryptedFields = [];

foreach ($properties as $key => $refProperty) {

if ($this->isEncryptedProperty($refProperty)) {
$encryptedFields[$key] = $refProperty;
}
Expand All @@ -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;
Expand All @@ -285,4 +289,10 @@ private function isEncryptedProperty(ReflectionProperty $refProperty)

return false;
}

protected function getOriginalEntityReflection($entity): \ReflectionClass
{
$realClassName = ClassUtils::getClass($entity);
return new \ReflectionClass($realClassName);
}
}

0 comments on commit bd38aff

Please sign in to comment.