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
|
2007-10-04 01:43:22 +04:00
|
|
|
* @subpackage Import
|
2006-12-29 17:01:31 +03:00
|
|
|
* @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>
|
2007-06-26 10:03:15 +04:00
|
|
|
* @author Nicolas Bérard-Nault <nicobn@php.net>
|
2006-12-29 17:01:31 +03:00
|
|
|
*/
|
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';
|
2007-10-05 03:55:37 +04:00
|
|
|
|
|
|
|
private $generateBaseClasses = false;
|
|
|
|
|
|
|
|
private $baseClassesDirectory = 'generated';
|
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
private static $tpl;
|
2007-10-05 03:55:37 +04:00
|
|
|
|
2006-12-29 17:40:47 +03:00
|
|
|
public function __construct()
|
|
|
|
{
|
2007-06-26 10:03:15 +04:00
|
|
|
$this->loadTemplate();
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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-10-05 03:55:37 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* generateBaseClasses
|
|
|
|
*
|
|
|
|
* Specify whether or not to generate classes which extend from generated base classes
|
|
|
|
*
|
|
|
|
* @param string $bool
|
|
|
|
* @return void
|
|
|
|
* @author Jonathan H. Wage
|
|
|
|
*/
|
|
|
|
public function generateBaseClasses($bool = null)
|
|
|
|
{
|
|
|
|
if ($bool !== null) {
|
|
|
|
$this->generateBaseClasses = $bool;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->generateBaseClasses;
|
|
|
|
}
|
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
/**
|
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-06-26 10:03:15 +04:00
|
|
|
/**
|
|
|
|
* This is a template that was previously in Builder/Record.tpl. Due to the fact
|
|
|
|
* that it was not bundled when compiling, it had to be moved here.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function loadTemplate()
|
2006-12-29 17:40:47 +03:00
|
|
|
{
|
2007-06-26 10:03:15 +04:00
|
|
|
if (isset(self::$tpl)) {
|
|
|
|
return;
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2007-06-26 10:03:15 +04:00
|
|
|
|
|
|
|
self::$tpl =<<<END
|
|
|
|
/**
|
|
|
|
* This class has been auto-generated by the Doctrine ORM Framework
|
|
|
|
*/
|
2007-09-22 00:30:10 +04:00
|
|
|
class %s extends %s
|
2007-06-26 10:03:15 +04:00
|
|
|
{
|
|
|
|
%s
|
2007-07-11 18:39:15 +04:00
|
|
|
%s
|
2007-06-26 10:03:15 +04:00
|
|
|
}
|
|
|
|
END;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-06-27 06:44:20 +04:00
|
|
|
/*
|
|
|
|
* Build the table definition of a Doctrine_Record object
|
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
* @param array $tableColumns
|
|
|
|
*/
|
2007-09-22 00:30:10 +04:00
|
|
|
public function buildTableDefinition(array $options, array $columns, array $relations)
|
2007-06-26 10:03:15 +04:00
|
|
|
{
|
2007-09-22 00:30:10 +04:00
|
|
|
$ret = array();
|
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
|
2007-10-05 03:55:37 +04:00
|
|
|
$ret[$i] = "\t\tpublic function setTableDefinition()\n\t\t{";
|
|
|
|
$i++;
|
|
|
|
|
2007-09-22 00:30:10 +04:00
|
|
|
if (isset($options['inheritance']) && isset($options['inheritance']['extends'])) {
|
|
|
|
$ret[$i] = "\t\tparent::setTableDefinition();";
|
|
|
|
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['tableName']) && !empty($options['tableName'])) {
|
|
|
|
$ret[$i] = str_repeat(' ', 8) . '$this->setTableName(\''. $options['tableName'].'\');';
|
|
|
|
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($columns as $name => $column) {
|
|
|
|
$ret[$i] = ' $this->hasColumn(\'' . $name . '\', \'' . $column['type'] . '\'';
|
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if ($column['length']) {
|
2007-09-22 00:30:10 +04:00
|
|
|
$ret[$i] .= ', ' . $column['length'];
|
2006-12-29 17:01:31 +03:00
|
|
|
} else {
|
2007-09-22 00:30:10 +04:00
|
|
|
$ret[$i] .= ', null';
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$a = array();
|
|
|
|
|
2007-03-27 00:52:34 +04:00
|
|
|
if (isset($column['default']) && $column['default']) {
|
2006-12-29 17:01:31 +03:00
|
|
|
$a[] = '\'default\' => ' . var_export($column['default'], true);
|
|
|
|
}
|
2007-03-27 00:52:34 +04:00
|
|
|
if (isset($column['notnull']) && $column['notnull']) {
|
2006-12-29 17:01:31 +03:00
|
|
|
$a[] = '\'notnull\' => true';
|
|
|
|
}
|
2007-03-27 00:52:34 +04:00
|
|
|
if (isset($column['primary']) && $column['primary']) {
|
2006-12-29 17:01:31 +03:00
|
|
|
$a[] = '\'primary\' => true';
|
|
|
|
}
|
2007-10-05 03:55:37 +04:00
|
|
|
if ((isset($column['autoinc']) && $column['autoinc']) || isset($column['autoincrement']) && $column['autoincrement']) {
|
2006-12-29 17:01:31 +03:00
|
|
|
$a[] = '\'autoincrement\' => true';
|
|
|
|
}
|
2007-03-27 00:52:34 +04:00
|
|
|
if (isset($column['unique']) && $column['unique']) {
|
2006-12-29 17:01:31 +03:00
|
|
|
$a[] = '\'unique\' => true';
|
|
|
|
}
|
2007-03-27 00:52:34 +04:00
|
|
|
if (isset($column['unsigned']) && $column['unsigned']) {
|
2007-02-10 14:02:52 +03:00
|
|
|
$a[] = '\'unsigned\' => true';
|
|
|
|
}
|
2007-09-21 11:14:37 +04:00
|
|
|
if ($column['type'] == 'enum' && isset($column['values']) ) {
|
|
|
|
$a[] = '\'values\' => array(\'' . implode('\',\'', $column['values']) . '\')';
|
2007-04-11 22:35:15 +04:00
|
|
|
}
|
2006-12-29 17:01:31 +03:00
|
|
|
|
|
|
|
if ( ! empty($a)) {
|
2007-09-22 00:30:10 +04:00
|
|
|
$ret[$i] .= ', ' . 'array(';
|
|
|
|
$length = strlen($ret[$i]);
|
|
|
|
$ret[$i] .= implode(',' . PHP_EOL . str_repeat(' ', $length), $a) . ')';
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2007-09-22 00:30:10 +04:00
|
|
|
|
|
|
|
$ret[$i] .= ');';
|
2006-12-29 17:01:31 +03:00
|
|
|
|
2007-09-22 00:30:10 +04:00
|
|
|
if ($i < (count($columns) - 1)) {
|
|
|
|
$ret[$i] .= PHP_EOL;
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
|
|
|
$i++;
|
|
|
|
}
|
2007-06-27 06:44:20 +04:00
|
|
|
|
2007-10-05 03:55:37 +04:00
|
|
|
return implode("\n", $ret)."\n\t\t}";
|
2007-06-27 06:44:20 +04:00
|
|
|
}
|
2007-09-22 00:30:10 +04:00
|
|
|
public function buildSetUp(array $options, array $columns, array $relations)
|
2007-06-27 06:44:20 +04:00
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
$ret = array();
|
2007-09-22 00:30:10 +04:00
|
|
|
|
2007-10-05 03:55:37 +04:00
|
|
|
$i = 0;
|
2007-09-22 00:30:10 +04:00
|
|
|
|
2007-10-05 03:55:37 +04:00
|
|
|
$ret[$i] = "\t\tpublic function setUp()\n\t\t{";
|
|
|
|
$i++;
|
|
|
|
|
|
|
|
if (isset($options['inheritance']) && isset($options['inheritance']['extends'])) {
|
|
|
|
$ret[$i] = "\t\tparent::setUp();";
|
|
|
|
$i++;
|
2007-09-22 00:30:10 +04:00
|
|
|
}
|
2007-09-14 23:14:40 +04:00
|
|
|
|
2007-07-11 18:39:15 +04:00
|
|
|
foreach ($relations as $name => $relation) {
|
|
|
|
$alias = (isset($relation['alias']) && $relation['alias'] !== $name) ? ' as ' . $relation['alias'] : '';
|
|
|
|
|
|
|
|
if ( ! isset($relation['type'])) {
|
|
|
|
$relation['type'] = Doctrine_Relation::ONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($relation['type'] === Doctrine_Relation::ONE ||
|
|
|
|
$relation['type'] === Doctrine_Relation::ONE_COMPOSITE) {
|
|
|
|
$ret[$i] = ' $this->hasOne(\'' . $name . $alias . '\'';
|
|
|
|
} else {
|
|
|
|
$ret[$i] = ' $this->hasMany(\'' . $name . $alias . '\'';
|
|
|
|
}
|
2007-09-14 23:14:40 +04:00
|
|
|
|
2007-07-11 18:39:15 +04:00
|
|
|
$a = array();
|
|
|
|
|
2007-09-14 23:14:40 +04:00
|
|
|
if (isset($relation['refClass'])) {
|
|
|
|
$a[] = '\'refClass\' => ' . var_export($relation['refClass'], true);
|
|
|
|
}
|
|
|
|
|
2007-07-11 18:39:15 +04:00
|
|
|
if (isset($relation['deferred']) && $relation['deferred']) {
|
|
|
|
$a[] = '\'default\' => ' . var_export($relation['deferred'], true);
|
|
|
|
}
|
2007-09-14 23:14:40 +04:00
|
|
|
|
2007-07-11 18:39:15 +04:00
|
|
|
if (isset($relation['local']) && $relation['local']) {
|
|
|
|
$a[] = '\'local\' => ' . var_export($relation['local'], true);
|
|
|
|
}
|
2007-09-14 23:14:40 +04:00
|
|
|
|
2007-07-11 18:39:15 +04:00
|
|
|
if (isset($relation['foreign']) && $relation['foreign']) {
|
|
|
|
$a[] = '\'foreign\' => ' . var_export($relation['foreign'], true);
|
|
|
|
}
|
2007-09-14 23:14:40 +04:00
|
|
|
|
2007-07-11 18:39:15 +04:00
|
|
|
if (isset($relation['onDelete']) && $relation['onDelete']) {
|
|
|
|
$a[] = '\'onDelete\' => ' . var_export($relation['onDelete'], true);
|
|
|
|
}
|
2007-09-14 23:14:40 +04:00
|
|
|
|
2007-07-11 18:39:15 +04:00
|
|
|
if (isset($relation['onUpdate']) && $relation['onUpdate']) {
|
|
|
|
$a[] = '\'onUpdate\' => ' . var_export($relation['onUpdate'], true);
|
|
|
|
}
|
2007-09-14 23:14:40 +04:00
|
|
|
|
2007-07-11 18:39:15 +04:00
|
|
|
if ( ! empty($a)) {
|
|
|
|
$ret[$i] .= ', ' . 'array(';
|
|
|
|
$length = strlen($ret[$i]);
|
|
|
|
$ret[$i] .= implode(',' . PHP_EOL . str_repeat(' ', $length), $a) . ')';
|
|
|
|
}
|
2007-09-14 23:14:40 +04:00
|
|
|
|
2007-07-11 18:39:15 +04:00
|
|
|
$ret[$i] .= ');';
|
|
|
|
$i++;
|
2007-07-10 19:20:13 +04:00
|
|
|
}
|
2007-09-14 23:14:40 +04:00
|
|
|
|
2007-09-22 00:30:10 +04:00
|
|
|
if (isset($options['inheritance']['keyField']) && isset($options['inheritance']['keyValue'])) {
|
|
|
|
$i++;
|
|
|
|
$ret[$i] = "\t\t".'$this->setInheritanceMap(array(\''.$options['inheritance']['keyField'].'\' => '.$options['inheritance']['keyValue'].'));';
|
|
|
|
}
|
|
|
|
|
2007-10-05 03:55:37 +04:00
|
|
|
return implode("\n", $ret)."\n\t\t}";
|
2007-07-11 18:39:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function buildDefinition(array $options, array $columns, array $relations = array())
|
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
if ( ! isset($options['className'])) {
|
|
|
|
throw new Doctrine_Import_Builder_Exception('Missing class name.');
|
|
|
|
}
|
2007-09-21 11:14:37 +04:00
|
|
|
|
2007-09-22 00:30:10 +04:00
|
|
|
$className = $options['className'];
|
|
|
|
$extends = isset($options['inheritance']['extends']) ? $options['inheritance']['extends']:'Doctrine_Record';
|
|
|
|
|
2007-10-05 03:55:37 +04:00
|
|
|
if (!isset($options['inheritance']['extends'])) {
|
|
|
|
$definition = $this->buildTableDefinition($options, $columns, $relations);
|
|
|
|
$setUp = $this->buildSetUp($options, $columns, $relations);
|
|
|
|
} else {
|
|
|
|
$definition = null;
|
|
|
|
$setUp = null;
|
|
|
|
}
|
|
|
|
|
2007-09-22 00:30:10 +04:00
|
|
|
$content = sprintf(self::$tpl, $className,
|
|
|
|
$extends,
|
2007-10-05 03:55:37 +04:00
|
|
|
$definition,
|
|
|
|
$setUp);
|
|
|
|
|
2007-07-11 18:39:15 +04:00
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
2007-09-22 00:30:10 +04:00
|
|
|
public function buildRecord(array $options, array $columns, array $relations = array())
|
2007-07-11 18:39:15 +04:00
|
|
|
{
|
2007-10-05 03:55:37 +04:00
|
|
|
if ( !isset($options['className'])) {
|
2007-09-03 18:57:18 +04:00
|
|
|
throw new Doctrine_Import_Builder_Exception('Missing class name.');
|
|
|
|
}
|
2007-07-24 00:28:46 +04:00
|
|
|
|
2007-10-05 03:55:37 +04:00
|
|
|
if ( !isset($options['fileName'])) {
|
2007-06-27 06:44:20 +04:00
|
|
|
if (empty($this->path)) {
|
2007-10-05 03:55:37 +04:00
|
|
|
throw new Doctrine_Import_Builder_Exception('No build target directory set.');
|
2007-06-27 06:44:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (is_writable($this->path) === false) {
|
2007-10-05 03:55:37 +04:00
|
|
|
throw new Doctrine_Import_Builder_Exception('Build target directory ' . $this->path . ' is not writable.');
|
2007-06-27 06:44:20 +04:00
|
|
|
}
|
|
|
|
|
2007-07-24 00:28:46 +04:00
|
|
|
$options['fileName'] = $this->path . DIRECTORY_SEPARATOR . $options['className'] . $this->suffix;
|
2007-06-27 06:44:20 +04:00
|
|
|
}
|
2007-10-05 03:55:37 +04:00
|
|
|
|
|
|
|
if ($this->generateBaseClasses()) {
|
|
|
|
|
|
|
|
$optionsBak = $options;
|
|
|
|
|
|
|
|
unset($options['tableName']);
|
|
|
|
$options['inheritance']['extends'] = 'Base' . $options['className'];
|
|
|
|
$this->writeDefinition($options, array(), array());
|
|
|
|
|
|
|
|
$options = $optionsBak;
|
|
|
|
|
|
|
|
$generatedPath = $this->path . DIRECTORY_SEPARATOR . $this->baseClassesDirectory;
|
|
|
|
|
|
|
|
if (!file_exists($generatedPath)) {
|
|
|
|
mkdir($generatedPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
$options['className'] = 'Base' . $options['className'];
|
|
|
|
$options['fileName'] = $generatedPath . DIRECTORY_SEPARATOR . $options['className'] . $this->suffix;
|
|
|
|
|
|
|
|
$this->writeDefinition($options, $columns, $relations);
|
|
|
|
} else {
|
|
|
|
$this->writeDefinition($options, $columns, $relations);
|
2007-02-10 14:02:52 +03:00
|
|
|
}
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2007-10-05 03:55:37 +04:00
|
|
|
|
|
|
|
public function writeDefinition(array $options, array $columns, array $relations = array())
|
|
|
|
{
|
|
|
|
$content = $this->buildDefinition($options, $columns, $relations);
|
|
|
|
|
|
|
|
$bytes = file_put_contents($options['fileName'], '<?php' . PHP_EOL . $content);
|
|
|
|
|
|
|
|
if ($bytes === false) {
|
|
|
|
throw new Doctrine_Import_Builder_Exception("Couldn't write file " . $options['fileName']);
|
|
|
|
}
|
|
|
|
}
|
2007-10-04 01:43:22 +04:00
|
|
|
}
|