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_Import_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 Import
|
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 |
* @author Jonathan H. Wage <jonwage@gmail.com>
|
39 |
*/
|
40 |
class Doctrine_Import_Schema
|
41 |
{
|
42 |
public $relations = array();
|
43 |
public $indexes = array();
|
44 |
public $generateBaseClasses = false;
|
45 |
|
46 |
public function generateBaseClasses($bool = null)
|
47 |
{
|
48 |
if ($bool !== null) {
|
49 |
$this->generateBaseClasses = $bool;
|
50 |
}
|
51 |
|
52 |
return $this->generateBaseClasses;
|
53 |
}
|
54 |
public function buildSchema($schema, $format)
|
55 |
{
|
56 |
$array = array();
|
57 |
foreach ((array) $schema AS $s) {
|
58 |
$array = array_merge($array, $this->parseSchema($s, $format));
|
59 |
}
|
60 |
|
61 |
$this->buildRelationships($array);
|
62 |
|
63 |
return array('schema' => $array, 'relations' => $this->relations, 'indexes' => $this->indexes);
|
64 |
}
|
65 |
/**
|
66 |
* importSchema
|
67 |
*
|
68 |
* A method to import a Schema and translate it into a Doctrine_Record object
|
69 |
*
|
70 |
* @param string $schema The file containing the XML schema
|
71 |
* @param string $directory The directory where the Doctrine_Record class will be written
|
72 |
* @param array $models Optional array of models to import
|
73 |
*
|
74 |
* @access public
|
75 |
*/
|
76 |
public function importSchema($schema, $format = 'yml', $directory = null, $models = array())
|
77 |
{
|
78 |
$builder = new Doctrine_Import_Builder();
|
79 |
$builder->setTargetPath($directory);
|
80 |
$builder->generateBaseClasses($this->generateBaseClasses());
|
81 |
|
82 |
$schema = $this->buildSchema($schema, $format);
|
83 |
|
84 |
$array = $schema['schema'];
|
85 |
|
86 |
foreach ($array as $name => $properties) {
|
87 |
if (!empty($models) && !in_array($properties['className'], $models)) {
|
88 |
continue;
|
89 |
}
|
90 |
|
91 |
$options = $this->getOptions($properties, $directory);
|
92 |
$columns = $this->getColumns($properties);
|
93 |
$relations = $this->getRelations($properties);
|
94 |
|
95 |
$builder->buildRecord($options, $columns, $relations);
|
96 |
}
|
97 |
}
|
98 |
|
99 |
public function getOptions($properties, $directory)
|
100 |
{
|
101 |
$options = array();
|
102 |
$options['className'] = $properties['className'];
|
103 |
$options['fileName'] = $directory.DIRECTORY_SEPARATOR.$properties['className'].'.class.php';
|
104 |
$options['tableName'] = isset($properties['tableName']) ? $properties['tableName']:null;
|
105 |
|
106 |
if (isset($properties['inheritance'])) {
|
107 |
$options['inheritance'] = $properties['inheritance'];
|
108 |
}
|
109 |
|
110 |
return $options;
|
111 |
}
|
112 |
|
113 |
public function getColumns($properties)
|
114 |
{
|
115 |
return isset($properties['columns']) ? $properties['columns']:array();
|
116 |
}
|
117 |
|
118 |
public function getRelations($properties)
|
119 |
{
|
120 |
return isset($this->relations[$properties['className']]) ? $this->relations[$properties['className']]:array();
|
121 |
}
|
122 |
|
123 |
public function getIndexes($properties)
|
124 |
{
|
125 |
return isset($properties['indexes']) ? $properties['indexes']:array();;
|
126 |
}
|
127 |
|
128 |
/**
|
129 |
* parseSchema
|
130 |
*
|
131 |
* A method to parse a Yml Schema and translate it into a property array.
|
132 |
* The function returns that property array.
|
133 |
*
|
134 |
* @param string $schema Path to the file containing the XML schema
|
135 |
* @return array
|
136 |
*/
|
137 |
public function parseSchema($schema, $type)
|
138 |
{
|
139 |
$array = Doctrine_Parser::load($schema, $type);
|
140 |
|
141 |
$build = array();
|
142 |
|
143 |
foreach ($array as $className => $table) {
|
144 |
$columns = array();
|
145 |
|
146 |
$className = isset($table['className']) ? (string) $table['className']:(string) $className;
|
147 |
$tableName = isset($table['tableName']) ? (string) $table['tableName']:(string) Doctrine::tableize($className);
|
148 |
|
149 |
$build[$className]['className'] = $className;
|
150 |
|
151 |
if (isset($table['columns'])) {
|
152 |
foreach ($table['columns'] as $columnName => $field) {
|
153 |
$colDesc = array();
|
154 |
$colDesc['name'] = isset($field['name']) ? (string) $field['name']:$columnName;
|
155 |
$colDesc['type'] = isset($field['type']) ? (string) $field['type']:null;
|
156 |
$colDesc['ptype'] = isset($field['ptype']) ? (string) $field['ptype']:(string) $colDesc['type'];
|
157 |
$colDesc['length'] = isset($field['length']) ? (int) $field['length']:null;
|
158 |
$colDesc['fixed'] = isset($field['fixed']) ? (int) $field['fixed']:null;
|
159 |
$colDesc['unsigned'] = isset($field['unsigned']) ? (bool) $field['unsigned']:null;
|
160 |
$colDesc['primary'] = isset($field['primary']) ? (bool) (isset($field['primary']) && $field['primary']):null;
|
161 |
$colDesc['default'] = isset($field['default']) ? (string) $field['default']:null;
|
162 |
$colDesc['notnull'] = isset($field['notnull']) ? (bool) (isset($field['notnull']) && $field['notnull']):null;
|
163 |
$colDesc['autoincrement'] = isset($field['autoincrement']) ? (bool) (isset($field['autoincrement']) && $field['autoincrement']):null;
|
164 |
$colDesc['unique'] = isset($field['unique']) ? (bool) (isset($field['unique']) && $field['unique']):null;
|
165 |
$colDesc['values'] = isset($field['values']) ? (array) $field['values']: null;
|
166 |
|
167 |
$columns[(string) $colDesc['name']] = $colDesc;
|
168 |
}
|
169 |
|
170 |
$build[$className]['tableName'] = $tableName;
|
171 |
$build[$className]['columns'] = $columns;
|
172 |
$build[$className]['relations'] = isset($table['relations']) ? $table['relations']:array();
|
173 |
$build[$className]['indexes'] = isset($table['indexes']) ? $table['indexes']:array();
|
174 |
}
|
175 |
|
176 |
if (isset($table['inheritance'])) {
|
177 |
$build[$className]['inheritance'] = $table['inheritance'];
|
178 |
}
|
179 |
}
|
180 |
|
181 |
return $build;
|
182 |
}
|
183 |
|
184 |
public function buildRelationships(&$array)
|
185 |
{
|
186 |
foreach ($array as $name => $properties) {
|
187 |
if (!isset($properties['relations'])) {
|
188 |
continue;
|
189 |
}
|
190 |
|
191 |
$className = $properties['className'];
|
192 |
$relations = $properties['relations'];
|
193 |
|
194 |
foreach ($relations as $alias => $relation) {
|
195 |
|
196 |
$class = isset($relation['class']) ? $relation['class']:$alias;
|
197 |
|
198 |
$relation['foreign'] = isset($relation['foreign'])?$relation['foreign']:'id';
|
199 |
$relation['alias'] = isset($relation['alias']) ? $relation['alias'] : $alias;
|
200 |
$relation['class'] = $class;
|
201 |
|
202 |
if (isset($relation['type']) && $relation['type']) {
|
203 |
$relation['type'] = $relation['type'] === 'one' ? Doctrine_Relation::ONE:Doctrine_Relation::MANY;
|
204 |
} else {
|
205 |
$relation['type'] = Doctrine_Relation::ONE;
|
206 |
}
|
207 |
|
208 |
if (isset($relation['foreignType']) && $relation['foreignType']) {
|
209 |
$relation['foreignType'] = $relation['foreignType'] === 'one' ? Doctrine_Relation::ONE:Doctrine_Relation::MANY;
|
210 |
}
|
211 |
|
212 |
if(isset($relation['refClass']) && !empty($relation['refClass']) && (!isset($array[$relation['refClass']]['relations']) || empty($array[$relation['refClass']]['relations']))) {
|
213 |
$array[$relation['refClass']]['relations'][$className] = array('local'=>$relation['local'],'foreign'=>$relation['foreign'],'ignore'=>true);
|
214 |
$array[$relation['refClass']]['relations'][$relation['class']] = array('local'=>$relation['local'],'foreign'=>$relation['foreign'],'ignore'=>true);
|
215 |
|
216 |
if(isset($relation['foreignAlias'])) {
|
217 |
$array[$relation['class']]['relations'][$relation['foreignAlias']] = array('type'=>$relation['type'],'local'=>$relation['foreign'],'foreign'=>$relation['local'],'refClass'=>$relation['refClass'],'class'=>$className);
|
218 |
}
|
219 |
}
|
220 |
|
221 |
$this->relations[$className][$alias] = $relation;
|
222 |
}
|
223 |
}
|
224 |
|
225 |
$this->fixRelationships();
|
226 |
}
|
227 |
|
228 |
public function fixRelationships()
|
229 |
{
|
230 |
// define both sides of the relationship
|
231 |
foreach($this->relations as $className => $relations) {
|
232 |
foreach ($relations AS $alias => $relation) {
|
233 |
if(isset($relation['ignore']) && $relation['ignore'] || isset($relation['refClass']) || isset($this->relations[$relation['class']]['relations'][$className])) {
|
234 |
continue;
|
235 |
}
|
236 |
|
237 |
$newRelation = array();
|
238 |
$newRelation['foreign'] = $relation['local'];
|
239 |
$newRelation['local'] = $relation['foreign'];
|
240 |
$newRelation['class'] = $className;
|
241 |
$newRelation['alias'] = isset($relation['foreignAlias'])?$relation['foreignAlias']:$className;
|
242 |
|
243 |
if(isset($relation['foreignType'])) {
|
244 |
$newRelation['type'] = $relation['foreignType'];
|
245 |
} else {
|
246 |
$newRelation['type'] = $relation['type'] === Doctrine_Relation::ONE ? Doctrine_Relation::MANY:Doctrine_Relation::ONE;
|
247 |
}
|
248 |
|
249 |
if( isset($this->relations[$relation['class']]) && is_array($this->relations[$relation['class']]) ) {
|
250 |
foreach($this->relations[$relation['class']] as $otherRelation) {
|
251 |
// skip fully defined m2m relationships
|
252 |
if(isset($otherRelation['refClass']) && $otherRelation['refClass'] == $className) {
|
253 |
continue(2);
|
254 |
}
|
255 |
}
|
256 |
}
|
257 |
|
258 |
$this->relations[$relation['class']][$className] = $newRelation;
|
259 |
}
|
260 |
}
|
261 |
}
|
262 |
} |