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.
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>.
20 namespace Doctrine\ORM\Tools\Export\Driver;
22 use Doctrine\ORM\Mapping\ClassMetadataInfo;
25 * ClassMetadata exporter for PHP code
28 * @link www.doctrine-project.org
30 * @author Jonathan Wage <jonwage@gmail.com>
32 class PhpExporter extends AbstractExporter
34 protected $_extension = '.php';
37 * Converts a single ClassMetadata instance to the exported format
40 * @param ClassMetadataInfo $metadata
41 * @return mixed $exported
43 public function exportClassMetadata(ClassMetadataInfo $metadata)
48 $lines[] = 'use Doctrine\ORM\Mapping\ClassMetadataInfo;';
51 if ($metadata->isMappedSuperclass) {
52 $lines[] = '$metadata->isMappedSuperclass = true;';
55 if ($metadata->inheritanceType) {
56 $lines[] = '$metadata->setInheritanceType(ClassMetadataInfo::INHERITANCE_TYPE_' . $this->_getInheritanceTypeString($metadata->inheritanceType) . ');';
59 if ($metadata->customRepositoryClassName) {
60 $lines[] = "\$metadata->customRepositoryClassName = '" . $metadata->customRepositoryClassName . "';";
63 if ($metadata->table) {
64 $lines[] = '$metadata->setPrimaryTable(' . $this->_varExport($metadata->table) . ');';
67 if ($metadata->discriminatorColumn) {
68 $lines[] = '$metadata->setDiscriminatorColumn(' . $this->_varExport($metadata->discriminatorColumn) . ');';
71 if ($metadata->discriminatorMap) {
72 $lines[] = '$metadata->setDiscriminatorMap(' . $this->_varExport($metadata->discriminatorMap) . ');';
75 if ($metadata->changeTrackingPolicy) {
76 $lines[] = '$metadata->setChangeTrackingPolicy(ClassMetadataInfo::CHANGETRACKING_' . $this->_getChangeTrackingPolicyString($metadata->changeTrackingPolicy) . ');';
79 if ($metadata->lifecycleCallbacks) {
80 foreach ($metadata->lifecycleCallbacks as $event => $callbacks) {
81 foreach ($callbacks as $callback) {
82 $lines[] = "\$metadata->addLifecycleCallback('$callback', '$event');";
87 foreach ($metadata->fieldMappings as $fieldMapping) {
88 $lines[] = '$metadata->mapField(' . $this->_varExport($fieldMapping) . ');';
91 if ( ! $metadata->isIdentifierComposite && $generatorType = $this->_getIdGeneratorTypeString($metadata->generatorType)) {
92 $lines[] = '$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_' . $generatorType . ');';
95 foreach ($metadata->associationMappings as $associationMapping) {
96 $cascade = array('remove', 'persist', 'refresh', 'merge', 'detach');
97 foreach ($cascade as $key => $value) {
98 if ( ! $associationMapping['isCascade'.ucfirst($value)]) {
99 unset($cascade[$key]);
102 $associationMappingArray = array(
103 'fieldName' => $associationMapping['fieldName'],
104 'targetEntity' => $associationMapping['targetEntity'],
105 'cascade' => $cascade,
108 if ($associationMapping['type'] & ClassMetadataInfo::TO_ONE) {
109 $method = 'mapOneToOne';
110 $oneToOneMappingArray = array(
111 'mappedBy' => $associationMapping['mappedBy'],
112 'inversedBy' => $associationMapping['inversedBy'],
113 'joinColumns' => $associationMapping['joinColumns'],
114 'orphanRemoval' => $associationMapping['orphanRemoval'],
117 $associationMappingArray = array_merge($associationMappingArray, $oneToOneMappingArray);
118 } else if ($associationMapping['type'] == ClassMetadataInfo::ONE_TO_MANY) {
119 $method = 'mapOneToMany';
120 $potentialAssociationMappingIndexes = array(
125 foreach ($potentialAssociationMappingIndexes as $index) {
126 if (isset($associationMapping[$index])) {
127 $oneToManyMappingArray[$index] = $associationMapping[$index];
130 $associationMappingArray = array_merge($associationMappingArray, $oneToManyMappingArray);
131 } else if ($associationMapping['type'] == ClassMetadataInfo::MANY_TO_MANY) {
132 $method = 'mapManyToMany';
133 $potentialAssociationMappingIndexes = array(
138 foreach ($potentialAssociationMappingIndexes as $index) {
139 if (isset($associationMapping[$index])) {
140 $manyToManyMappingArray[$index] = $associationMapping[$index];
143 $associationMappingArray = array_merge($associationMappingArray, $manyToManyMappingArray);
146 $lines[] = '$metadata->' . $method . '(' . $this->_varExport($associationMappingArray) . ');';
149 return implode("\n", $lines);
152 protected function _varExport($var)
154 $export = var_export($var, true);
155 $export = str_replace("\n", PHP_EOL . str_repeat(' ', 8), $export);
156 $export = str_replace(' ', ' ', $export);
157 $export = str_replace('array (', 'array(', $export);
158 $export = str_replace('array( ', 'array(', $export);
159 $export = str_replace(',)', ')', $export);
160 $export = str_replace(', )', ')', $export);
161 $export = str_replace(' ', ' ', $export);