2006-12-29 17:01:31 +03:00
|
|
|
<?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>
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
class Doctrine_Import_Builder
|
|
|
|
{
|
2007-02-10 14:02:52 +03:00
|
|
|
/**
|
|
|
|
* @var string $path the path where imported files are being generated
|
|
|
|
*/
|
2006-12-29 17:01:31 +03:00
|
|
|
private $path = '';
|
|
|
|
|
|
|
|
private $suffix = '.php';
|
|
|
|
|
|
|
|
private static $tpl;
|
|
|
|
|
2006-12-29 17:40:47 +03:00
|
|
|
public function __construct()
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
if ( ! isset(self::$tpl)) {
|
|
|
|
self::$tpl = file_get_contents(Doctrine::getPath()
|
|
|
|
. DIRECTORY_SEPARATOR . 'Doctrine'
|
|
|
|
. DIRECTORY_SEPARATOR . 'Import'
|
|
|
|
. DIRECTORY_SEPARATOR . 'Builder'
|
|
|
|
. DIRECTORY_SEPARATOR . 'Record.tpl');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-02-10 14:02:52 +03:00
|
|
|
* setTargetPath
|
2006-12-29 17:01:31 +03:00
|
|
|
*
|
2007-02-10 14:02:52 +03:00
|
|
|
* @param string path the path where imported files are being generated
|
2006-12-29 17:01:31 +03:00
|
|
|
* @return
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function setTargetPath($path)
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
if ( ! file_exists($path)) {
|
|
|
|
mkdir($path, 0777);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->path = $path;
|
|
|
|
}
|
|
|
|
/**
|
2007-02-10 14:02:52 +03:00
|
|
|
* getTargetPath
|
2006-12-29 17:01:31 +03:00
|
|
|
*
|
2007-02-10 14:02:52 +03:00
|
|
|
* @return string the path where imported files are being generated
|
2006-12-29 17:01:31 +03:00
|
|
|
*/
|
2007-02-10 14:02:52 +03:00
|
|
|
public function getTargetPath()
|
2006-12-29 17:40:47 +03:00
|
|
|
{
|
2007-02-10 14:02:52 +03:00
|
|
|
return $this->path;
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
|
|
|
|
2007-02-10 14:02:52 +03:00
|
|
|
public function buildRecord($table, $tableColumns)
|
2006-12-29 17:40:47 +03:00
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
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');
|
2007-02-10 14:02:52 +03:00
|
|
|
$className = Doctrine::classify($table);
|
2006-12-29 17:01:31 +03:00
|
|
|
$fileName = $this->path . DIRECTORY_SEPARATOR . $className . $this->suffix;
|
|
|
|
$columns = array();
|
|
|
|
$i = 0;
|
|
|
|
|
2007-02-10 14:02:52 +03:00
|
|
|
foreach ($tableColumns as $name => $column) {
|
|
|
|
$columns[$i] = ' $this->hasColumn(\'' . $name . '\', \'' . $column['ptype'][0] . '\'';
|
2006-12-29 17:01:31 +03:00
|
|
|
if ($column['length']) {
|
|
|
|
$columns[$i] .= ', ' . $column['length'];
|
|
|
|
} else {
|
|
|
|
$columns[$i] .= ', null';
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = array();
|
|
|
|
|
|
|
|
if ($column['default']) {
|
|
|
|
$a[] = '\'default\' => ' . var_export($column['default'], true);
|
|
|
|
}
|
|
|
|
if ($column['notnull']) {
|
|
|
|
$a[] = '\'notnull\' => true';
|
|
|
|
}
|
|
|
|
if ($column['primary']) {
|
|
|
|
$a[] = '\'primary\' => true';
|
|
|
|
}
|
|
|
|
if ($column['autoinc']) {
|
|
|
|
$a[] = '\'autoincrement\' => true';
|
|
|
|
}
|
|
|
|
if ($column['unique']) {
|
|
|
|
$a[] = '\'unique\' => true';
|
|
|
|
}
|
2007-02-10 14:02:52 +03:00
|
|
|
if ($column['unsigned']) {
|
|
|
|
$a[] = '\'unsigned\' => true';
|
|
|
|
}
|
2006-12-29 17:01:31 +03:00
|
|
|
|
|
|
|
if ( ! empty($a)) {
|
2007-02-10 14:02:52 +03:00
|
|
|
$columns[$i] .= ', ' . 'array(';
|
|
|
|
$length = strlen($columns[$i]);
|
|
|
|
$columns[$i] .= implode(',
|
|
|
|
' . str_repeat(' ', $length), $a) . ')';
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
|
|
|
$columns[$i] .= ');';
|
|
|
|
|
|
|
|
if ($i < (count($table) - 1)) {
|
|
|
|
$columns[$i] .= '
|
|
|
|
';
|
|
|
|
}
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
2007-02-10 14:02:52 +03:00
|
|
|
$content = sprintf(self::$tpl, $created, $className, implode("\n", $columns));
|
2006-12-29 17:01:31 +03:00
|
|
|
|
|
|
|
$bytes = file_put_contents($fileName, $content);
|
|
|
|
|
2007-02-10 14:02:52 +03:00
|
|
|
if ($bytes === false) {
|
2006-12-29 17:01:31 +03:00
|
|
|
throw new Doctrine_Import_Builder_Exception("Couldn't write file " . $fileName);
|
2007-02-10 14:02:52 +03:00
|
|
|
}
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param Doctrine_Schema_Object $schema
|
|
|
|
* @throws Doctrine_Import_Exception
|
|
|
|
* @return void
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function build(Doctrine_Schema_Object $schema)
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
foreach ($schema->getDatabases() as $database){
|
|
|
|
foreach ($database->getTables() as $table){
|
|
|
|
$this->buildRecord($table);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|