1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\ODM\MongoDB\Proxy; |
21
|
|
|
|
22
|
|
|
use Doctrine\Common\NotifyPropertyChanged; |
23
|
|
|
use Doctrine\Common\Proxy\AbstractProxyFactory; |
24
|
|
|
use Doctrine\Common\Proxy\ProxyDefinition; |
25
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
26
|
|
|
use Doctrine\ODM\MongoDB\DocumentNotFoundException; |
27
|
|
|
use Doctrine\Common\Proxy\ProxyGenerator; |
28
|
|
|
use Doctrine\Common\Util\ClassUtils; |
29
|
|
|
use Doctrine\Common\Proxy\Proxy as BaseProxy; |
30
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata as BaseClassMetadata; |
31
|
|
|
use Doctrine\ODM\MongoDB\Persisters\DocumentPersister; |
32
|
|
|
use Doctrine\ODM\MongoDB\Utility\LifecycleEventManager; |
33
|
|
|
use ReflectionProperty; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* This factory is used to create proxy objects for documents at runtime. |
37
|
|
|
* |
38
|
|
|
* @since 1.0 |
39
|
|
|
*/ |
40
|
|
|
class ProxyFactory extends AbstractProxyFactory |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* @var \Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory |
44
|
|
|
*/ |
45
|
|
|
private $metadataFactory; |
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var \Doctrine\ODM\MongoDB\UnitOfWork The UnitOfWork this factory is bound to. |
49
|
|
|
*/ |
50
|
|
|
private $uow; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string The namespace that contains all proxy classes. |
54
|
|
|
*/ |
55
|
|
|
private $proxyNamespace; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var \Doctrine\Common\EventManager |
59
|
|
|
*/ |
60
|
|
|
private $lifecycleEventManager; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Initializes a new instance of the <tt>ProxyFactory</tt> class that is |
64
|
|
|
* connected to the given <tt>DocumentManager</tt>. |
65
|
|
|
* |
66
|
|
|
* @param \Doctrine\ODM\MongoDB\DocumentManager $documentManager The DocumentManager the new factory works for. |
67
|
|
|
* @param string $proxyDir The directory to use for the proxy classes. It |
68
|
|
|
* must exist. |
69
|
|
|
* @param string $proxyNamespace The namespace to use for the proxy classes. |
70
|
|
|
* @param integer $autoGenerate Whether to automatically generate proxy classes. |
71
|
|
|
*/ |
72
|
1038 |
|
public function __construct(DocumentManager $documentManager, $proxyDir, $proxyNamespace, $autoGenerate = AbstractProxyFactory::AUTOGENERATE_NEVER) |
73
|
|
|
{ |
74
|
1038 |
|
$this->metadataFactory = $documentManager->getMetadataFactory(); |
75
|
1038 |
|
$this->uow = $documentManager->getUnitOfWork(); |
76
|
1038 |
|
$this->proxyNamespace = $proxyNamespace; |
77
|
1038 |
|
$this->lifecycleEventManager = new LifecycleEventManager($documentManager, $this->uow, $documentManager->getEventManager()); |
|
|
|
|
78
|
1038 |
|
$proxyGenerator = new ProxyGenerator($proxyDir, $proxyNamespace); |
79
|
|
|
|
80
|
1038 |
|
$proxyGenerator->setPlaceholder('baseProxyInterface', Proxy::class); |
81
|
|
|
|
82
|
1038 |
|
parent::__construct($proxyGenerator, $this->metadataFactory, $autoGenerate); |
83
|
1038 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritDoc} |
87
|
|
|
*/ |
88
|
|
|
public function skipClass(BaseClassMetadata $class) |
89
|
|
|
{ |
90
|
|
|
/* @var $class \Doctrine\ODM\Mongodb\Mapping\ClassMetadataInfo */ |
91
|
|
|
return $class->isMappedSuperclass || $class->getReflectionClass()->isAbstract(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritDoc} |
96
|
|
|
*/ |
97
|
93 |
|
public function createProxyDefinition($className) |
98
|
|
|
{ |
99
|
|
|
/* @var $classMetadata \Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo */ |
100
|
93 |
|
$classMetadata = $this->metadataFactory->getMetadataFor($className); |
101
|
93 |
|
$documentPersister = $this->uow->getDocumentPersister($className); |
102
|
93 |
|
$reflectionId = $classMetadata->reflFields[$classMetadata->identifier]; |
103
|
|
|
|
104
|
93 |
|
return new ProxyDefinition( |
105
|
93 |
|
ClassUtils::generateProxyClassName($className, $this->proxyNamespace), |
106
|
93 |
|
$classMetadata->getIdentifierFieldNames(), |
107
|
93 |
|
$classMetadata->getReflectionProperties(), |
108
|
93 |
|
$this->createInitializer($classMetadata, $documentPersister, $reflectionId), |
109
|
93 |
|
$this->createCloner($classMetadata, $documentPersister, $reflectionId) |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Generates a closure capable of initializing a proxy |
115
|
|
|
* |
116
|
|
|
* @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $classMetadata |
117
|
|
|
* @param \Doctrine\ODM\MongoDB\Persisters\DocumentPersister $documentPersister |
118
|
|
|
* @param \ReflectionProperty $reflectionId |
119
|
|
|
* |
120
|
|
|
* @return \Closure |
121
|
|
|
* |
122
|
|
|
* @throws \Doctrine\ODM\MongoDB\DocumentNotFoundException |
123
|
|
|
*/ |
124
|
93 |
|
private function createInitializer( |
125
|
|
|
BaseClassMetadata $classMetadata, |
126
|
|
|
DocumentPersister $documentPersister, |
127
|
|
|
ReflectionProperty $reflectionId |
128
|
|
|
) { |
129
|
93 |
|
if ($classMetadata->getReflectionClass()->hasMethod('__wakeup')) { |
130
|
|
View Code Duplication |
return function (BaseProxy $proxy) use ($documentPersister, $reflectionId) { |
|
|
|
|
131
|
|
|
$proxy->__setInitializer(null); |
132
|
|
|
$proxy->__setCloner(null); |
133
|
|
|
|
134
|
|
|
if ($proxy->__isInitialized()) { |
135
|
|
|
return; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$properties = $proxy->__getLazyProperties(); |
139
|
|
|
|
140
|
|
|
foreach ($properties as $propertyName => $property) { |
141
|
|
|
if ( ! isset($proxy->$propertyName)) { |
142
|
|
|
$proxy->$propertyName = $properties[$propertyName]; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$proxy->__setInitialized(true); |
147
|
|
|
$proxy->__wakeup(); |
|
|
|
|
148
|
|
|
|
149
|
|
|
$id = $reflectionId->getValue($proxy); |
150
|
|
|
|
151
|
|
|
if (null === $documentPersister->load(array('_id' => $id), $proxy)) { |
152
|
|
|
if ( ! $this->lifecycleEventManager->documentNotFound($proxy, $id)) { |
|
|
|
|
153
|
|
|
throw DocumentNotFoundException::documentNotFound(get_class($proxy), $id); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if ($proxy instanceof NotifyPropertyChanged) { |
158
|
|
|
$proxy->addPropertyChangedListener($this->uow); |
159
|
|
|
} |
160
|
|
|
}; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
View Code Duplication |
return function (BaseProxy $proxy) use ($documentPersister, $reflectionId) { |
|
|
|
|
164
|
42 |
|
$proxy->__setInitializer(null); |
165
|
42 |
|
$proxy->__setCloner(null); |
166
|
|
|
|
167
|
42 |
|
if ($proxy->__isInitialized()) { |
168
|
3 |
|
return; |
169
|
|
|
} |
170
|
|
|
|
171
|
42 |
|
$properties = $proxy->__getLazyProperties(); |
172
|
|
|
|
173
|
42 |
|
foreach ($properties as $propertyName => $property) { |
174
|
22 |
|
if ( ! isset($proxy->$propertyName)) { |
175
|
22 |
|
$proxy->$propertyName = $properties[$propertyName]; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
42 |
|
$proxy->__setInitialized(true); |
180
|
|
|
|
181
|
42 |
|
$id = $reflectionId->getValue($proxy); |
182
|
|
|
|
183
|
42 |
|
if (null === $documentPersister->load(array('_id' => $id), $proxy)) { |
184
|
9 |
|
if ( ! $this->lifecycleEventManager->documentNotFound($proxy, $id)) { |
|
|
|
|
185
|
8 |
|
throw DocumentNotFoundException::documentNotFound(get_class($proxy), $id); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
38 |
|
if ($proxy instanceof NotifyPropertyChanged) { |
190
|
1 |
|
$proxy->addPropertyChangedListener($this->uow); |
191
|
|
|
} |
192
|
93 |
|
}; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Generates a closure capable of finalizing a cloned proxy |
197
|
|
|
* |
198
|
|
|
* @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $classMetadata |
199
|
|
|
* @param \Doctrine\ODM\MongoDB\Persisters\DocumentPersister $documentPersister |
200
|
|
|
* @param \ReflectionProperty $reflectionId |
201
|
|
|
* |
202
|
|
|
* @return \Closure |
203
|
|
|
* |
204
|
|
|
* @throws \Doctrine\ODM\MongoDB\DocumentNotFoundException |
205
|
|
|
*/ |
206
|
|
|
private function createCloner( |
207
|
|
|
BaseClassMetadata $classMetadata, |
208
|
|
|
DocumentPersister $documentPersister, |
209
|
|
|
ReflectionProperty $reflectionId |
210
|
|
|
) { |
211
|
93 |
|
return function (BaseProxy $proxy) use ($documentPersister, $classMetadata, $reflectionId) { |
212
|
|
|
if ($proxy->__isInitialized()) { |
213
|
|
|
return; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
$proxy->__setInitialized(true); |
217
|
|
|
$proxy->__setInitializer(null); |
218
|
|
|
|
219
|
|
|
$id = $reflectionId->getValue($proxy); |
220
|
|
|
$original = $documentPersister->load(array('_id' => $id)); |
221
|
|
|
|
222
|
|
|
if (null === $original) { |
223
|
|
|
if ( ! $this->lifecycleEventManager->documentNotFound($proxy, $id)) { |
|
|
|
|
224
|
|
|
throw DocumentNotFoundException::documentNotFound(get_class($proxy), $id); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
foreach ($classMetadata->getReflectionClass()->getProperties() as $reflectionProperty) { |
229
|
|
|
$propertyName = $reflectionProperty->getName(); |
230
|
|
|
|
231
|
|
|
if ($classMetadata->hasField($propertyName) || $classMetadata->hasAssociation($propertyName)) { |
232
|
|
|
$reflectionProperty->setAccessible(true); |
233
|
|
|
$reflectionProperty->setValue($proxy, $reflectionProperty->getValue($original)); |
234
|
|
|
} |
235
|
|
|
} |
236
|
93 |
|
}; |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|