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.phpdoctrine.com>.
|
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.phpdoctrine.com
|
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 |
*/
|
35 |
class Doctrine_Export_Schema
|
36 |
{
|
37 |
/**
|
38 |
* buildSchema
|
39 |
*
|
40 |
* Build schema array that can be dumped to file
|
41 |
*
|
42 |
* @param string $directory
|
43 |
* @return void
|
44 |
*/
|
45 |
public function buildSchema($directory = null, $models = array())
|
46 |
{
|
47 |
if ($directory) {
|
48 |
$loadedModels = Doctrine::loadModels($directory);
|
49 |
} else {
|
50 |
$loadedModels = Doctrine::getLoadedModels();
|
51 |
}
|
52 |
|
53 |
$array = array();
|
54 |
|
55 |
$parent = new ReflectionClass('Doctrine_Record');
|
56 |
|
57 |
$sql = array();
|
58 |
$fks = array();
|
59 |
|
60 |
// we iterate trhough the diff of previously declared classes
|
61 |
// and currently declared classes
|
62 |
foreach ($loadedModels as $name) {
|
63 |
if ( ! empty($models) && !in_array($name, $models)) {
|
64 |
continue;
|
65 |
}
|
66 |
|
67 |
$record = new $name();
|
68 |
$recordTable = $record->getTable();
|
69 |
|
70 |
$data = $recordTable->getExportableFormat();
|
71 |
|
72 |
$table = array();
|
73 |
$table['tableName'] = $data['tableName'];
|
74 |
$table['className'] = get_class($record);
|
75 |
|
76 |
foreach ($data['columns'] AS $name => $column) {
|
77 |
$data['columns'][$name]['name'] = $name;
|
78 |
}
|
79 |
|
80 |
$table['columns'] = $data['columns'];
|
81 |
|
82 |
$relations = $recordTable->getRelations();
|
83 |
foreach ($relations as $key => $relation) {
|
84 |
$relationData = $relation->toArray();
|
85 |
|
86 |
$relationKey = $relationData['alias'];
|
87 |
|
88 |
if (isset($relationData['refTable']) && $relationData['refTable']) {
|
89 |
$table['relations'][$relationKey]['refClass'] = $relationData['refTable']->getComponentName();
|
90 |
}
|
91 |
|
92 |
if (isset($relationData['class']) && $relationData['class'] && $relation['class'] != $relationKey) {
|
93 |
$table['relations'][$relationKey]['class'] = $relationData['class'];
|
94 |
}
|
95 |
|
96 |
$table['relations'][$relationKey]['local'] = $relationData['local'];
|
97 |
$table['relations'][$relationKey]['foreign'] = $relationData['foreign'];
|
98 |
|
99 |
if ($relationData['type'] === Doctrine_Relation::ONE) {
|
100 |
$table['relations'][$relationKey]['type'] = 'one';
|
101 |
} else if($relationData['type'] === Doctrine_Relation::MANY) {
|
102 |
$table['relations'][$relationKey]['type'] = 'many';
|
103 |
} else {
|
104 |
$table['relations'][$relationKey]['type'] = 'one';
|
105 |
}
|
106 |
}
|
107 |
|
108 |
$array[$table['className']] = $table;
|
109 |
}
|
110 |
|
111 |
return $array;
|
112 |
}
|
113 |
|
114 |
/**
|
115 |
* exportSchema
|
116 |
*
|
117 |
* @param string $schema
|
118 |
* @param string $directory
|
119 |
* @return string $string of data in the specified format
|
120 |
* @return void
|
121 |
*/
|
122 |
public function exportSchema($schema, $format = 'yml', $directory = null, $models = array())
|
123 |
{
|
124 |
$array = $this->buildSchema($directory, $models);
|
125 |
|
126 |
return Doctrine_Parser::dump($array, $format, $schema);
|
127 |
}
|
128 |
} |