~patrix-sbs/oraculum/git

« back to all changes in this revision

Viewing changes to models/doctrine/lib/Doctrine/Migration/Builder.php

  • Committer: Patrick Kaminski
  • Date: 2009-09-02 02:33:07 UTC
  • Revision ID: git-v1:943803254fca67bfb4c0374422b1b836b14dc518
Tags: v0.1a
Sending Oraculum Framework v0.1 alpha

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/*
 
3
 *  $Id: Builder.php 2939 2007-10-19 14:23:42Z Jonathan.Wage $
 
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_Migration_Builder
 
24
 *
 
25
 * @package     Doctrine
 
26
 * @subpackage  Migration
 
27
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 
28
 * @author      Jonathan H. Wage <jwage@mac.com>
 
29
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 
30
 * @link        www.phpdoctrine.org
 
31
 * @since       1.0
 
32
 * @version     $Revision: 2939 $
 
33
 */
 
34
class Doctrine_Migration_Builder extends Doctrine_Builder
 
35
{
 
36
    /**
 
37
     * migrationsPath
 
38
     * 
 
39
     * The path to your migration classes directory
 
40
     *
 
41
     * @var string
 
42
     */
 
43
    private $migrationsPath = '';
 
44
 
 
45
    /**
 
46
     * suffix
 
47
     * 
 
48
     * File suffix to use when writing class definitions
 
49
     *
 
50
     * @var string $suffix
 
51
     */
 
52
    private $suffix = '.class.php';
 
53
 
 
54
    /**
 
55
     * migration
 
56
     *
 
57
     * @var string
 
58
     */
 
59
    private $migration;
 
60
 
 
61
    /**
 
62
     * tpl
 
63
     *
 
64
     * Class template used for writing classes
 
65
     *
 
66
     * @var $tpl
 
67
     */
 
68
    private static $tpl;
 
69
 
 
70
    /**
 
71
     * __construct
 
72
     *
 
73
     * @return void
 
74
     */
 
75
    public function __construct($migrationsPath = null)
 
76
    {
 
77
        if ($migrationsPath) {
 
78
            $this->setMigrationsPath($migrationsPath);
 
79
            $this->migration = new Doctrine_Migration($migrationsPath);
 
80
        }
 
81
        
 
82
        $this->loadTemplate();
 
83
    }
 
84
 
 
85
    /**
 
86
     * setMigrationsPath
 
87
     *
 
88
     * @param string path   the path where migration classes are stored and being generated
 
89
     * @return
 
90
     */
 
91
    public function setMigrationsPath($path)
 
92
    {
 
93
        Doctrine_Lib::makeDirectories($path);
 
94
 
 
95
        $this->migrationsPath = $path;
 
96
    }
 
97
 
 
98
    /**
 
99
     * getMigrationsPath
 
100
     *
 
101
     * @return string       the path where migration classes are stored and being generated
 
102
     */
 
103
    public function getMigrationsPath()
 
104
    {
 
105
        return $this->migrationsPath;
 
106
    }
 
107
 
 
108
    /**
 
109
     * loadTemplate
 
110
     * 
 
111
     * Loads the class template used for generating classes
 
112
     *
 
113
     * @return void
 
114
     */
 
115
    protected function loadTemplate() 
 
116
    {
 
117
        if (isset(self::$tpl)) {
 
118
            return;
 
119
        }
 
120
 
 
121
        self::$tpl =<<<END
 
122
/**
 
123
 * This class has been auto-generated by the Doctrine ORM Framework
 
124
 */
 
125
class %s extends %s
 
126
{
 
127
        public function up()
 
128
        {
 
129
%s
 
130
        }
 
131
 
 
132
        public function down()
 
133
        {
 
134
%s
 
135
        }
 
136
}
 
137
END;
 
138
    }
 
139
 
 
140
    /**
 
141
     * generateMigrationsFromDb
 
142
     *
 
143
     * @return void
 
144
     */
 
145
    public function generateMigrationsFromDb()
 
146
    {
 
147
        $directory = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'tmp_doctrine_models';
 
148
 
 
149
        Doctrine::generateModelsFromDb($directory);
 
150
 
 
151
        $result = $this->generateMigrationsFromModels($directory, Doctrine::MODEL_LOADING_CONSERVATIVE);
 
152
 
 
153
        Doctrine_Lib::removeDirectories($directory);
 
154
 
 
155
        return $result;
 
156
    }
 
157
 
 
158
    /**
 
159
     * generateMigrationsFromModels
 
160
     *
 
161
     * @param string $modelsPath 
 
162
     * @return void
 
163
     */
 
164
    public function generateMigrationsFromModels($modelsPath = null, $modelLoading = null)
 
165
    {
 
166
        if ($modelsPath !== null) {
 
167
            $models = Doctrine::filterInvalidModels(Doctrine::loadModels($modelsPath, $modelLoading));
 
168
        } else {
 
169
            $models = Doctrine::getLoadedModels();
 
170
        }
 
171
 
 
172
        $models = Doctrine::initializeModels($models);
 
173
 
 
174
        $foreignKeys = array();
 
175
        
 
176
        foreach ($models as $model) {
 
177
            $table = Doctrine::getTable($model);
 
178
            if ($table->getTableName() !== $this->migration->getTableName()) {
 
179
                $export = $table->getExportableFormat();
 
180
 
 
181
                $foreignKeys[$export['tableName']] = $export['options']['foreignKeys'];
 
182
 
 
183
                $up = $this->buildCreateTable($export);
 
184
                $down = $this->buildDropTable($export);
 
185
 
 
186
                $className = 'Add' . Doctrine_Inflector::classify($export['tableName']);
 
187
 
 
188
                $this->generateMigrationClass($className, array(), $up, $down);
 
189
            }
 
190
        }
 
191
        
 
192
        if ( ! empty($foreignKeys)) {
 
193
            $className = 'ApplyForeignKeyConstraints';
 
194
        
 
195
            $up = '';
 
196
            $down = '';
 
197
            foreach ($foreignKeys as $tableName => $definitions)    {
 
198
                $tableForeignKeyNames[$tableName] = array();
 
199
            
 
200
                foreach ($definitions as $definition) {
 
201
                    $definition['name'] = $tableName . '_' .implode('_', (array)$definition['local']);
 
202
 
 
203
                    $up .= $this->buildCreateForeignKey($tableName, $definition);
 
204
                    $down .= $this->buildDropForeignKey($tableName, $definition);
 
205
                }
 
206
            }
 
207
        
 
208
            $this->generateMigrationClass($className, array(), $up, $down);
 
209
        }
 
210
        
 
211
        return true;
 
212
    }
 
213
 
 
214
    /**
 
215
     * buildCreateForeignKey
 
216
     *
 
217
     * @param string $tableName 
 
218
     * @param string $definition 
 
219
     * @return void
 
220
     */
 
221
    public function buildCreateForeignKey($tableName, $definition)
 
222
    {
 
223
        return "\t\t\$this->createForeignKey('" . $tableName . "', " . $this->varExport($definition, true) . ");";
 
224
    }
 
225
 
 
226
    /**
 
227
     * buildDropForeignKey
 
228
     *
 
229
     * @param string $tableName 
 
230
     * @param string $definition 
 
231
     * @return void
 
232
     */
 
233
    public function buildDropForeignKey($tableName, $definition)
 
234
    {
 
235
        return "\t\t\$this->dropForeignKey('" . $tableName . "', '" . $definition['name'] . "');\n";
 
236
    }
 
237
 
 
238
    /**
 
239
     * buildCreateTable
 
240
     *
 
241
     * @param string $tableData 
 
242
     * @return void
 
243
     */
 
244
    public function buildCreateTable($tableData)
 
245
    {
 
246
        $code  = "\t\t\$this->createTable('" . $tableData['tableName'] . "', ";
 
247
        
 
248
        $code .= $this->varExport($tableData['columns'], true) . ", ";
 
249
        
 
250
        $code .= $this->varExport(array('indexes' => $tableData['options']['indexes'], 'primary' => $tableData['options']['primary']), true);
 
251
        
 
252
        $code .= ");";
 
253
        
 
254
        return $code;
 
255
    }
 
256
 
 
257
    /**
 
258
     * buildDropTable
 
259
     *
 
260
     * @param string $tableData 
 
261
     * @return string
 
262
     */
 
263
    public function buildDropTable($tableData)
 
264
    {
 
265
        return "\t\t\$this->dropTable('" . $tableData['tableName'] . "');";
 
266
    }
 
267
 
 
268
    /**
 
269
     * generateMigrationClass
 
270
     *
 
271
     * @return void
 
272
     */
 
273
    public function generateMigrationClass($className, $options = array(), $up = null, $down = null, $return = false)
 
274
    {
 
275
        $className = Doctrine_Inflector::urlize($className);
 
276
        $className = str_replace('-', '_', $className);
 
277
        $className = Doctrine_Inflector::classify($className);
 
278
 
 
279
        if ($return || ! $this->getMigrationsPath()) {
 
280
            return $this->buildMigrationClass($className, null, $options, $up, $down);
 
281
        } else {
 
282
            if ( ! $this->getMigrationsPath()) {
 
283
                throw new Doctrine_Migration_Exception('You must specify the path to your migrations.');
 
284
            }
 
285
            
 
286
            $next = (string) $this->migration->getNextVersion();
 
287
            
 
288
            $fileName = str_repeat('0', (3 - strlen($next))) . $next . '_' . Doctrine_Inflector::tableize($className) . $this->suffix;
 
289
            
 
290
            $class = $this->buildMigrationClass($className, $fileName, $options, $up, $down);
 
291
            
 
292
            $path = $this->getMigrationsPath() . DIRECTORY_SEPARATOR . $fileName;
 
293
            
 
294
            if ( class_exists($className) || file_exists($path)) {
 
295
                return false;
 
296
            }
 
297
            
 
298
            file_put_contents($path, $class);
 
299
 
 
300
            return true;
 
301
        }
 
302
    }
 
303
 
 
304
    /**
 
305
     * buildMigrationClass
 
306
     *
 
307
     * @return string
 
308
     */
 
309
    public function buildMigrationClass($className, $fileName = null, $options = array(), $up = null, $down = null)
 
310
    {
 
311
        $extends = isset($options['extends']) ? $options['extends']:'Doctrine_Migration';
 
312
        
 
313
        $content  = '<?php' . PHP_EOL;
 
314
        
 
315
        $content .= sprintf(self::$tpl, $className,
 
316
                                       $extends,
 
317
                                       $up,
 
318
                                       $down);
 
319
        
 
320
        
 
321
        return $content;
 
322
    }
 
323
}
 
 
b'\\ No newline at end of file'