|
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\ORM\Tools\Export\Driver; |
|
21
|
|
|
|
|
22
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* ClassMetadata exporter for PHP code. |
|
26
|
|
|
* |
|
27
|
|
|
* @link www.doctrine-project.org |
|
28
|
|
|
* @since 2.0 |
|
29
|
|
|
* @author Jonathan Wage <[email protected]> |
|
30
|
|
|
*/ |
|
31
|
|
|
class PhpExporter extends AbstractExporter |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $_extension = '.php'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
1 |
|
public function exportClassMetadata(ClassMetadataInfo $metadata) |
|
42
|
|
|
{ |
|
43
|
1 |
|
$lines = []; |
|
44
|
1 |
|
$lines[] = '<?php'; |
|
45
|
1 |
|
$lines[] = null; |
|
46
|
1 |
|
$lines[] = 'use Doctrine\ORM\Mapping\ClassMetadataInfo;'; |
|
47
|
1 |
|
$lines[] = null; |
|
48
|
|
|
|
|
49
|
1 |
|
if ($metadata->isMappedSuperclass) { |
|
50
|
|
|
$lines[] = '$metadata->isMappedSuperclass = true;'; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
if ($metadata->inheritanceType) { |
|
54
|
1 |
|
$lines[] = '$metadata->setInheritanceType(ClassMetadataInfo::INHERITANCE_TYPE_' . $this->_getInheritanceTypeString($metadata->inheritanceType) . ');'; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
if ($metadata->customRepositoryClassName) { |
|
58
|
|
|
$lines[] = "\$metadata->customRepositoryClassName = '" . $metadata->customRepositoryClassName . "';"; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
if ($metadata->table) { |
|
|
|
|
|
|
62
|
1 |
|
$lines[] = '$metadata->setPrimaryTable(' . $this->_varExport($metadata->table) . ');'; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
if ($metadata->discriminatorColumn) { |
|
|
|
|
|
|
66
|
|
|
$lines[] = '$metadata->setDiscriminatorColumn(' . $this->_varExport($metadata->discriminatorColumn) . ');'; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
if ($metadata->discriminatorMap) { |
|
70
|
|
|
$lines[] = '$metadata->setDiscriminatorMap(' . $this->_varExport($metadata->discriminatorMap) . ');'; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
if ($metadata->changeTrackingPolicy) { |
|
74
|
1 |
|
$lines[] = '$metadata->setChangeTrackingPolicy(ClassMetadataInfo::CHANGETRACKING_' . $this->_getChangeTrackingPolicyString($metadata->changeTrackingPolicy) . ');'; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
if ($metadata->lifecycleCallbacks) { |
|
|
|
|
|
|
78
|
1 |
|
foreach ($metadata->lifecycleCallbacks as $event => $callbacks) { |
|
79
|
1 |
|
foreach ($callbacks as $callback) { |
|
80
|
1 |
|
$lines[] = "\$metadata->addLifecycleCallback('$callback', '$event');"; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
$lines = array_merge($lines, $this->processEntityListeners($metadata)); |
|
86
|
|
|
|
|
87
|
1 |
|
foreach ($metadata->fieldMappings as $fieldMapping) { |
|
88
|
1 |
|
$lines[] = '$metadata->mapField(' . $this->_varExport($fieldMapping) . ');'; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
if ( ! $metadata->isIdentifierComposite && $generatorType = $this->_getIdGeneratorTypeString($metadata->generatorType)) { |
|
92
|
1 |
|
$lines[] = '$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_' . $generatorType . ');'; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
foreach ($metadata->associationMappings as $associationMapping) { |
|
96
|
1 |
|
$cascade = ['remove', 'persist', 'refresh', 'merge', 'detach']; |
|
97
|
1 |
|
foreach ($cascade as $key => $value) { |
|
98
|
1 |
|
if ( ! $associationMapping['isCascade'.ucfirst($value)]) { |
|
99
|
1 |
|
unset($cascade[$key]); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
1 |
|
if (count($cascade) === 5) { |
|
104
|
1 |
|
$cascade = ['all']; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
1 |
|
$method = null; |
|
108
|
|
|
$associationMappingArray = [ |
|
109
|
1 |
|
'fieldName' => $associationMapping['fieldName'], |
|
110
|
1 |
|
'targetEntity' => $associationMapping['targetEntity'], |
|
111
|
1 |
|
'cascade' => $cascade, |
|
112
|
|
|
]; |
|
113
|
|
|
|
|
114
|
1 |
|
if (isset($associationMapping['fetch'])) { |
|
115
|
1 |
|
$associationMappingArray['fetch'] = $associationMapping['fetch']; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
1 |
|
if ($associationMapping['type'] & ClassMetadataInfo::TO_ONE) { |
|
119
|
1 |
|
$method = 'mapOneToOne'; |
|
120
|
|
|
$oneToOneMappingArray = [ |
|
121
|
1 |
|
'mappedBy' => $associationMapping['mappedBy'], |
|
122
|
1 |
|
'inversedBy' => $associationMapping['inversedBy'], |
|
123
|
1 |
|
'joinColumns' => $associationMapping['isOwningSide'] ? $associationMapping['joinColumns'] : [], |
|
124
|
1 |
|
'orphanRemoval' => $associationMapping['orphanRemoval'], |
|
125
|
|
|
]; |
|
126
|
|
|
|
|
127
|
1 |
|
$associationMappingArray = array_merge($associationMappingArray, $oneToOneMappingArray); |
|
128
|
1 |
|
} elseif ($associationMapping['type'] == ClassMetadataInfo::ONE_TO_MANY) { |
|
129
|
1 |
|
$method = 'mapOneToMany'; |
|
130
|
|
|
$potentialAssociationMappingIndexes = [ |
|
131
|
1 |
|
'mappedBy', |
|
132
|
|
|
'orphanRemoval', |
|
133
|
|
|
'orderBy', |
|
134
|
|
|
]; |
|
135
|
1 |
|
$oneToManyMappingArray = []; |
|
136
|
1 |
|
foreach ($potentialAssociationMappingIndexes as $index) { |
|
137
|
1 |
|
if (isset($associationMapping[$index])) { |
|
138
|
1 |
|
$oneToManyMappingArray[$index] = $associationMapping[$index]; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
1 |
|
$associationMappingArray = array_merge($associationMappingArray, $oneToManyMappingArray); |
|
142
|
1 |
|
} elseif ($associationMapping['type'] == ClassMetadataInfo::MANY_TO_MANY) { |
|
143
|
1 |
|
$method = 'mapManyToMany'; |
|
144
|
|
|
$potentialAssociationMappingIndexes = [ |
|
145
|
1 |
|
'mappedBy', |
|
146
|
|
|
'joinTable', |
|
147
|
|
|
'orderBy', |
|
148
|
|
|
]; |
|
149
|
1 |
|
$manyToManyMappingArray = []; |
|
150
|
1 |
|
foreach ($potentialAssociationMappingIndexes as $index) { |
|
151
|
1 |
|
if (isset($associationMapping[$index])) { |
|
152
|
1 |
|
$manyToManyMappingArray[$index] = $associationMapping[$index]; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
1 |
|
$associationMappingArray = array_merge($associationMappingArray, $manyToManyMappingArray); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
1 |
|
$lines[] = '$metadata->' . $method . '(' . $this->_varExport($associationMappingArray) . ');'; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
1 |
|
return implode("\n", $lines); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param mixed $var |
|
166
|
|
|
* |
|
167
|
|
|
* @return string |
|
168
|
|
|
*/ |
|
169
|
1 |
|
protected function _varExport($var) |
|
170
|
|
|
{ |
|
171
|
1 |
|
$export = var_export($var, true); |
|
172
|
1 |
|
$export = str_replace("\n", PHP_EOL . str_repeat(' ', 8), $export); |
|
173
|
1 |
|
$export = str_replace(' ', ' ', $export); |
|
174
|
1 |
|
$export = str_replace('array (', 'array(', $export); |
|
175
|
1 |
|
$export = str_replace('array( ', 'array(', $export); |
|
176
|
1 |
|
$export = str_replace(',)', ')', $export); |
|
177
|
1 |
|
$export = str_replace(', )', ')', $export); |
|
178
|
1 |
|
$export = str_replace(' ', ' ', $export); |
|
179
|
|
|
|
|
180
|
1 |
|
return $export; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
1 |
|
private function processEntityListeners(ClassMetadataInfo $metadata) : array |
|
184
|
|
|
{ |
|
185
|
1 |
|
$lines = []; |
|
186
|
|
|
|
|
187
|
1 |
|
foreach ($metadata->entityListeners as $event => $entityListenerConfig) { |
|
188
|
1 |
|
foreach ($entityListenerConfig as $entityListener) { |
|
189
|
1 |
|
$lines[] = \sprintf( |
|
190
|
1 |
|
'$metadata->addEntityListener(%s, %s, %s);', |
|
191
|
1 |
|
\var_export($event, true), |
|
192
|
1 |
|
\var_export($entityListener['class'], true), |
|
193
|
1 |
|
\var_export($entityListener['method'], true) |
|
194
|
|
|
); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
1 |
|
return $lines; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
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.