PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/RuaApp/libraries/Doctrine/ORM/Tools/Export/Driver/PhpExporter.php

https://github.com/webvpro/RUA-CORE
PHP | 169 lines | 112 code | 22 blank | 35 comment | 19 complexity | 848df6fbbc45b25c218b9bc4c38f9f6f MD5 | raw file
  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. namespace Doctrine\ORM\Tools\Export\Driver;
  22. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  23. /**
  24. * ClassMetadata exporter for PHP code
  25. *
  26. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  27. * @link www.doctrine-project.org
  28. * @since 2.0
  29. * @version $Revision$
  30. * @author Jonathan Wage <jonwage@gmail.com>
  31. */
  32. class PhpExporter extends AbstractExporter
  33. {
  34. protected $_extension = '.php';
  35. /**
  36. * Converts a single ClassMetadata instance to the exported format
  37. * and returns it
  38. *
  39. * @param ClassMetadataInfo $metadata
  40. * @return mixed $exported
  41. */
  42. public function exportClassMetadata(ClassMetadataInfo $metadata)
  43. {
  44. $lines = array();
  45. $lines[] = '<?php';
  46. $lines[] = null;
  47. $lines[] = 'use Doctrine\ORM\Mapping\ClassMetadataInfo;';
  48. $lines[] = null;
  49. if ($metadata->isMappedSuperclass) {
  50. $lines[] = '$metadata->isMappedSuperclass = true;';
  51. }
  52. if ($metadata->inheritanceType) {
  53. $lines[] = '$metadata->setInheritanceType(ClassMetadataInfo::INHERITANCE_TYPE_' . $this->_getInheritanceTypeString($metadata->inheritanceType) . ');';
  54. }
  55. if ($metadata->customRepositoryClassName) {
  56. $lines[] = "\$metadata->customRepositoryClassName = '" . $metadata->customRepositoryClassName . "';";
  57. }
  58. if ($metadata->table) {
  59. $lines[] = '$metadata->setPrimaryTable(' . $this->_varExport($metadata->table) . ');';
  60. }
  61. if ($metadata->discriminatorColumn) {
  62. $lines[] = '$metadata->setDiscriminatorColumn(' . $this->_varExport($metadata->discriminatorColumn) . ');';
  63. }
  64. if ($metadata->discriminatorMap) {
  65. $lines[] = '$metadata->setDiscriminatorMap(' . $this->_varExport($metadata->discriminatorMap) . ');';
  66. }
  67. if ($metadata->changeTrackingPolicy) {
  68. $lines[] = '$metadata->setChangeTrackingPolicy(ClassMetadataInfo::CHANGETRACKING_' . $this->_getChangeTrackingPolicyString($metadata->changeTrackingPolicy) . ');';
  69. }
  70. if ($metadata->lifecycleCallbacks) {
  71. foreach ($metadata->lifecycleCallbacks as $event => $callbacks) {
  72. foreach ($callbacks as $callback) {
  73. $lines[] = "\$metadata->addLifecycleCallback('$callback', '$event');";
  74. }
  75. }
  76. }
  77. foreach ($metadata->fieldMappings as $fieldMapping) {
  78. $lines[] = '$metadata->mapField(' . $this->_varExport($fieldMapping) . ');';
  79. }
  80. if ($generatorType = $this->_getIdGeneratorTypeString($metadata->generatorType)) {
  81. $lines[] = '$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_' . $generatorType . ');';
  82. }
  83. foreach ($metadata->associationMappings as $associationMapping) {
  84. $cascade = array('remove', 'persist', 'refresh', 'merge', 'detach');
  85. foreach ($cascade as $key => $value) {
  86. if ( ! $associationMapping['isCascade'.ucfirst($value)]) {
  87. unset($cascade[$key]);
  88. }
  89. }
  90. $associationMappingArray = array(
  91. 'fieldName' => $associationMapping['fieldName'],
  92. 'targetEntity' => $associationMapping['targetEntity'],
  93. 'cascade' => $cascade,
  94. );
  95. if ($associationMapping['type'] & ClassMetadataInfo::TO_ONE) {
  96. $method = 'mapOneToOne';
  97. $oneToOneMappingArray = array(
  98. 'mappedBy' => $associationMapping['mappedBy'],
  99. 'inversedBy' => $associationMapping['inversedBy'],
  100. 'joinColumns' => $associationMapping['joinColumns'],
  101. 'orphanRemoval' => $associationMapping['orphanRemoval'],
  102. );
  103. $associationMappingArray = array_merge($associationMappingArray, $oneToOneMappingArray);
  104. } else if ($associationMapping['type'] == ClassMetadataInfo::ONE_TO_MANY) {
  105. $method = 'mapOneToMany';
  106. $potentialAssociationMappingIndexes = array(
  107. 'mappedBy',
  108. 'orphanRemoval',
  109. 'orderBy',
  110. );
  111. foreach ($potentialAssociationMappingIndexes as $index) {
  112. if (isset($associationMapping[$index])) {
  113. $oneToManyMappingArray[$index] = $associationMapping[$index];
  114. }
  115. }
  116. $associationMappingArray = array_merge($associationMappingArray, $oneToManyMappingArray);
  117. } else if ($associationMapping['type'] == ClassMetadataInfo::MANY_TO_MANY) {
  118. $method = 'mapManyToMany';
  119. $potentialAssociationMappingIndexes = array(
  120. 'mappedBy',
  121. 'joinTable',
  122. 'orderBy',
  123. );
  124. foreach ($potentialAssociationMappingIndexes as $index) {
  125. if (isset($associationMapping[$index])) {
  126. $manyToManyMappingArray[$index] = $associationMapping[$index];
  127. }
  128. }
  129. $associationMappingArray = array_merge($associationMappingArray, $manyToManyMappingArray);
  130. }
  131. $lines[] = '$metadata->' . $method . '(' . $this->_varExport($associationMappingArray) . ');';
  132. }
  133. return implode("\n", $lines);
  134. }
  135. protected function _varExport($var)
  136. {
  137. $export = var_export($var, true);
  138. $export = str_replace("\n", PHP_EOL . str_repeat(' ', 8), $export);
  139. $export = str_replace(' ', ' ', $export);
  140. $export = str_replace('array (', 'array(', $export);
  141. $export = str_replace('array( ', 'array(', $export);
  142. $export = str_replace(',)', ')', $export);
  143. $export = str_replace(', )', ')', $export);
  144. $export = str_replace(' ', ' ', $export);
  145. return $export;
  146. }
  147. }