Skip to content

Commit 9158921

Browse files
author
Bjorn Van Acker
authored
Merge pull request #147 from sumocoders/25-04-2024-use-actual-doctrine-entity-classes-and-no-proxies-for-trail
Use doctrine helper class to get the actual class name
2 parents 40dd5f2 + 124b192 commit 9158921

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

src/DoctrineListener/DoctrineAuditListener.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace SumoCoders\FrameworkCoreBundle\DoctrineListener;
44

55
use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
6+
use Doctrine\Common\Util\ClassUtils;
67
use Doctrine\ORM\EntityManagerInterface;
78
use Doctrine\ORM\Event\PostPersistEventArgs;
89
use Doctrine\ORM\Event\PostRemoveEventArgs;
@@ -31,9 +32,9 @@
3132
class DoctrineAuditListener
3233
{
3334
public function __construct(
34-
private readonly AuditLogger $auditLogger,
35+
private readonly AuditLogger $auditLogger,
3536
private readonly SerializerInterface $serializer,
36-
private $removals = [],
37+
private $removals = [],
3738
) {
3839
}
3940

@@ -214,13 +215,13 @@ private function getIdForEntity(object $entity): ?string
214215

215216
foreach ($properties as $property) {
216217
if ($this->isPropertyPrimaryKey($entity, $property)) {
217-
return (string) $serializedData[$property];
218+
return (string)$serializedData[$property];
218219
}
219220
}
220221

221222
foreach (['getId', 'getUuid'] as $method) {
222223
if (in_array($method, $methods)) {
223-
return (string) $entity->$method();
224+
return (string)$entity->$method();
224225
}
225226
}
226227

@@ -236,19 +237,19 @@ private function getIdentifierForEntity(object $entity): ?string
236237

237238
foreach ($properties as $property) {
238239
if ($this->isPropertyIdentifier($entity, $property)) {
239-
return (string) $serializedData[$property];
240+
return (string)$serializedData[$property];
240241
}
241242
}
242243

243244
foreach ($methods as $method) {
244245
if ($this->isMethodIdentifier($entity, $method)) {
245-
return (string) $entity->$method();
246+
return (string)$entity->$method();
246247
}
247248
}
248249

249250
foreach (['__toString', 'getName', 'getTitle', 'getId', 'getUuid'] as $method) {
250251
if (in_array($method, $methods)) {
251-
return (string) $entity->$method();
252+
return (string)$entity->$method();
252253
}
253254
}
254255

@@ -303,7 +304,8 @@ private function classHasAttribute(
303304
object $entity,
304305
string $attribute
305306
): bool {
306-
$reflectionClass = new ReflectionClass($entity);
307+
$classString = ClassUtils::getRealClass($entity);
308+
$reflectionClass = new ReflectionClass($classString);
307309
return count($reflectionClass->getAttributes($attribute)) > 0;
308310
}
309311

@@ -312,7 +314,8 @@ private function propertyHasAttribute(
312314
string $property,
313315
string $attribute
314316
): bool {
315-
$reflectionClass = new ReflectionClass($entity);
317+
$classString = ClassUtils::getRealClass($entity);
318+
$reflectionClass = new ReflectionClass($classString);
316319
$properties = $reflectionClass->getProperties();
317320
foreach ($properties as $item) {
318321
if (
@@ -331,7 +334,8 @@ private function methodHasAttribute(
331334
string $method,
332335
string $attribute
333336
): bool {
334-
$reflectionClass = new ReflectionClass($entity);
337+
$classString = ClassUtils::getRealClass($entity);
338+
$reflectionClass = new ReflectionClass($classString);
335339
$methods = $reflectionClass->getMethods();
336340
foreach ($methods as $item) {
337341
if (
@@ -359,7 +363,8 @@ private function showPropertyDataForEntity(object $entity): bool
359363

360364
private function showDataForEntity(object $entity): bool
361365
{
362-
$reflectionClass = new ReflectionClass($entity);
366+
$classString = ClassUtils::getRealClass($entity);
367+
$reflectionClass = new ReflectionClass($classString);
363368
if ($reflectionClass->getAttributes(DisplayAllEntityFieldWithDataInLog::class)) {
364369
return true;
365370
}
@@ -369,7 +374,8 @@ private function showDataForEntity(object $entity): bool
369374

370375
private function getMethods(object $entity): array
371376
{
372-
$reflectionClass = new ReflectionClass($entity);
377+
$classString = ClassUtils::getRealClass($entity);
378+
$reflectionClass = new ReflectionClass($classString);
373379
$methods = $reflectionClass->getMethods();
374380

375381
return array_map(
@@ -380,7 +386,8 @@ private function getMethods(object $entity): array
380386

381387
private function getProperties(object $entity): array
382388
{
383-
$reflectionClass = new ReflectionClass($entity);
389+
$classString = ClassUtils::getRealClass($entity);
390+
$reflectionClass = new ReflectionClass($classString);
384391
$properties = $reflectionClass->getProperties();
385392

386393
return array_map(

0 commit comments

Comments
 (0)