|
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\Mapping; |
|
21
|
|
|
|
|
22
|
|
|
use Doctrine\Instantiator\Instantiator; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* A <tt>ClassMetadata</tt> instance holds all the object-document mapping metadata |
|
26
|
|
|
* of a document and it's references. |
|
27
|
|
|
* |
|
28
|
|
|
* Once populated, ClassMetadata instances are usually cached in a serialized form. |
|
29
|
|
|
* |
|
30
|
|
|
* <b>IMPORTANT NOTE:</b> |
|
31
|
|
|
* |
|
32
|
|
|
* The fields of this class are only public for 2 reasons: |
|
33
|
|
|
* 1) To allow fast READ access. |
|
34
|
|
|
* 2) To drastically reduce the size of a serialized instance (private/protected members |
|
35
|
|
|
* get the whole class name, namespace inclusive, prepended to every property in |
|
36
|
|
|
* the serialized representation). |
|
37
|
|
|
* |
|
38
|
|
|
* @since 1.0 |
|
39
|
|
|
*/ |
|
40
|
|
|
class ClassMetadata extends ClassMetadataInfo |
|
41
|
|
|
{ |
|
42
|
|
|
/** |
|
43
|
|
|
* The ReflectionProperty instances of the mapped class. |
|
44
|
|
|
* |
|
45
|
|
|
* @var \ReflectionProperty[] |
|
46
|
|
|
*/ |
|
47
|
|
|
public $reflFields = array(); |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var \Doctrine\Instantiator\InstantiatorInterface|null |
|
51
|
|
|
*/ |
|
52
|
|
|
private $instantiator; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Initializes a new ClassMetadata instance that will hold the object-document mapping |
|
56
|
|
|
* metadata of the class with the given name. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $documentName The name of the document class the new instance is used for. |
|
59
|
|
|
*/ |
|
60
|
963 |
|
public function __construct($documentName) |
|
61
|
|
|
{ |
|
62
|
963 |
|
parent::__construct($documentName); |
|
63
|
963 |
|
$this->reflClass = new \ReflectionClass($documentName); |
|
64
|
963 |
|
$this->namespace = $this->reflClass->getNamespaceName(); |
|
65
|
963 |
|
$this->setCollection($this->reflClass->getShortName()); |
|
66
|
963 |
|
$this->instantiator = new Instantiator(); |
|
67
|
963 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Map a field. |
|
71
|
|
|
* |
|
72
|
|
|
* @param array $mapping The mapping information. |
|
73
|
|
|
* @return void |
|
74
|
|
|
*/ |
|
75
|
953 |
|
public function mapField(array $mapping) |
|
76
|
|
|
{ |
|
77
|
953 |
|
$mapping = parent::mapField($mapping); |
|
78
|
|
|
|
|
79
|
952 |
|
$reflProp = $this->reflClass->getProperty($mapping['fieldName']); |
|
80
|
951 |
|
$reflProp->setAccessible(true); |
|
81
|
951 |
|
$this->reflFields[$mapping['fieldName']] = $reflProp; |
|
82
|
951 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Determines which fields get serialized. |
|
86
|
|
|
* |
|
87
|
|
|
* It is only serialized what is necessary for best unserialization performance. |
|
88
|
|
|
* That means any metadata properties that are not set or empty or simply have |
|
89
|
|
|
* their default value are NOT serialized. |
|
90
|
|
|
* |
|
91
|
|
|
* Parts that are also NOT serialized because they can not be properly unserialized: |
|
92
|
|
|
* - reflClass (ReflectionClass) |
|
93
|
|
|
* - reflFields (ReflectionProperty array) |
|
94
|
|
|
* |
|
95
|
|
|
* @return array The names of all the fields that should be serialized. |
|
96
|
|
|
*/ |
|
97
|
6 |
|
public function __sleep() |
|
98
|
|
|
{ |
|
99
|
|
|
// This metadata is always serialized/cached. |
|
100
|
|
|
$serialized = array( |
|
101
|
6 |
|
'fieldMappings', |
|
102
|
6 |
|
'associationMappings', |
|
103
|
6 |
|
'identifier', |
|
104
|
6 |
|
'name', |
|
105
|
6 |
|
'namespace', // TODO: REMOVE |
|
106
|
6 |
|
'db', |
|
107
|
6 |
|
'collection', |
|
108
|
6 |
|
'writeConcern', |
|
109
|
6 |
|
'rootDocumentName', |
|
110
|
6 |
|
'generatorType', |
|
111
|
6 |
|
'generatorOptions', |
|
112
|
6 |
|
'idGenerator', |
|
113
|
6 |
|
'indexes', |
|
114
|
6 |
|
'shardKey', |
|
115
|
6 |
|
); |
|
116
|
|
|
|
|
117
|
|
|
// The rest of the metadata is only serialized if necessary. |
|
118
|
6 |
|
if ($this->changeTrackingPolicy != self::CHANGETRACKING_DEFERRED_IMPLICIT) { |
|
119
|
|
|
$serialized[] = 'changeTrackingPolicy'; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
6 |
|
if ($this->customRepositoryClassName) { |
|
123
|
1 |
|
$serialized[] = 'customRepositoryClassName'; |
|
124
|
1 |
|
} |
|
125
|
|
|
|
|
126
|
6 |
|
if ($this->inheritanceType != self::INHERITANCE_TYPE_NONE || $this->discriminatorField !== null) { |
|
127
|
4 |
|
$serialized[] = 'inheritanceType'; |
|
128
|
4 |
|
$serialized[] = 'discriminatorField'; |
|
129
|
4 |
|
$serialized[] = 'discriminatorValue'; |
|
130
|
4 |
|
$serialized[] = 'discriminatorMap'; |
|
131
|
4 |
|
$serialized[] = 'defaultDiscriminatorValue'; |
|
132
|
4 |
|
$serialized[] = 'parentClasses'; |
|
133
|
4 |
|
$serialized[] = 'subClasses'; |
|
134
|
4 |
|
} |
|
135
|
|
|
|
|
136
|
6 |
|
if ($this->isMappedSuperclass) { |
|
137
|
1 |
|
$serialized[] = 'isMappedSuperclass'; |
|
138
|
1 |
|
} |
|
139
|
|
|
|
|
140
|
6 |
|
if ($this->isEmbeddedDocument) { |
|
141
|
1 |
|
$serialized[] = 'isEmbeddedDocument'; |
|
142
|
1 |
|
} |
|
143
|
|
|
|
|
144
|
6 |
|
if ($this->isQueryResultDocument) { |
|
145
|
|
|
$serialized[] = 'isQueryResultDocument'; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
6 |
|
if ($this->isVersioned) { |
|
149
|
|
|
$serialized[] = 'isVersioned'; |
|
150
|
|
|
$serialized[] = 'versionField'; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
6 |
|
if ($this->lifecycleCallbacks) { |
|
|
|
|
|
|
154
|
|
|
$serialized[] = 'lifecycleCallbacks'; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
6 |
|
if ($this->file) { |
|
158
|
1 |
|
$serialized[] = 'file'; |
|
159
|
1 |
|
} |
|
160
|
|
|
|
|
161
|
6 |
|
if ($this->slaveOkay) { |
|
|
|
|
|
|
162
|
1 |
|
$serialized[] = 'slaveOkay'; |
|
163
|
1 |
|
} |
|
164
|
|
|
|
|
165
|
6 |
|
if ($this->distance) { |
|
166
|
1 |
|
$serialized[] = 'distance'; |
|
167
|
1 |
|
} |
|
168
|
|
|
|
|
169
|
6 |
|
if ($this->collectionCapped) { |
|
170
|
1 |
|
$serialized[] = 'collectionCapped'; |
|
171
|
1 |
|
$serialized[] = 'collectionSize'; |
|
172
|
1 |
|
$serialized[] = 'collectionMax'; |
|
173
|
1 |
|
} |
|
174
|
|
|
|
|
175
|
6 |
|
if ($this->isReadOnly) { |
|
176
|
|
|
$serialized[] = 'isReadOnly'; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
6 |
|
return $serialized; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Restores some state that can not be serialized/unserialized. |
|
184
|
|
|
* |
|
185
|
|
|
* @return void |
|
186
|
|
|
*/ |
|
187
|
6 |
|
public function __wakeup() |
|
188
|
|
|
{ |
|
189
|
|
|
// Restore ReflectionClass and properties |
|
190
|
6 |
|
$this->reflClass = new \ReflectionClass($this->name); |
|
191
|
6 |
|
$this->instantiator = $this->instantiator ?: new Instantiator(); |
|
192
|
|
|
|
|
193
|
6 |
|
foreach ($this->fieldMappings as $field => $mapping) { |
|
194
|
3 |
|
if (isset($mapping['declared'])) { |
|
195
|
1 |
|
$reflField = new \ReflectionProperty($mapping['declared'], $field); |
|
196
|
1 |
|
} else { |
|
197
|
3 |
|
$reflField = $this->reflClass->getProperty($field); |
|
198
|
|
|
} |
|
199
|
3 |
|
$reflField->setAccessible(true); |
|
200
|
3 |
|
$this->reflFields[$field] = $reflField; |
|
201
|
6 |
|
} |
|
202
|
6 |
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Creates a new instance of the mapped class, without invoking the constructor. |
|
206
|
|
|
* |
|
207
|
|
|
* @return object |
|
208
|
|
|
*/ |
|
209
|
400 |
|
public function newInstance() |
|
210
|
|
|
{ |
|
211
|
400 |
|
return $this->instantiator->instantiate($this->name); |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.