Builder.php 5.63 KB
Newer Older
lsmith's avatar
lsmith committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
<?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>.
 */

/**
 * Doctrine_Import_Builder
 * Import builder is responsible of building Doctrine ActiveRecord classes
 * based on a database schema.
 *
 * @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>
 * @author      Jukka Hassinen <Jukka.Hassinen@BrainAlliance.com>
 */
lsmith's avatar
lsmith committed
36 37
class Doctrine_Import_Builder
{
zYne's avatar
zYne committed
38 39 40
    /**
     * @var string $path    the path where imported files are being generated
     */
lsmith's avatar
lsmith committed
41 42 43 44 45 46
    private $path = '';

    private $suffix = '.php';

    private static $tpl;

lsmith's avatar
lsmith committed
47 48
    public function __construct()
    {
lsmith's avatar
lsmith committed
49 50 51 52 53 54 55 56 57 58
        if ( ! isset(self::$tpl)) {
            self::$tpl = file_get_contents(Doctrine::getPath()
                       . DIRECTORY_SEPARATOR . 'Doctrine'
                       . DIRECTORY_SEPARATOR . 'Import'
                       . DIRECTORY_SEPARATOR . 'Builder'
                       . DIRECTORY_SEPARATOR . 'Record.tpl');
        }
    }

    /**
zYne's avatar
zYne committed
59
     * setTargetPath
lsmith's avatar
lsmith committed
60
     *
zYne's avatar
zYne committed
61
     * @param string path   the path where imported files are being generated
lsmith's avatar
lsmith committed
62 63
     * @return
     */
lsmith's avatar
lsmith committed
64 65
    public function setTargetPath($path)
    {
lsmith's avatar
lsmith committed
66 67 68 69 70 71 72
        if ( ! file_exists($path)) {
            mkdir($path, 0777);
        }

        $this->path = $path;
    }
    /**
zYne's avatar
zYne committed
73
     * getTargetPath
lsmith's avatar
lsmith committed
74
     *
zYne's avatar
zYne committed
75
     * @return string       the path where imported files are being generated
lsmith's avatar
lsmith committed
76
     */
zYne's avatar
zYne committed
77
    public function getTargetPath()
lsmith's avatar
lsmith committed
78
    {
zYne's avatar
zYne committed
79
        return $this->path;
lsmith's avatar
lsmith committed
80 81
    }

zYne's avatar
zYne committed
82
    public function buildRecord($table, $tableColumns)
lsmith's avatar
lsmith committed
83
    {
lsmith's avatar
lsmith committed
84 85 86 87 88 89 90
        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.');
        }
        $created   = date('l dS \of F Y h:i:s A');
zYne's avatar
zYne committed
91
        $className = Doctrine::classify($table);
lsmith's avatar
lsmith committed
92 93 94 95
        $fileName  = $this->path . DIRECTORY_SEPARATOR . $className . $this->suffix;
        $columns   = array();
        $i = 0;

zYne's avatar
zYne committed
96 97
        foreach ($tableColumns as $name => $column) {
            $columns[$i] = '        $this->hasColumn(\'' . $name . '\', \'' . $column['ptype'][0] . '\'';
lsmith's avatar
lsmith committed
98 99 100 101 102 103 104 105
            if ($column['length']) {
                $columns[$i] .= ', ' . $column['length'];
            } else {
                $columns[$i] .= ', null';
            }

            $a = array();

zYne's avatar
zYne committed
106
            if (isset($column['default']) && $column['default']) {
lsmith's avatar
lsmith committed
107 108
                $a[] = '\'default\' => ' . var_export($column['default'], true);
            }
zYne's avatar
zYne committed
109
            if (isset($column['notnull']) && $column['notnull']) {
lsmith's avatar
lsmith committed
110 111
                $a[] = '\'notnull\' => true';
            }
zYne's avatar
zYne committed
112
            if (isset($column['primary']) && $column['primary']) {
lsmith's avatar
lsmith committed
113 114
                $a[] = '\'primary\' => true';
            }
zYne's avatar
zYne committed
115
            if (isset($column['autoinc']) && $column['autoinc']) {
lsmith's avatar
lsmith committed
116 117
                $a[] = '\'autoincrement\' => true';
            }
zYne's avatar
zYne committed
118
            if (isset($column['unique']) && $column['unique']) {
lsmith's avatar
lsmith committed
119 120
                $a[] = '\'unique\' => true';
            }
zYne's avatar
zYne committed
121
            if (isset($column['unsigned']) && $column['unsigned']) {
zYne's avatar
zYne committed
122 123
                $a[] = '\'unsigned\' => true';
            }
zYne's avatar
zYne committed
124 125 126
            if ($column['ptype'][0] == 'enum' && isset($column['values']) && $column['values']) {
                $a[] = '\'values\' => array(' . implode(',', $column['values']) . ')';
            }
lsmith's avatar
lsmith committed
127 128

            if ( ! empty($a)) {
zYne's avatar
zYne committed
129 130 131 132
                $columns[$i] .= ', ' . 'array(';
                $length = strlen($columns[$i]);
                $columns[$i] .= implode(', 
' . str_repeat(' ', $length), $a) . ')';
lsmith's avatar
lsmith committed
133 134 135 136 137 138 139 140 141 142
            }
            $columns[$i] .= ');';

            if ($i < (count($table) - 1)) {
                $columns[$i] .= '
';
            }
            $i++;
        }

zYne's avatar
zYne committed
143
        $content = sprintf(self::$tpl, $created, $className, implode("\n", $columns));
lsmith's avatar
lsmith committed
144

zYne's avatar
zYne committed
145
        $bytes   = file_put_contents($fileName, $content);
lsmith's avatar
lsmith committed
146

zYne's avatar
zYne committed
147
        if ($bytes === false) {
lsmith's avatar
lsmith committed
148
            throw new Doctrine_Import_Builder_Exception("Couldn't write file " . $fileName);
zYne's avatar
zYne committed
149
        }
lsmith's avatar
lsmith committed
150 151 152 153 154 155 156
    }
    /**
     *
     * @param Doctrine_Schema_Object $schema
     * @throws Doctrine_Import_Exception
     * @return void
     */
lsmith's avatar
lsmith committed
157 158
    public function build(Doctrine_Schema_Object $schema)
    {
lsmith's avatar
lsmith committed
159 160 161 162 163 164 165 166
        foreach ($schema->getDatabases() as $database){
            foreach ($database->getTables() as $table){
                $this->buildRecord($table);
            }
        }
    }

}