|
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\Utility; |
|
21
|
|
|
|
|
22
|
|
|
use Doctrine\Common\EventManager; |
|
23
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
|
24
|
|
|
use Doctrine\ODM\MongoDB\Event\DocumentNotFoundEventArgs; |
|
25
|
|
|
use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs; |
|
26
|
|
|
use Doctrine\ODM\MongoDB\Event\PostCollectionLoadEventArgs; |
|
27
|
|
|
use Doctrine\ODM\MongoDB\Event\PreUpdateEventArgs; |
|
28
|
|
|
use Doctrine\ODM\MongoDB\Events; |
|
29
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
|
30
|
|
|
use Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionInterface; |
|
31
|
|
|
use Doctrine\ODM\MongoDB\UnitOfWork; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @internal |
|
35
|
|
|
* @since 1.1 |
|
36
|
|
|
*/ |
|
37
|
|
|
class LifecycleEventManager |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* @var DocumentManager |
|
41
|
|
|
*/ |
|
42
|
|
|
private $dm; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var EventManager |
|
46
|
|
|
*/ |
|
47
|
|
|
private $evm; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var UnitOfWork |
|
51
|
|
|
*/ |
|
52
|
|
|
private $uow; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param DocumentManager $dm |
|
56
|
|
|
* @param UnitOfWork $uow |
|
57
|
|
|
* @param EventManager $evm |
|
58
|
|
|
*/ |
|
59
|
958 |
|
public function __construct(DocumentManager $dm, UnitOfWork $uow, EventManager $evm) |
|
60
|
|
|
{ |
|
61
|
958 |
|
$this->dm = $dm; |
|
62
|
958 |
|
$this->evm = $evm; |
|
63
|
958 |
|
$this->uow = $uow; |
|
64
|
958 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param object $proxy |
|
68
|
|
|
* @param mixed $id |
|
69
|
|
|
* @return bool Returns whether the exceptionDisabled flag was set |
|
70
|
|
|
*/ |
|
71
|
9 |
|
public function documentNotFound($proxy, $id) |
|
72
|
|
|
{ |
|
73
|
9 |
|
$eventArgs = new DocumentNotFoundEventArgs($proxy, $this->dm, $id); |
|
74
|
9 |
|
$this->evm->dispatchEvent(Events::documentNotFound, $eventArgs); |
|
75
|
|
|
|
|
76
|
9 |
|
return $eventArgs->isExceptionDisabled(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
161 |
|
public function postCollectionLoad(PersistentCollectionInterface $coll) |
|
80
|
|
|
{ |
|
81
|
161 |
|
$eventArgs = new PostCollectionLoadEventArgs($coll, $this->dm); |
|
82
|
161 |
|
$this->evm->dispatchEvent(Events::postCollectionLoad, $eventArgs); |
|
83
|
161 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Invokes postPersist callbacks and events for given document cascading them to embedded documents as well. |
|
87
|
|
|
* |
|
88
|
|
|
* @param ClassMetadata $class |
|
89
|
|
|
* @param object $document |
|
90
|
|
|
*/ |
|
91
|
566 |
|
public function postPersist(ClassMetadata $class, $document) |
|
92
|
|
|
{ |
|
93
|
566 |
|
$class->invokeLifecycleCallbacks(Events::postPersist, $document, array(new LifecycleEventArgs($document, $this->dm))); |
|
94
|
566 |
|
$this->evm->dispatchEvent(Events::postPersist, new LifecycleEventArgs($document, $this->dm)); |
|
95
|
566 |
|
$this->cascadePostPersist($class, $document); |
|
96
|
566 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Invokes postRemove callbacks and events for given document. |
|
100
|
|
|
* |
|
101
|
|
|
* @param ClassMetadata $class |
|
102
|
|
|
* @param object $document |
|
103
|
|
|
*/ |
|
104
|
62 |
|
public function postRemove(ClassMetadata $class, $document) |
|
105
|
|
|
{ |
|
106
|
62 |
|
$class->invokeLifecycleCallbacks(Events::postRemove, $document, array(new LifecycleEventArgs($document, $this->dm))); |
|
107
|
62 |
|
$this->evm->dispatchEvent(Events::postRemove, new LifecycleEventArgs($document, $this->dm)); |
|
108
|
62 |
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Invokes postUpdate callbacks and events for given document. The same will be done for embedded documents owned |
|
112
|
|
|
* by given document unless they were new in which case postPersist callbacks and events will be dispatched. |
|
113
|
|
|
* |
|
114
|
|
|
* @param ClassMetadata $class |
|
115
|
|
|
* @param object $document |
|
116
|
|
|
*/ |
|
117
|
218 |
|
public function postUpdate(ClassMetadata $class, $document) |
|
118
|
|
|
{ |
|
119
|
218 |
|
$class->invokeLifecycleCallbacks(Events::postUpdate, $document, array(new LifecycleEventArgs($document, $this->dm))); |
|
120
|
218 |
|
$this->evm->dispatchEvent(Events::postUpdate, new LifecycleEventArgs($document, $this->dm)); |
|
121
|
218 |
|
$this->cascadePostUpdate($class, $document); |
|
122
|
218 |
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Invokes prePersist callbacks and events for given document. |
|
126
|
|
|
* |
|
127
|
|
|
* @param ClassMetadata $class |
|
128
|
|
|
* @param object $document |
|
129
|
|
|
*/ |
|
130
|
596 |
|
public function prePersist(ClassMetadata $class, $document) |
|
131
|
|
|
{ |
|
132
|
596 |
|
$class->invokeLifecycleCallbacks(Events::prePersist, $document, array(new LifecycleEventArgs($document, $this->dm))); |
|
133
|
596 |
|
$this->evm->dispatchEvent(Events::prePersist, new LifecycleEventArgs($document, $this->dm)); |
|
134
|
596 |
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Invokes prePersist callbacks and events for given document. |
|
138
|
|
|
* |
|
139
|
|
|
* @param ClassMetadata $class |
|
140
|
|
|
* @param object $document |
|
141
|
|
|
*/ |
|
142
|
68 |
|
public function preRemove(ClassMetadata $class, $document) |
|
143
|
|
|
{ |
|
144
|
68 |
|
$class->invokeLifecycleCallbacks(Events::preRemove, $document, array(new LifecycleEventArgs($document, $this->dm))); |
|
145
|
68 |
|
$this->evm->dispatchEvent(Events::preRemove, new LifecycleEventArgs($document, $this->dm)); |
|
146
|
68 |
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Invokes preUpdate callbacks and events for given document cascading them to embedded documents as well. |
|
150
|
|
|
* |
|
151
|
|
|
* @param ClassMetadata $class |
|
152
|
|
|
* @param object $document |
|
153
|
|
|
*/ |
|
154
|
222 |
|
public function preUpdate(ClassMetadata $class, $document) |
|
155
|
|
|
{ |
|
156
|
222 |
|
if ( ! empty($class->lifecycleCallbacks[Events::preUpdate])) { |
|
157
|
12 |
|
$class->invokeLifecycleCallbacks(Events::preUpdate, $document, array( |
|
158
|
12 |
|
new PreUpdateEventArgs($document, $this->dm, $this->uow->getDocumentChangeSet($document)) |
|
159
|
12 |
|
)); |
|
160
|
12 |
|
$this->uow->recomputeSingleDocumentChangeSet($class, $document); |
|
161
|
12 |
|
} |
|
162
|
222 |
|
$this->evm->dispatchEvent(Events::preUpdate, new PreUpdateEventArgs($document, $this->dm, $this->uow->getDocumentChangeSet($document))); |
|
163
|
222 |
|
$this->cascadePreUpdate($class, $document); |
|
164
|
222 |
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Cascades the preUpdate event to embedded documents. |
|
168
|
|
|
* |
|
169
|
|
|
* @param ClassMetadata $class |
|
170
|
|
|
* @param object $document |
|
171
|
|
|
*/ |
|
172
|
222 |
|
private function cascadePreUpdate(ClassMetadata $class, $document) |
|
173
|
|
|
{ |
|
174
|
222 |
|
foreach ($class->getEmbeddedFieldsMappings() as $mapping) { |
|
175
|
136 |
|
$value = $class->reflFields[$mapping['fieldName']]->getValue($document); |
|
176
|
136 |
|
if ($value === null) { |
|
177
|
50 |
|
continue; |
|
178
|
|
|
} |
|
179
|
134 |
|
$values = $mapping['type'] === ClassMetadata::ONE ? array($value) : $value; |
|
180
|
|
|
|
|
181
|
134 |
|
foreach ($values as $entry) { |
|
182
|
87 |
|
if ($this->uow->isScheduledForInsert($entry) || empty($this->uow->getDocumentChangeSet($entry))) { |
|
183
|
71 |
|
continue; |
|
184
|
|
|
} |
|
185
|
50 |
|
$this->preUpdate($this->dm->getClassMetadata(get_class($entry)), $entry); |
|
186
|
134 |
|
} |
|
187
|
222 |
|
} |
|
188
|
222 |
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Cascades the postUpdate and postPersist events to embedded documents. |
|
192
|
|
|
* |
|
193
|
|
|
* @param ClassMetadata $class |
|
194
|
|
|
* @param object $document |
|
195
|
|
|
*/ |
|
196
|
218 |
|
private function cascadePostUpdate(ClassMetadata $class, $document) |
|
197
|
|
|
{ |
|
198
|
218 |
|
foreach ($class->getEmbeddedFieldsMappings() as $mapping) { |
|
199
|
132 |
|
$value = $class->reflFields[$mapping['fieldName']]->getValue($document); |
|
200
|
132 |
|
if ($value === null) { |
|
201
|
53 |
|
continue; |
|
202
|
|
|
} |
|
203
|
130 |
|
$values = $mapping['type'] === ClassMetadata::ONE ? array($value) : $value; |
|
204
|
|
|
|
|
205
|
130 |
|
foreach ($values as $entry) { |
|
206
|
87 |
|
if (empty($this->uow->getDocumentChangeSet($entry)) && ! $this->uow->hasScheduledCollections($entry)) { |
|
207
|
47 |
|
continue; |
|
208
|
|
|
} |
|
209
|
73 |
|
$entryClass = $this->dm->getClassMetadata(get_class($entry)); |
|
210
|
73 |
|
$event = $this->uow->isScheduledForInsert($entry) ? Events::postPersist : Events::postUpdate; |
|
211
|
73 |
|
$entryClass->invokeLifecycleCallbacks($event, $entry, array(new LifecycleEventArgs($entry, $this->dm))); |
|
212
|
73 |
|
$this->evm->dispatchEvent($event, new LifecycleEventArgs($entry, $this->dm)); |
|
213
|
|
|
|
|
214
|
73 |
|
$this->cascadePostUpdate($entryClass, $entry); |
|
215
|
130 |
|
} |
|
216
|
218 |
|
} |
|
217
|
218 |
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Cascades the postPersist events to embedded documents. |
|
221
|
|
|
* |
|
222
|
|
|
* @param ClassMetadata $class |
|
223
|
|
|
* @param object $document |
|
224
|
|
|
*/ |
|
225
|
566 |
|
private function cascadePostPersist(ClassMetadata $class, $document) |
|
226
|
|
|
{ |
|
227
|
566 |
|
foreach ($class->getEmbeddedFieldsMappings() as $mapping) { |
|
228
|
348 |
|
$value = $class->reflFields[$mapping['fieldName']]->getValue($document); |
|
229
|
348 |
|
if ($value === null) { |
|
230
|
223 |
|
continue; |
|
231
|
|
|
} |
|
232
|
328 |
|
$values = $mapping['type'] === ClassMetadata::ONE ? array($value) : $value; |
|
233
|
328 |
|
foreach ($values as $embeddedDocument) { |
|
234
|
164 |
|
$this->postPersist($this->dm->getClassMetadata(get_class($embeddedDocument)), $embeddedDocument); |
|
235
|
328 |
|
} |
|
236
|
566 |
|
} |
|
237
|
566 |
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|