Builder.php 4.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.com>.
 */

/**
zYne's avatar
zYne committed
23 24 25 26
 * Doctrine_Import_Builder
 * Import builder is responsible of building Doctrine ActiveRecord classes 
 * based on a database schema.
 *
27 28 29 30 31 32 33
 * @package     Doctrine
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @since       1.0
 * @version     $Revision$
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
zYne's avatar
zYne committed
34
 * @author      Jukka Hassinen <Jukka.Hassinen@BrainAlliance.com>
35
 */
36 37 38 39 40
class Doctrine_Import_Builder {
    
    private $path = '';
    
    private $suffix = '.php';
41

42
    private static $tpl;
43

44 45 46 47 48 49 50 51
    public function __construct() {
        if( ! isset(self::$tpl))
            self::$tpl = file_get_contents(Doctrine::getPath()
                       . DIRECTORY_SEPARATOR . 'Doctrine'
                       . DIRECTORY_SEPARATOR . 'Import'
                       . DIRECTORY_SEPARATOR . 'Builder'
                       . DIRECTORY_SEPARATOR . 'Record.tpl');
    }
52 53 54

    /**
     *
55
     * @param string path
56 57 58
     * @return 
     * @access public
     */
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    public function setTargetPath($path) {
        if( ! file_exists($path)) {
            mkdir($path, 0777);
        }

        $this->path = $path;
    }
    public function getTargetPath() {
        return $this->path;
    }
    /**
     *
     * @param string path
     * @return
     * @access public
     */
    public function setFileSuffix($suffix) {
        $this->suffix = $suffix;
    }
    public function getFileSuffix() {
        return $this->suffix;
    }

82

83 84 85 86 87 88
    public function buildRecord(Doctrine_Schema_Table $table) {
        if (empty($this->path)) 
            throw new Doctrine_Import_Builder_Exception('No build target directory set.');

        if (is_writable($this->path) === false) 
            throw new Doctrine_Import_Builder_Exception('Build target directory ' . $this->path . ' is not writable.');
89

90 91 92 93
        $created   = date('l dS \of F Y h:i:s A');
        $className = Doctrine::classify($table->get('name'));
        $fileName  = $this->path . DIRECTORY_SEPARATOR . $className . $this->suffix;
        $columns   = array();
94

95
        $i = 0;
96

97
        foreach($table as $name => $column) {
98

99 100 101
            $columns[$i] = '        $this->hasColumn(\'' . $column['name'] . '\', \'' . $column['type'] . '\'';
            if($column['length'])
                $columns[$i] .= ', ' . $column['length'];
102 103 104 105 106 107 108 109 110 111 112
            else
                $columns[$i] .= ', null';
           
            $a = array();
            
            if($column['default']) {
                $a[] = '\'default\' => ' . var_export($column['default'], true);
            }
            if($column['notnull']) {
                $a[] = '\'notnull\' => true';
            }
zYne's avatar
zYne committed
113
            if($column['primary']) {
114 115
                $a[] = '\'primary\' => true';
            }
zYne's avatar
zYne committed
116
            if($column['autoinc']) {
117 118
                $a[] = '\'autoincrement\' => true';
            }
zYne's avatar
zYne committed
119
            if($column['unique']) {
120 121 122
                $a[] = '\'unique\' => true';
            }

123 124 125
            if( ! empty($a))
                $columns[$i] .= ', ' . 'array(' . implode(',
', $a) . ')';
126

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
            $columns[$i] .= ');';
            
            if($i < (count($table) - 1))
                $columns[$i] .= '
';
            $i++;
        }

        $content   = sprintf(self::$tpl, $created, $className, implode('', $columns));

        $bytes     = file_put_contents($fileName, $content);


        if($bytes === false)
            throw new Doctrine_Import_Builder_Exception("Couldn't write file " . $fileName);
    }
    /**
     *
     * @param Doctrine_Schema_Object $schema
     * @return
     * @access public
     * @throws Doctrine_Import_Exception
     */
    public function build(Doctrine_Schema_Object $schema) {
151 152 153 154 155
	foreach($schema->getDatabases() as $database){
		foreach($database->getTables() as $table){
			$this->buildRecord($table);
		}
	}
156
    }
157

158
}