-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
MetadataLoadInterceptor.php
134 lines (116 loc) · 4.46 KB
/
MetadataLoadInterceptor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
declare(strict_types=1);
/*
* Go! AOP framework
*
* @copyright Copyright 2012, Lisachenko Alexander <lisachenko.it@gmail.com>
*
* This source file is subject to the license that is bundled
* with this source code in the file LICENSE.
*/
namespace Go\Bridge\Doctrine;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\ClassMetadata;
use Go\Core\AspectContainer;
use InvalidArgumentException;
use ReflectionClass;
use RuntimeException;
/**
* Class MetadataLoadInterceptor
*
* Support for weaving Doctrine entities.
*/
final class MetadataLoadInterceptor implements EventSubscriber
{
/**
* {@inheritdoc}
*/
public function getSubscribedEvents(): array
{
return [
Events::loadClassMetadata
];
}
/**
* Handles \Doctrine\ORM\Events::loadClassMetadata event by modifying metadata of Go! AOP proxied classes.
*
* This method intercepts loaded metadata of Doctrine's entities which are weaved by Go! AOP,
* and denotes them as mapped superclass. If weaved entities uses mappings from traits
* (such as Timestampable, Blameable, etc... from https://github.com/Atlantic18/DoctrineExtensions),
* it will remove all mappings from proxied class for fields inherited from traits in order to prevent
* collision with concrete subclass of weaved entity. Fields from trait will be present in concrete subclass
* of weaved entitites.
*
* @see http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html#mapped-superclasses
* @see https://github.com/Atlantic18/DoctrineExtensions
*/
public function loadClassMetadata(LoadClassMetadataEventArgs $args): void
{
$metadata = $args->getClassMetadata();
if (1 === preg_match(sprintf('/.+(%s)$/', AspectContainer::AOP_PROXIED_SUFFIX), $metadata->name)) {
$metadata->isMappedSuperclass = true;
$metadata->isEmbeddedClass = false;
$metadata->table = [];
$metadata->customRepositoryClassName = null;
$this->removeMappingsFromTraits($metadata);
}
}
/**
* Remove fields in Go! AOP proxied class metadata that are inherited
* from traits.
*/
private function removeMappingsFromTraits(ClassMetadata $metadata): void
{
$traits = $this->getTraits($metadata->name);
foreach ($traits as $trait) {
$trait = new ReflectionClass($trait);
foreach ($trait->getProperties() as $property) {
$name = $property->getName();
if (isset($metadata->fieldMappings[$name])) {
$mapping = $metadata->fieldMappings[$name];
unset(
$metadata->fieldMappings[$name],
$metadata->fieldNames[$mapping['columnName']],
$metadata->columnNames[$name]
);
}
}
}
}
/**
* Get ALL traits used by one class.
*
* This method is copied from
* https://github.com/RunOpenCode/traitor-bundle/blob/master/src/RunOpenCode/Bundle/Traitor/Utils/ClassUtils.php
*
* @param class-string $className FQCN
* @param bool $autoload Weather to autoload class.
*
* @throws InvalidArgumentException
* @throws RuntimeException
*/
private function getTraits(string $className, bool $autoload = true): array
{
if (!class_exists($className)) {
throw new RuntimeException(sprintf('Class "%s" does not exists or it can not be autoloaded.', $className));
}
$traits = [];
// Get traits of all parent classes
do {
$traits = array_merge(class_uses($className, $autoload), $traits);
$className = get_parent_class($className);
} while ($className);
$traitsToSearch = $traits;
while (count($traitsToSearch) > 0) {
$newTraits = class_uses(array_pop($traitsToSearch), $autoload);
$traits = array_merge($newTraits, $traits);
$traitsToSearch = array_merge($newTraits, $traitsToSearch);
}
foreach ($traits as $trait => $same) {
$traits = array_merge(class_uses($trait, $autoload), $traits);
}
return array_unique(array_map(fn($fqcn) => ltrim($fqcn, '\\'), $traits));
}
}