1<?php 2/* 3 * $Id: Schema.php 1838 2007-06-26 00:58:21Z nicobn $ 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 22/** 23 * Doctrine_Export_Schema 24 * 25 * Used for exporting a schema to a yaml file 26 * 27 * @package Doctrine 28 * @subpackage Export 29 * @link www.doctrine-project.org 30 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL 31 * @version $Revision: 1838 $ 32 * @author Nicolas Bérard-Nault <nicobn@gmail.com> 33 * @author Jonathan H. Wage <jwage@mac.com> 34 */ 35class Doctrine_Export_Schema 36{ 37 /** 38 * buildSchema 39 * 40 * Build schema array that can be dumped to file 41 * 42 * @param string $directory The directory of models to build the schema from 43 * @param array $models The array of model names to build the schema for 44 * @param integer $modelLoading The model loading strategy to use to load the models from the passed directory 45 * @return void 46 */ 47 public function buildSchema($directory = null, $models = array(), $modelLoading = null) 48 { 49 if ($directory !== null) { 50 $loadedModels = Doctrine_Core::filterInvalidModels(Doctrine_Core::loadModels($directory, $modelLoading)); 51 } else { 52 $loadedModels = Doctrine_Core::getLoadedModels(); 53 } 54 55 $array = array(); 56 57 $parent = new ReflectionClass('Doctrine_Record'); 58 59 $sql = array(); 60 $fks = array(); 61 62 // we iterate through the diff of previously declared classes 63 // and currently declared classes 64 foreach ($loadedModels as $className) { 65 if ( ! empty($models) && !in_array($className, $models)) { 66 continue; 67 } 68 69 $recordTable = Doctrine_Core::getTable($className); 70 71 $data = $recordTable->getExportableFormat(); 72 73 $table = array(); 74 $table['connection'] = $recordTable->getConnection()->getName(); 75 $remove = array('ptype', 'ntype', 'alltypes'); 76 // Fix explicit length in schema, concat it to type in this format: type(length) 77 foreach ($data['columns'] AS $name => $column) { 78 if (isset($column['length']) && $column['length'] && isset($column['scale']) && $column['scale']) { 79 $data['columns'][$name]['type'] = $column['type'] . '(' . $column['length'] . ', ' . $column['scale'] . ')'; 80 unset($data['columns'][$name]['length'], $data['columns'][$name]['scale']); 81 } else { 82 $data['columns'][$name]['type'] = $column['type'] . '(' . $column['length'] . ')'; 83 unset($data['columns'][$name]['length']); 84 } 85 // Strip out schema information which is not necessary to be dumped to the yaml schema file 86 foreach ($remove as $value) { 87 if (isset($data['columns'][$name][$value])) { 88 unset($data['columns'][$name][$value]); 89 } 90 } 91 92 // If type is the only property of the column then lets abbreviate the syntax 93 // columns: { name: string(255) } 94 if (count($data['columns'][$name]) === 1 && isset($data['columns'][$name]['type'])) { 95 $type = $data['columns'][$name]['type']; 96 unset($data['columns'][$name]); 97 $data['columns'][$name] = $type; 98 } 99 } 100 $table['tableName'] = $data['tableName']; 101 $table['columns'] = $data['columns']; 102 103 $relations = $recordTable->getRelations(); 104 foreach ($relations as $key => $relation) { 105 $relationData = $relation->toArray(); 106 107 $relationKey = $relationData['alias']; 108 109 if (isset($relationData['refTable']) && $relationData['refTable']) { 110 $table['relations'][$relationKey]['refClass'] = $relationData['refTable']->getComponentName(); 111 } 112 113 if (isset($relationData['class']) && $relationData['class'] && $relation['class'] != $relationKey) { 114 $table['relations'][$relationKey]['class'] = $relationData['class']; 115 } 116 117 $table['relations'][$relationKey]['local'] = $relationData['local']; 118 $table['relations'][$relationKey]['foreign'] = $relationData['foreign']; 119 120 if ($relationData['type'] === Doctrine_Relation::ONE) { 121 $table['relations'][$relationKey]['type'] = 'one'; 122 } else if ($relationData['type'] === Doctrine_Relation::MANY) { 123 $table['relations'][$relationKey]['type'] = 'many'; 124 } else { 125 $table['relations'][$relationKey]['type'] = 'one'; 126 } 127 } 128 129 $array[$className] = $table; 130 } 131 132 return $array; 133 } 134 135 /** 136 * exportSchema 137 * 138 * @param string $schema 139 * @param string $directory 140 * @param string $string of data in the specified format 141 * @param integer $modelLoading The model loading strategy to use to load the models from the passed directory 142 * @return void 143 */ 144 public function exportSchema($schema, $format = 'yml', $directory = null, $models = array(), $modelLoading = null) 145 { 146 $array = $this->buildSchema($directory, $models, $modelLoading); 147 148 if (is_dir($schema)) { 149 $schema = $schema . DIRECTORY_SEPARATOR . 'schema.' . $format; 150 } 151 152 return Doctrine_Parser::dump($array, $format, $schema); 153 } 154}