Coverage for Doctrine_Export_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.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.phpdoctrine.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  */
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 !== null) {
48             $loadedModels = Doctrine::filterInvalidModels(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 through the diff of previously declared classes
61         // and currently declared classes
62         foreach ($loadedModels as $className) {
63             if ( ! empty($models) && !in_array($className, $models)) {
64                 continue;
65             }
66
67             $recordTable = Doctrine::getTable($className);
68             
69             $data = $recordTable->getExportableFormat();
70             
71             $table = array();
72             $remove = array('ptype', 'ntype', 'alltypes');
73             // Fix explicit length in schema, concat it to type in this format: type(length)
74             foreach ($data['columns'] AS $name => $column) {
75                 $data['columns'][$name]['type'] = $column['type'] . '(' . $column['length'] . ')';
76                 unset($data['columns'][$name]['length']);
77
78                 // Strip out schema information which is not necessary to be dumped to the yaml schema file
79                 foreach ($remove as $value) {
80                     if (isset($data['columns'][$name][$value])) {
81                         unset($data['columns'][$name][$value]);
82                     }
83                 }
84                 
85                 // If type is the only property of the column then lets abbreviate the syntax
86                 // columns: { name: string(255) }
87                 if (count($data['columns'][$name]) === 1 && isset($data['columns'][$name]['type'])) {
88                     $type = $data['columns'][$name]['type'];
89                     unset($data['columns'][$name]);
90                     $data['columns'][$name] = $type;
91                 }
92             }
93             
94             $table['columns'] = $data['columns'];
95             
96             $relations = $recordTable->getRelations();
97             foreach ($relations as $key => $relation) {
98                 $relationData = $relation->toArray();
99                 
100                 $relationKey = $relationData['alias'];
101                 
102                 if (isset($relationData['refTable']) && $relationData['refTable']) {
103                     $table['relations'][$relationKey]['refClass'] = $relationData['refTable']->getComponentName();
104                 }
105                 
106                 if (isset($relationData['class']) && $relationData['class'] && $relation['class'] != $relationKey) {
107                     $table['relations'][$relationKey]['class'] = $relationData['class'];
108                 }
109  
110                 $table['relations'][$relationKey]['local'] = $relationData['local'];
111                 $table['relations'][$relationKey]['foreign'] = $relationData['foreign'];
112                 
113                 if ($relationData['type'] === Doctrine_Relation::ONE) {
114                     $table['relations'][$relationKey]['type'] = 'one';
115                 } else if($relationData['type'] === Doctrine_Relation::MANY) {
116                     $table['relations'][$relationKey]['type'] = 'many';
117                 } else {
118                     $table['relations'][$relationKey]['type'] = 'one';
119                 }
120             }
121             
122             $array[$className] = $table;
123         }
124         
125         return $array;
126     }
127
128     /**
129      * exportSchema
130      *
131      * @param  string $schema 
132      * @param  string $directory 
133      * @return string $string of data in the specified format
134      * @return void
135      */
136     public function exportSchema($schema, $format = 'yml', $directory = null, $models = array())
137     {
138         $array = $this->buildSchema($directory, $models);
139         
140         if (is_dir($schema)) {
141           $schema = $schema . DIRECTORY_SEPARATOR . 'schema.' . $format;
142         }
143         
144         return Doctrine_Parser::dump($array, $format, $schema);
145     }
146 }