9
9
final class ObjectMetadataResolver
10
10
{
11
11
12
- /** @var ?ObjectManager */
12
+ /** @var string|null */
13
+ private $ objectManagerLoader ;
14
+
15
+ /** @var ObjectManager|null|false */
13
16
private $ objectManager ;
14
17
15
- /** @var string */
18
+ /** @var string|null */
16
19
private $ repositoryClass ;
17
20
21
+ /** @var string|null */
22
+ private $ resolvedRepositoryClass ;
23
+
18
24
public function __construct (?string $ objectManagerLoader , ?string $ repositoryClass )
19
25
{
20
- if ($ objectManagerLoader !== null ) {
21
- $ this ->objectManager = $ this ->loadObjectManager ($ objectManagerLoader );
26
+ $ this ->objectManagerLoader = $ objectManagerLoader ;
27
+ $ this ->repositoryClass = $ repositoryClass ;
28
+ }
29
+
30
+ public function getObjectManager (): ?ObjectManager
31
+ {
32
+ if ($ this ->objectManager === false ) {
33
+ return null ;
22
34
}
23
- if ($ repositoryClass !== null ) {
24
- $ this ->repositoryClass = $ repositoryClass ;
25
- } elseif ($ this ->objectManager !== null && get_class ($ this ->objectManager ) === 'Doctrine\ODM\MongoDB\DocumentManager ' ) {
26
- $ this ->repositoryClass = 'Doctrine\ODM\MongoDB\DocumentRepository ' ;
27
- } else {
28
- $ this ->repositoryClass = 'Doctrine\ORM\EntityRepository ' ;
35
+
36
+ if ($ this ->objectManager !== null ) {
37
+ return $ this ->objectManager ;
29
38
}
39
+
40
+ if ($ this ->objectManagerLoader === null ) {
41
+ $ this ->objectManager = false ;
42
+
43
+ return null ;
44
+ }
45
+
46
+ $ this ->objectManager = $ this ->loadObjectManager ($ this ->objectManagerLoader );
47
+
48
+ return $ this ->objectManager ;
30
49
}
31
50
32
51
private function loadObjectManager (string $ objectManagerLoader ): ?ObjectManager
@@ -41,34 +60,46 @@ private function loadObjectManager(string $objectManagerLoader): ?ObjectManager
41
60
return require $ objectManagerLoader ;
42
61
}
43
62
63
+ private function getResolvedRepositoryClass (): string
64
+ {
65
+ if ($ this ->resolvedRepositoryClass !== null ) {
66
+ return $ this ->resolvedRepositoryClass ;
67
+ }
68
+
69
+ $ objectManager = $ this ->getObjectManager ();
70
+ if ($ this ->repositoryClass !== null ) {
71
+ return $ this ->resolvedRepositoryClass = $ this ->repositoryClass ;
72
+ } elseif ($ objectManager !== null && get_class ($ objectManager ) === 'Doctrine\ODM\MongoDB\DocumentManager ' ) {
73
+ return $ this ->resolvedRepositoryClass = 'Doctrine\ODM\MongoDB\DocumentRepository ' ;
74
+ }
75
+
76
+ return $ this ->resolvedRepositoryClass = 'Doctrine\ORM\EntityRepository ' ;
77
+ }
78
+
44
79
public function getRepositoryClass (string $ className ): string
45
80
{
46
- if ($ this ->objectManager === null ) {
47
- return $ this ->repositoryClass ;
81
+ $ objectManager = $ this ->getObjectManager ();
82
+ if ($ objectManager === null ) {
83
+ return $ this ->getResolvedRepositoryClass ();
48
84
}
49
85
50
- $ metadata = $ this -> objectManager ->getClassMetadata ($ className );
86
+ $ metadata = $ objectManager ->getClassMetadata ($ className );
51
87
52
88
$ ormMetadataClass = 'Doctrine\ORM\Mapping\ClassMetadata ' ;
53
89
if ($ metadata instanceof $ ormMetadataClass ) {
54
90
/** @var \Doctrine\ORM\Mapping\ClassMetadata $ormMetadata */
55
91
$ ormMetadata = $ metadata ;
56
- return $ ormMetadata ->customRepositoryClassName ?? $ this ->repositoryClass ;
92
+ return $ ormMetadata ->customRepositoryClassName ?? $ this ->getResolvedRepositoryClass () ;
57
93
}
58
94
59
95
$ odmMetadataClass = 'Doctrine\ODM\MongoDB\Mapping\ClassMetadata ' ;
60
96
if ($ metadata instanceof $ odmMetadataClass ) {
61
97
/** @var \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $odmMetadata */
62
98
$ odmMetadata = $ metadata ;
63
- return $ odmMetadata ->customRepositoryClassName ?? $ this ->repositoryClass ;
99
+ return $ odmMetadata ->customRepositoryClassName ?? $ this ->getResolvedRepositoryClass () ;
64
100
}
65
101
66
- return $ this ->repositoryClass ;
67
- }
68
-
69
- public function getObjectManager (): ?ObjectManager
70
- {
71
- return $ this ->objectManager ;
102
+ return $ this ->getResolvedRepositoryClass ();
72
103
}
73
104
74
105
}
0 commit comments