1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author KonstantinKuklin <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace KonstantinKuklin\DoctrineCompressedFields\EventListener; |
8
|
|
|
|
9
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
10
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
11
|
|
|
use Doctrine\Common\Annotations\CachedReader; |
12
|
|
|
use Doctrine\Common\Annotations\DocParser; |
13
|
|
|
use Doctrine\Common\Cache\VoidCache; |
14
|
|
|
use Doctrine\Common\EventSubscriber; |
15
|
|
|
use Doctrine\ORM\Event\LoadClassMetadataEventArgs; |
16
|
|
|
use Doctrine\ORM\Events; |
17
|
|
|
use KonstantinKuklin\DoctrineCompressedFields\Annotation\Hub; |
18
|
|
|
use KonstantinKuklin\DoctrineCompressedFields\Annotation\Mask; |
19
|
|
|
use KonstantinKuklin\DoctrineCompressedFields\BitNormalizer; |
20
|
|
|
use KonstantinKuklin\DoctrineCompressedFields\MetadataLayer; |
21
|
|
|
|
22
|
|
|
class LoadClassMetadataListener implements EventSubscriber |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var AnnotationReader |
26
|
|
|
*/ |
27
|
|
|
private static $defaultAnnotationReader; |
28
|
|
|
|
29
|
|
|
public function __construct() |
30
|
|
|
{ |
31
|
|
|
$this->initDefaultAnnotationReader(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Create default annotation reader for extension |
36
|
|
|
* |
37
|
|
|
* @throws \RuntimeException |
38
|
|
|
*/ |
39
|
|
|
private function initDefaultAnnotationReader() |
40
|
|
|
{ |
41
|
|
|
if (null !== self::$defaultAnnotationReader) { |
42
|
|
|
return; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$docParser = new DocParser(); |
46
|
|
|
$docParser->setImports([ |
47
|
|
|
'Bits' => 'KonstantinKuklin\\DoctrineCompressedFields\\Annotation', |
48
|
|
|
]); |
49
|
|
|
$docParser->setIgnoreNotImportedAnnotations(true); |
50
|
|
|
|
51
|
|
|
$reader = new AnnotationReader($docParser); |
52
|
|
|
|
53
|
|
|
AnnotationRegistry::registerFile(__DIR__ . '/../Annotation/Hub.php'); |
|
|
|
|
54
|
|
|
AnnotationRegistry::registerFile(__DIR__ . '/../Annotation/Mask.php'); |
|
|
|
|
55
|
|
|
$reader = new CachedReader($reader, new VoidCache()); |
56
|
|
|
|
57
|
|
|
self::$defaultAnnotationReader = $reader; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function getSubscribedEvents() |
64
|
|
|
{ |
65
|
|
|
return [ |
66
|
|
|
Events::loadClassMetadata, |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param LoadClassMetadataEventArgs $eventArgs |
72
|
|
|
* |
73
|
|
|
* @throws \Doctrine\ORM\ORMException |
74
|
|
|
*/ |
75
|
8 |
|
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs) |
76
|
|
|
{ |
77
|
8 |
|
$classMetadata = $eventArgs->getClassMetadata(); |
78
|
8 |
|
if (MetadataLayer::isAlreadyProcessed($classMetadata)) { |
79
|
|
|
// we can skip it |
80
|
|
|
return; |
81
|
|
|
} |
82
|
|
|
|
83
|
8 |
|
$fieldNames = array_flip($classMetadata->getFieldNames()); |
84
|
8 |
|
$reflectionClass = $classMetadata->getReflectionClass(); |
85
|
|
|
|
86
|
8 |
|
foreach ($reflectionClass->getProperties() as $reflectionProperty) { |
87
|
8 |
|
if (isset($fieldNames[$reflectionProperty->getName()])) { |
88
|
8 |
|
$this->loadHubProperty($reflectionProperty, $classMetadata); |
89
|
|
|
} else { |
90
|
8 |
|
$this->loadMaskProperty($reflectionProperty, $classMetadata); |
91
|
|
|
} |
92
|
|
|
} |
93
|
8 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param \ReflectionProperty $reflectionProperty |
97
|
|
|
* @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $classMetadata |
98
|
|
|
* |
99
|
|
|
* @throws \Exception |
100
|
|
|
*/ |
101
|
8 |
|
private function loadMaskProperty($reflectionProperty, $classMetadata) |
102
|
|
|
{ |
103
|
|
|
/** @var Mask $maskAnnotation */ |
104
|
8 |
|
$maskAnnotation = self::$defaultAnnotationReader->getPropertyAnnotation( |
105
|
8 |
|
$reflectionProperty, |
106
|
8 |
|
Mask::class |
107
|
|
|
); |
108
|
|
|
|
109
|
8 |
|
if (!$maskAnnotation) { |
110
|
|
|
return; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// normalize bit list |
114
|
8 |
|
$maskAnnotation->bits = BitNormalizer::getNormalized( |
|
|
|
|
115
|
8 |
|
$maskAnnotation->bits |
116
|
|
|
); |
117
|
|
|
|
118
|
8 |
|
MetadataLayer::setMask($classMetadata, $maskAnnotation, $reflectionProperty); |
119
|
8 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param \ReflectionProperty $reflectionProperty |
123
|
|
|
* @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $classMetadata |
124
|
|
|
*/ |
125
|
8 |
|
private function loadHubProperty($reflectionProperty, $classMetadata) |
126
|
|
|
{ |
127
|
8 |
|
$hubAnnotation = self::$defaultAnnotationReader->getPropertyAnnotation( |
128
|
8 |
|
$reflectionProperty, |
129
|
8 |
|
Hub::class |
130
|
|
|
); |
131
|
|
|
|
132
|
8 |
|
if (!$hubAnnotation) { |
133
|
8 |
|
return; |
134
|
|
|
} |
135
|
|
|
|
136
|
8 |
|
MetadataLayer::setHub($classMetadata, $hubAnnotation, $reflectionProperty); |
137
|
8 |
|
} |
138
|
|
|
} |
139
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.