These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 = array(); |
|
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) { |
|
0 ignored issues
–
show
|
|||
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 | foreach ($metadata->fieldMappings as $fieldMapping) { |
|
86 | 1 | $lines[] = '$metadata->mapField(' . $this->_varExport($fieldMapping) . ');'; |
|
87 | } |
||
88 | |||
89 | 1 | if ( ! $metadata->isIdentifierComposite && $generatorType = $this->_getIdGeneratorTypeString($metadata->generatorType)) { |
|
90 | 1 | $lines[] = '$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_' . $generatorType . ');'; |
|
91 | } |
||
92 | |||
93 | 1 | foreach ($metadata->associationMappings as $associationMapping) { |
|
94 | 1 | $cascade = array('remove', 'persist', 'refresh', 'merge', 'detach'); |
|
95 | 1 | foreach ($cascade as $key => $value) { |
|
96 | 1 | if ( ! $associationMapping['isCascade'.ucfirst($value)]) { |
|
97 | 1 | unset($cascade[$key]); |
|
98 | } |
||
99 | } |
||
100 | |||
101 | 1 | if (count($cascade) === 5) { |
|
102 | 1 | $cascade = array('all'); |
|
103 | } |
||
104 | |||
105 | $associationMappingArray = array( |
||
106 | 1 | 'fieldName' => $associationMapping['fieldName'], |
|
107 | 1 | 'targetEntity' => $associationMapping['targetEntity'], |
|
108 | 1 | 'cascade' => $cascade, |
|
109 | ); |
||
110 | |||
111 | 1 | if (isset($associationMapping['fetch'])) { |
|
112 | 1 | $associationMappingArray['fetch'] = $associationMapping['fetch']; |
|
113 | } |
||
114 | |||
115 | 1 | if ($associationMapping['type'] & ClassMetadataInfo::TO_ONE) { |
|
116 | 1 | $method = 'mapOneToOne'; |
|
117 | $oneToOneMappingArray = array( |
||
118 | 1 | 'mappedBy' => $associationMapping['mappedBy'], |
|
119 | 1 | 'inversedBy' => $associationMapping['inversedBy'], |
|
120 | 1 | 'joinColumns' => $associationMapping['isOwningSide'] ? $associationMapping['joinColumns'] : [], |
|
121 | 1 | 'orphanRemoval' => $associationMapping['orphanRemoval'], |
|
122 | ); |
||
123 | |||
124 | 1 | $associationMappingArray = array_merge($associationMappingArray, $oneToOneMappingArray); |
|
125 | 1 | } elseif ($associationMapping['type'] == ClassMetadataInfo::ONE_TO_MANY) { |
|
126 | 1 | $method = 'mapOneToMany'; |
|
127 | $potentialAssociationMappingIndexes = array( |
||
128 | 1 | 'mappedBy', |
|
129 | 'orphanRemoval', |
||
130 | 'orderBy', |
||
131 | ); |
||
132 | 1 | foreach ($potentialAssociationMappingIndexes as $index) { |
|
133 | 1 | if (isset($associationMapping[$index])) { |
|
134 | 1 | $oneToManyMappingArray[$index] = $associationMapping[$index]; |
|
135 | } |
||
136 | } |
||
137 | 1 | $associationMappingArray = array_merge($associationMappingArray, $oneToManyMappingArray); |
|
138 | 1 | } elseif ($associationMapping['type'] == ClassMetadataInfo::MANY_TO_MANY) { |
|
139 | 1 | $method = 'mapManyToMany'; |
|
140 | $potentialAssociationMappingIndexes = array( |
||
141 | 1 | 'mappedBy', |
|
142 | 'joinTable', |
||
143 | 'orderBy', |
||
144 | ); |
||
145 | 1 | foreach ($potentialAssociationMappingIndexes as $index) { |
|
146 | 1 | if (isset($associationMapping[$index])) { |
|
147 | 1 | $manyToManyMappingArray[$index] = $associationMapping[$index]; |
|
148 | } |
||
149 | } |
||
150 | 1 | $associationMappingArray = array_merge($associationMappingArray, $manyToManyMappingArray); |
|
151 | } |
||
152 | |||
153 | 1 | $lines[] = '$metadata->' . $method . '(' . $this->_varExport($associationMappingArray) . ');'; |
|
154 | } |
||
155 | |||
156 | 1 | return implode("\n", $lines); |
|
157 | } |
||
158 | |||
159 | /** |
||
160 | * @param mixed $var |
||
161 | * |
||
162 | * @return string |
||
163 | */ |
||
164 | 1 | protected function _varExport($var) |
|
165 | { |
||
166 | 1 | $export = var_export($var, true); |
|
167 | 1 | $export = str_replace("\n", PHP_EOL . str_repeat(' ', 8), $export); |
|
168 | 1 | $export = str_replace(' ', ' ', $export); |
|
169 | 1 | $export = str_replace('array (', 'array(', $export); |
|
170 | 1 | $export = str_replace('array( ', 'array(', $export); |
|
171 | 1 | $export = str_replace(',)', ')', $export); |
|
172 | 1 | $export = str_replace(', )', ')', $export); |
|
173 | 1 | $export = str_replace(' ', ' ', $export); |
|
174 | |||
175 | 1 | return $export; |
|
176 | } |
||
177 | } |
||
178 |
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.