|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace DH\Auditor\Provider\Doctrine\Auditing\Annotation; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
8
|
|
|
use Doctrine\ORM\Mapping\Entity; |
|
9
|
|
|
use Doctrine\Persistence\Mapping\ClassMetadata; |
|
10
|
|
|
use ReflectionClass; |
|
11
|
|
|
|
|
12
|
|
|
class AnnotationLoader |
|
13
|
|
|
{ |
|
14
|
|
|
private EntityManagerInterface $entityManager; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct(EntityManagerInterface $entityManager) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->entityManager = $entityManager; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function load(): array |
|
22
|
|
|
{ |
|
23
|
|
|
$configuration = []; |
|
24
|
|
|
|
|
25
|
|
|
$metadatas = $this->entityManager->getMetadataFactory()->getAllMetadata(); |
|
26
|
|
|
foreach ($metadatas as $metadata) { |
|
27
|
|
|
$config = $this->getEntityConfiguration($metadata); |
|
28
|
|
|
if (null !== $config) { |
|
29
|
|
|
$configuration[$metadata->getName()] = $config; |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
return $configuration; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
private function getEntityConfiguration(ClassMetadata $metadata): ?array |
|
37
|
|
|
{ |
|
38
|
|
|
$annotation = null; |
|
39
|
|
|
$auditableAnnotation = null; |
|
40
|
|
|
$securityAnnotation = null; |
|
41
|
|
|
$reflection = $metadata->getReflectionClass(); |
|
42
|
|
|
|
|
43
|
|
|
// Check that we have an Entity annotation or attribute |
|
44
|
|
|
$attributes = $reflection->getAttributes(Entity::class); |
|
45
|
|
|
if (\is_array($attributes) && [] !== $attributes) { |
|
46
|
|
|
$annotation = $attributes[0]->newInstance(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if (null === $annotation) { |
|
50
|
|
|
return null; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// Check that we have an Auditable annotation or attribute |
|
54
|
|
|
$attributes = $reflection->getAttributes(Auditable::class); |
|
55
|
|
|
if (\is_array($attributes) && [] !== $attributes) { |
|
56
|
|
|
$auditableAnnotation = $attributes[0]->newInstance(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if (null === $auditableAnnotation) { |
|
60
|
|
|
return null; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// Check that we have a Security annotation or attribute |
|
64
|
|
|
$attributes = $reflection->getAttributes(Security::class); |
|
65
|
|
|
if (\is_array($attributes) && [] !== $attributes) { |
|
66
|
|
|
$securityAnnotation = $attributes[0]->newInstance(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$roles = null === $securityAnnotation ? null : [Security::VIEW_SCOPE => $securityAnnotation->view]; |
|
70
|
|
|
|
|
71
|
|
|
// Are there any Ignore annotation or attribute? |
|
72
|
|
|
$ignoredColumns = $this->getAllProperties($reflection); |
|
73
|
|
|
|
|
74
|
|
|
return [ |
|
75
|
|
|
'ignored_columns' => $ignoredColumns, |
|
76
|
|
|
'enabled' => $auditableAnnotation->enabled, |
|
77
|
|
|
'roles' => $roles, |
|
78
|
|
|
]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private function getAllProperties(ReflectionClass $reflection): array |
|
82
|
|
|
{ |
|
83
|
|
|
$annotationProperty = null; |
|
84
|
|
|
$properties = []; |
|
85
|
|
|
|
|
86
|
|
|
foreach ($reflection->getProperties() as $property) { |
|
87
|
|
|
$attributes = $property->getAttributes(Ignore::class); |
|
88
|
|
|
if (\is_array($attributes) && [] !== $attributes) { |
|
89
|
|
|
$annotationProperty = $attributes[0]->newInstance(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if (null !== $annotationProperty) { |
|
93
|
|
|
$properties[] = $property->getName(); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if (false !== $reflection->getParentClass()) { |
|
98
|
|
|
$properties = array_unique(array_merge($this->getAllProperties($reflection->getParentClass()), $properties)); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $properties; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|