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