Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / lib / Doctrine / ORM / Tools / Export / Driver / PhpExporter.php
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  * 
28  * @link    www.doctrine-project.org
29  * @since   2.0
30  * @author  Jonathan Wage <jonwage@gmail.com>
31  */
32 class PhpExporter extends AbstractExporter
33 {
34     protected $_extension = '.php';
35
36     /**
37      * Converts a single ClassMetadata instance to the exported format
38      * and returns it
39      *
40      * @param ClassMetadataInfo $metadata
41      * @return mixed $exported
42      */
43     public function exportClassMetadata(ClassMetadataInfo $metadata)
44     {
45         $lines = array();
46         $lines[] = '<?php';
47         $lines[] = null;
48         $lines[] = 'use Doctrine\ORM\Mapping\ClassMetadataInfo;';
49         $lines[] = null;
50
51         if ($metadata->isMappedSuperclass) {
52             $lines[] = '$metadata->isMappedSuperclass = true;';
53         }
54
55         if ($metadata->inheritanceType) {
56             $lines[] = '$metadata->setInheritanceType(ClassMetadataInfo::INHERITANCE_TYPE_' . $this->_getInheritanceTypeString($metadata->inheritanceType) . ');';
57         }
58
59         if ($metadata->customRepositoryClassName) {
60             $lines[] = "\$metadata->customRepositoryClassName = '" . $metadata->customRepositoryClassName . "';";
61         }
62
63         if ($metadata->table) {
64             $lines[] = '$metadata->setPrimaryTable(' . $this->_varExport($metadata->table) . ');';
65         }
66
67         if ($metadata->discriminatorColumn) {
68             $lines[] = '$metadata->setDiscriminatorColumn(' . $this->_varExport($metadata->discriminatorColumn) . ');';
69         }
70
71         if ($metadata->discriminatorMap) {
72             $lines[] = '$metadata->setDiscriminatorMap(' . $this->_varExport($metadata->discriminatorMap) . ');';
73         }
74
75         if ($metadata->changeTrackingPolicy) {
76             $lines[] = '$metadata->setChangeTrackingPolicy(ClassMetadataInfo::CHANGETRACKING_' . $this->_getChangeTrackingPolicyString($metadata->changeTrackingPolicy) . ');';
77         }
78
79         if ($metadata->lifecycleCallbacks) {
80             foreach ($metadata->lifecycleCallbacks as $event => $callbacks) {
81                 foreach ($callbacks as $callback) {
82                     $lines[] = "\$metadata->addLifecycleCallback('$callback', '$event');";
83                 }
84             }
85         }
86
87         foreach ($metadata->fieldMappings as $fieldMapping) {
88             $lines[] = '$metadata->mapField(' . $this->_varExport($fieldMapping) . ');';
89         }
90
91         if ( ! $metadata->isIdentifierComposite && $generatorType = $this->_getIdGeneratorTypeString($metadata->generatorType)) {
92             $lines[] = '$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_' . $generatorType . ');';
93         }
94
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]);
100                 }
101             }
102             $associationMappingArray = array(
103                 'fieldName'    => $associationMapping['fieldName'],
104                 'targetEntity' => $associationMapping['targetEntity'],
105                 'cascade'     => $cascade,
106             );
107
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'],
115                 );
116
117                 $associationMappingArray = array_merge($associationMappingArray, $oneToOneMappingArray);
118             } else if ($associationMapping['type'] == ClassMetadataInfo::ONE_TO_MANY) {
119                 $method = 'mapOneToMany';
120                 $potentialAssociationMappingIndexes = array(
121                     'mappedBy',
122                     'orphanRemoval',
123                     'orderBy',
124                 );
125                 foreach ($potentialAssociationMappingIndexes as $index) {
126                     if (isset($associationMapping[$index])) {
127                         $oneToManyMappingArray[$index] = $associationMapping[$index];
128                     }
129                 }
130                 $associationMappingArray = array_merge($associationMappingArray, $oneToManyMappingArray);
131             } else if ($associationMapping['type'] == ClassMetadataInfo::MANY_TO_MANY) {
132                 $method = 'mapManyToMany';
133                 $potentialAssociationMappingIndexes = array(
134                     'mappedBy',
135                     'joinTable',
136                     'orderBy',
137                 );
138                 foreach ($potentialAssociationMappingIndexes as $index) {
139                     if (isset($associationMapping[$index])) {
140                         $manyToManyMappingArray[$index] = $associationMapping[$index];
141                     }
142                 }
143                 $associationMappingArray = array_merge($associationMappingArray, $manyToManyMappingArray);
144             }
145
146             $lines[] = '$metadata->' . $method . '(' . $this->_varExport($associationMappingArray) . ');';
147         }
148
149         return implode("\n", $lines);
150     }
151
152     protected function _varExport($var)
153     {
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);
162
163         return $export;
164     }
165 }