# 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) {
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 $className) {
63 | if ( ! empty($models) && !in_array($className, $models)) {
64 | continue;
65 | }
66 |
67 | $record = new $className();
68 | $recordTable = $record->getTable();
69 |
70 | $data = $recordTable->getExportableFormat();
71 |
72 | $table = array();
73 |
74 | foreach ($data['columns'] AS $name => $column) {
75 | $data['columns'][$name]['type'] = $column['type'] . '(' . $column['length'] . ')';
76 | unset($data['columns'][$name]['length']);
77 | }
78 |
79 | $table['columns'] = $data['columns'];
80 |
81 | $relations = $recordTable->getRelations();
82 | foreach ($relations as $key => $relation) {
83 | $relationData = $relation->toArray();
84 |
85 | $relationKey = $relationData['alias'];
86 |
87 | if (isset($relationData['refTable']) && $relationData['refTable']) {
88 | $table['relations'][$relationKey]['refClass'] = $relationData['refTable']->getComponentName();
89 | }
90 |
91 | if (isset($relationData['class']) && $relationData['class'] && $relation['class'] != $relationKey) {
92 | $table['relations'][$relationKey]['class'] = $relationData['class'];
93 | }
94 |
95 | $table['relations'][$relationKey]['local'] = $relationData['local'];
96 | $table['relations'][$relationKey]['foreign'] = $relationData['foreign'];
97 |
98 | if ($relationData['type'] === Doctrine_Relation::ONE) {
99 | $table['relations'][$relationKey]['type'] = 'one';
100 | } else if($relationData['type'] === Doctrine_Relation::MANY) {
101 | $table['relations'][$relationKey]['type'] = 'many';
102 | } else {
103 | $table['relations'][$relationKey]['type'] = 'one';
104 | }
105 | }
106 |
107 | $array[$className] = $table;
108 | }
109 |
110 | return $array;
111 | }
112 |
113 | /**
114 | * exportSchema
115 | *
116 | * @param string $schema
117 | * @param string $directory
118 | * @return string $string of data in the specified format
119 | * @return void
120 | */
121 | public function exportSchema($schema, $format = 'yml', $directory = null, $models = array())
122 | {
123 | $array = $this->buildSchema($directory, $models);
124 |
125 | if (is_dir($schema)) {
126 | $schema = $schema . DIRECTORY_SEPARATOR . 'schema.' . $format;
127 | }
128 |
129 | return Doctrine_Parser::dump($array, $format, $schema);
130 | }
131 | }