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 |
* class Doctrine_Export_Schema
|
24 |
*
|
25 |
* Different methods to import a XML schema. The logic behind using two different
|
26 |
* methods is simple. Some people will like the idea of producing Doctrine_Record
|
27 |
* objects directly, which is totally fine. But in fast and growing application,
|
28 |
* table definitions tend to be a little bit more volatile. importArr() can be used
|
29 |
* to output a table definition in a PHP file. This file can then be stored
|
30 |
* independantly from the object itself.
|
31 |
*
|
32 |
* @package Doctrine
|
33 |
* @subpackage Export
|
34 |
* @link www.phpdoctrine.com
|
35 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
36 |
* @version $Revision: 1838 $
|
37 |
* @author Nicolas BĂ©rard-Nault <nicobn@gmail.com>
|
38 |
*/
|
39 |
class Doctrine_Export_Schema
|
40 |
{
|
41 |
/**
|
42 |
* buildSchema
|
43 |
*
|
44 |
* Build schema array that can be dumped to file
|
45 |
*
|
46 |
* @param string $directory
|
47 |
* @return void
|
48 |
*/
|
49 |
public function buildSchema($directory = null, $models = array())
|
50 |
{
|
51 |
$array = array();
|
52 |
|
53 |
if ($directory) {
|
54 |
$loadedModels = Doctrine::loadModels($directory);
|
55 |
} else {
|
56 |
$loadedModels = Doctrine::getLoadedModels();
|
57 |
}
|
58 |
|
59 |
$parent = new ReflectionClass('Doctrine_Record');
|
60 |
|
61 |
$sql = array();
|
62 |
$fks = array();
|
63 |
|
64 |
// we iterate trhough the diff of previously declared classes
|
65 |
// and currently declared classes
|
66 |
foreach ($loadedModels as $name) {
|
67 |
if (!empty($models) && !in_array($name, $models)) {
|
68 |
continue;
|
69 |
}
|
70 |
|
71 |
$class = new ReflectionClass($name);
|
72 |
|
73 |
// check if class is an instance of Doctrine_Record and not abstract
|
74 |
// class must have method setTableDefinition (to avoid non-Record subclasses like symfony's sfDoctrineRecord)
|
75 |
// we have to recursively iterate through the class parents just to be sure that the classes using for example
|
76 |
// column aggregation inheritance are properly exported to database
|
77 |
while ($class->isAbstract() ||
|
78 |
! $class->isSubclassOf($parent) ||
|
79 |
! $class->hasMethod('setTableDefinition') ||
|
80 |
( $class->hasMethod('setTableDefinition') &&
|
81 |
$class->getMethod('setTableDefinition')->getDeclaringClass()->getName() !== $class->getName())) {
|
82 |
|
83 |
$class = $class->getParentClass();
|
84 |
if ($class === false) {
|
85 |
break;
|
86 |
}
|
87 |
}
|
88 |
|
89 |
if ($class === false) {
|
90 |
continue;
|
91 |
}
|
92 |
|
93 |
$record = new $name();
|
94 |
$recordTable = $record->getTable();
|
95 |
|
96 |
$data = $recordTable->getExportableFormat();
|
97 |
|
98 |
$table = array();
|
99 |
$table['tableName'] = $data['tableName'];
|
100 |
$table['className'] = get_class($record);
|
101 |
|
102 |
foreach ($data['columns'] AS $name => $column) {
|
103 |
$data['columns'][$name]['name'] = $name;
|
104 |
}
|
105 |
|
106 |
$table['columns'] = $data['columns'];
|
107 |
|
108 |
$relations = $recordTable->getRelations();
|
109 |
foreach ($relations as $key => $relation) {
|
110 |
$relationData = $relation->toArray();
|
111 |
|
112 |
$relationKey = $relationData['alias'];
|
113 |
|
114 |
if (isset($relationData['refTable']) && $relationData['refTable']) {
|
115 |
$table['relations'][$relationKey]['refClass'] = $relationData['refTable']->getComponentName();
|
116 |
}
|
117 |
|
118 |
if (isset($relationData['class']) && $relationData['class'] && $relation['class'] != $relationKey) {
|
119 |
$table['relations'][$relationKey]['class'] = $relationData['class'];
|
120 |
}
|
121 |
|
122 |
$table['relations'][$relationKey]['local'] = $relationData['local'];
|
123 |
$table['relations'][$relationKey]['foreign'] = $relationData['foreign'];
|
124 |
|
125 |
if ($relationData['type'] === Doctrine_Relation::ONE) {
|
126 |
$table['relations'][$relationKey]['type'] = 'one';
|
127 |
} else if($relationData['type'] === Doctrine_Relation::MANY) {
|
128 |
$table['relations'][$relationKey]['type'] = 'many';
|
129 |
} else {
|
130 |
$table['relations'][$relationKey]['type'] = 'one';
|
131 |
}
|
132 |
}
|
133 |
|
134 |
$array[$table['className']] = $table;
|
135 |
}
|
136 |
|
137 |
return $array;
|
138 |
}
|
139 |
|
140 |
/**
|
141 |
* exportSchema
|
142 |
*
|
143 |
* @param string $schema
|
144 |
* @param string $directory
|
145 |
* @return void
|
146 |
*/
|
147 |
public function exportSchema($schema, $format = 'yml', $directory = null, $models = array())
|
148 |
{
|
149 |
$array = $this->buildSchema($directory, $models);
|
150 |
|
151 |
Doctrine_Parser::dump($array, $format, $schema);
|
152 |
}
|
153 |
} |