1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\ODM\MongoDB\Proxy\Factory; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use Doctrine\Common\EventManager; |
9
|
|
|
use Doctrine\Common\NotifyPropertyChanged; |
10
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
11
|
|
|
use Doctrine\ODM\MongoDB\DocumentNotFoundException; |
12
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
13
|
|
|
use Doctrine\ODM\MongoDB\Persisters\DocumentPersister; |
14
|
|
|
use Doctrine\ODM\MongoDB\UnitOfWork; |
15
|
|
|
use Doctrine\ODM\MongoDB\Utility\LifecycleEventManager; |
16
|
|
|
use ProxyManager\Factory\LazyLoadingGhostFactory; |
17
|
|
|
use ProxyManager\Proxy\GhostObjectInterface; |
18
|
|
|
use ReflectionProperty; |
19
|
|
|
use function array_filter; |
20
|
|
|
use function count; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* This factory is used to create proxy objects for documents at runtime. |
24
|
|
|
*/ |
25
|
|
|
class StaticProxyFactory implements ProxyFactory |
26
|
|
|
{ |
27
|
|
|
/** @var UnitOfWork The UnitOfWork this factory is bound to. */ |
28
|
|
|
private $uow; |
29
|
|
|
|
30
|
|
|
/** @var EventManager */ |
31
|
|
|
private $lifecycleEventManager; |
32
|
|
|
|
33
|
|
|
/** @var LazyLoadingGhostFactory */ |
34
|
|
|
private $proxyFactory; |
35
|
|
|
|
36
|
1634 |
|
public function __construct(DocumentManager $documentManager) |
37
|
|
|
{ |
38
|
1634 |
|
$this->uow = $documentManager->getUnitOfWork(); |
39
|
1634 |
|
$this->lifecycleEventManager = new LifecycleEventManager($documentManager, $this->uow, $documentManager->getEventManager()); |
|
|
|
|
40
|
1634 |
|
$this->proxyFactory = $documentManager->getConfiguration()->buildGhostObjectFactory(); |
41
|
1634 |
|
} |
42
|
|
|
|
43
|
104 |
|
public function getProxy(ClassMetadata $metadata, $identifier) : GhostObjectInterface |
44
|
|
|
{ |
45
|
104 |
|
$documentPersister = $this->uow->getDocumentPersister($metadata->getName()); |
46
|
|
|
|
47
|
|
|
$ghostObject = $this |
48
|
104 |
|
->proxyFactory |
49
|
104 |
|
->createProxy( |
50
|
104 |
|
$metadata->getName(), |
51
|
104 |
|
$this->createInitializer($metadata, $documentPersister), |
52
|
|
|
[ |
53
|
104 |
|
'skippedProperties' => $this->skippedFieldsFqns($metadata), |
54
|
|
|
] |
55
|
|
|
); |
56
|
|
|
|
57
|
104 |
|
$metadata->setIdentifierValue($ghostObject, $identifier); |
|
|
|
|
58
|
|
|
|
59
|
104 |
|
return $ghostObject; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
* |
65
|
|
|
* @param ClassMetadata[] $classes |
66
|
|
|
*/ |
67
|
|
|
public function generateProxyClasses(array $classes) : int |
68
|
|
|
{ |
69
|
|
|
$concreteClasses = array_filter($classes, static function (ClassMetadata $metadata) : bool { |
70
|
|
|
return ! ($metadata->isMappedSuperclass || $metadata->isQueryResultDocument || $metadata->getReflectionClass()->isAbstract()); |
71
|
|
|
}); |
72
|
|
|
|
73
|
|
|
foreach ($concreteClasses as $metadata) { |
74
|
|
|
$this |
75
|
|
|
->proxyFactory |
76
|
|
|
->createProxy( |
77
|
|
|
$metadata->getName(), |
78
|
|
|
static function () { |
79
|
|
|
// empty closure, serves its purpose, for now |
80
|
|
|
}, |
81
|
|
|
$this->skippedFieldsFqns($metadata) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return count($concreteClasses); |
86
|
|
|
} |
87
|
|
|
|
88
|
104 |
|
private function createInitializer( |
89
|
|
|
ClassMetadata $metadata, |
90
|
|
|
DocumentPersister $documentPersister |
91
|
|
|
) : Closure { |
92
|
|
|
return function ( |
93
|
|
|
GhostObjectInterface $ghostObject, |
94
|
|
|
string $method, |
95
|
|
|
// we don't care |
96
|
|
|
array $parameters, |
97
|
|
|
// we don't care |
98
|
|
|
& $initializer, |
99
|
|
|
array $properties // we currently do not use this |
|
|
|
|
100
|
|
|
) use ( |
101
|
36 |
|
$metadata, |
102
|
36 |
|
$documentPersister |
103
|
|
|
) : bool { |
104
|
36 |
|
$originalInitializer = $initializer; |
105
|
36 |
|
$initializer = null; |
106
|
36 |
|
$identifier = $metadata->getIdentifierValue($ghostObject); |
|
|
|
|
107
|
|
|
|
108
|
36 |
|
if (! $documentPersister->load(['_id' => $identifier], $ghostObject)) { |
109
|
9 |
|
$initializer = $originalInitializer; |
110
|
|
|
|
111
|
9 |
|
if (! $this->lifecycleEventManager->documentNotFound($ghostObject, $identifier)) { |
|
|
|
|
112
|
8 |
|
throw DocumentNotFoundException::documentNotFound($metadata->getName(), $identifier); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
30 |
|
if ($ghostObject instanceof NotifyPropertyChanged) { |
117
|
1 |
|
$ghostObject->addPropertyChangedListener($this->uow); |
118
|
|
|
} |
119
|
|
|
|
120
|
30 |
|
return true; |
121
|
104 |
|
}; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return string[] |
126
|
|
|
*/ |
127
|
104 |
|
private function skippedFieldsFqns(ClassMetadata $metadata) : array |
128
|
|
|
{ |
129
|
104 |
|
$idFieldFqcns = []; |
130
|
|
|
|
131
|
104 |
|
foreach ($metadata->getIdentifierFieldNames() as $idField) { |
132
|
104 |
|
$idFieldFqcns[] = $this->propertyFqcn($metadata->getReflectionProperty($idField)); |
133
|
|
|
} |
134
|
|
|
|
135
|
104 |
|
return $idFieldFqcns; |
136
|
|
|
} |
137
|
|
|
|
138
|
104 |
|
private function propertyFqcn(ReflectionProperty $property) : string |
139
|
|
|
{ |
140
|
104 |
|
if ($property->isPrivate()) { |
141
|
50 |
|
return "\0" . $property->getDeclaringClass()->getName() . "\0" . $property->getName(); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
60 |
|
if ($property->isProtected()) { |
145
|
22 |
|
return "\0*\0" . $property->getName(); |
146
|
|
|
} |
147
|
|
|
|
148
|
38 |
|
return $property->getName(); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..