1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oro\Bundle\EntityExtendBundle\EventListener; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
7
|
|
|
use Doctrine\Common\Annotations\Reader; |
8
|
|
|
use Doctrine\ORM\Event\OnFlushEventArgs; |
9
|
|
|
use Doctrine\ORM\Mapping\MappingException; |
10
|
|
|
use Doctrine\ORM\Event\LoadClassMetadataEventArgs; |
11
|
|
|
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
12
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadataFactory; |
13
|
|
|
|
14
|
|
|
use Oro\Bundle\EntityExtendBundle\ORM\ExtendMetadataBuilder; |
15
|
|
|
use Oro\Bundle\EntityExtendBundle\Entity\Manager\MultiEnumManager; |
16
|
|
|
use Oro\Bundle\EntityExtendBundle\Annotation\ORM\DiscriminatorValue; |
17
|
|
|
use Oro\Bundle\EntityConfigBundle\DependencyInjection\Utils\ServiceLink; |
18
|
|
|
|
19
|
|
|
class DoctrineListener |
20
|
|
|
{ |
21
|
|
|
const ANNOTATION_DISCRIMINATOR_VALUE = 'Oro\\Bundle\\EntityExtendBundle\\Annotation\\ORM\\DiscriminatorValue'; |
22
|
|
|
|
23
|
|
|
/** @var ServiceLink */ |
24
|
|
|
protected $metadataBuilderServiceLink; |
25
|
|
|
|
26
|
|
|
/** @var MultiEnumManager */ |
27
|
|
|
protected $multiEnumManager; |
28
|
|
|
|
29
|
|
|
/** @var array */ |
30
|
|
|
protected $collectedMaps = []; |
31
|
|
|
|
32
|
|
|
/** @var array */ |
33
|
|
|
protected $collectedValues = []; |
34
|
|
|
|
35
|
|
|
/** @var Reader */ |
36
|
|
|
protected $annotationReader; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param ServiceLink $metadataBuilderLink The link to ExtendMetadataBuilder |
40
|
|
|
* @param MultiEnumManager $multiEnumManager |
41
|
|
|
* @param Reader $reader |
42
|
|
|
*/ |
43
|
|
|
public function __construct(ServiceLink $metadataBuilderLink, MultiEnumManager $multiEnumManager, Reader $reader) |
44
|
|
|
{ |
45
|
|
|
$this->metadataBuilderServiceLink = $metadataBuilderLink; |
46
|
|
|
$this->multiEnumManager = $multiEnumManager; |
47
|
|
|
$this->annotationReader = $reader; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param LoadClassMetadataEventArgs $event |
52
|
|
|
*/ |
53
|
|
|
public function loadClassMetadata(LoadClassMetadataEventArgs $event) |
54
|
|
|
{ |
55
|
|
|
$classMetadata = $event->getClassMetadata(); |
56
|
|
|
$className = $classMetadata->getName(); |
57
|
|
|
|
58
|
|
|
/** @var ExtendMetadataBuilder $metadataBuilder */ |
59
|
|
|
$metadataBuilder = $this->metadataBuilderServiceLink->getService(); |
60
|
|
|
if ($metadataBuilder->supports($className)) { |
61
|
|
|
$classMetadataBuilder = new ClassMetadataBuilder($classMetadata); |
62
|
|
|
$metadataBuilder->build($classMetadataBuilder, $className); |
63
|
|
|
$event->getEntityManager() |
64
|
|
|
->getMetadataFactory() |
65
|
|
|
->setMetadataFor($className, $classMetadata); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->processDiscriminatorValues($classMetadata, $event->getObjectManager()); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param OnFlushEventArgs $event |
73
|
|
|
*/ |
74
|
|
|
public function onFlush(OnFlushEventArgs $event) |
75
|
|
|
{ |
76
|
|
|
$this->multiEnumManager->handleOnFlush($event); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Collecting discriminator map entries from child classes for entities with inheritance not equals NONE |
81
|
|
|
* |
82
|
|
|
* @param ClassMetadata $class |
83
|
|
|
* @param EntityManager $em |
84
|
|
|
* |
85
|
|
|
* @throws MappingException |
86
|
|
|
*/ |
87
|
|
|
protected function processDiscriminatorValues(ClassMetadata $class, EntityManager $em) |
88
|
|
|
{ |
89
|
|
|
if (!$class->isInheritanceTypeNone()) { |
90
|
|
|
if ($class->isRootEntity()) { |
91
|
|
|
$allClasses = $em->getConfiguration()->getMetadataDriverImpl()->getAllClassNames(); |
92
|
|
|
$FQCN = $class->getName(); |
93
|
|
|
$map = $class->discriminatorMap ?: []; |
94
|
|
|
|
95
|
|
|
$duplicates = []; |
96
|
|
|
foreach ($allClasses as $subClassCandidate) { |
97
|
|
|
if (is_subclass_of($subClassCandidate, $FQCN) && !in_array($subClassCandidate, $map, true)) { |
98
|
|
|
$value = $this->getDiscriminatorValue($em->getMetadataFactory(), $subClassCandidate); |
99
|
|
|
|
100
|
|
|
if (null !== $value) { |
101
|
|
|
if (isset($map[$value])) { |
102
|
|
|
$duplicates[] = $value; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$map[$value] = $subClassCandidate; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ($duplicates) { |
111
|
|
|
throw MappingException::duplicateDiscriminatorEntry($class->getName(), $duplicates, $map); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$class->setDiscriminatorMap($map); |
115
|
|
|
$this->collectedMaps = array_merge($this->collectedMaps, array_fill_keys(array_values($map), $map)); |
116
|
|
|
} elseif (isset($this->collectedMaps[$class->name]) |
117
|
|
|
&& $class->discriminatorMap !== $this->collectedMaps[$class->name] |
118
|
|
|
) { |
119
|
|
|
$class->setDiscriminatorMap($this->collectedMaps[$class->name]); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param ClassMetadataFactory $factory |
126
|
|
|
* @param string $entityFQCN Class name |
127
|
|
|
* |
128
|
|
|
* @return mixed |
129
|
|
|
*/ |
130
|
|
|
protected function getDiscriminatorValue(ClassMetadataFactory $factory, $entityFQCN) |
131
|
|
|
{ |
132
|
|
|
if (!array_key_exists($entityFQCN, $this->collectedValues)) { |
133
|
|
|
$value = null; |
134
|
|
|
|
135
|
|
|
if ($factory->hasMetadataFor($entityFQCN)) { |
136
|
|
|
/** @var ClassMetadata $metadata */ |
137
|
|
|
$metadata = $factory->getMetadataFor($entityFQCN); |
138
|
|
|
$value = $metadata->discriminatorValue; |
139
|
|
|
} else { |
140
|
|
|
$ref = new \ReflectionClass($entityFQCN); |
141
|
|
|
$annotation = $this->annotationReader->getClassAnnotation($ref, self::ANNOTATION_DISCRIMINATOR_VALUE); |
142
|
|
|
|
143
|
|
|
if ($annotation instanceof DiscriminatorValue) { |
144
|
|
|
$value = $annotation->getValue(); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$this->collectedValues[$entityFQCN] = $value; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $this->collectedValues[$entityFQCN]; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.