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