Coverage for Doctrine_Import_Schema

Back to coverage report

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