2007-10-14 10:44:49 +04: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
|
2007-10-21 10:23:59 +04:00
|
|
|
*
|
|
|
|
* Import builder is responsible of building Doctrine_Record classes
|
2007-10-14 10:44:49 +04:00
|
|
|
* based on a database schema.
|
|
|
|
*
|
|
|
|
* @package Doctrine
|
|
|
|
* @subpackage Import
|
|
|
|
* @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>
|
|
|
|
* @author Nicolas Bérard-Nault <nicobn@php.net>
|
2007-10-21 10:23:59 +04:00
|
|
|
* @author Jonathan H. Wage <jwage@mac.com>
|
2007-10-14 10:44:49 +04:00
|
|
|
*/
|
|
|
|
class Doctrine_Import_Builder
|
|
|
|
{
|
|
|
|
/**
|
2007-10-15 18:18:31 +04:00
|
|
|
* Path
|
|
|
|
*
|
|
|
|
* the path where imported files are being generated
|
|
|
|
*
|
2007-10-26 08:13:29 +04:00
|
|
|
* @var string $_path
|
2007-10-14 10:44:49 +04:00
|
|
|
*/
|
2007-10-26 08:13:29 +04:00
|
|
|
protected $_path = '';
|
2007-10-23 07:50:45 +04:00
|
|
|
|
2007-10-25 01:21:45 +04:00
|
|
|
/**
|
|
|
|
* packagesPrefix
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2007-10-26 08:13:29 +04:00
|
|
|
protected $_packagesPrefix = 'Package';
|
2007-10-25 01:21:45 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* packagesPath
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2007-10-26 08:13:29 +04:00
|
|
|
protected $_packagesPath = '';
|
2007-10-23 07:50:45 +04:00
|
|
|
|
2007-10-15 18:18:31 +04:00
|
|
|
/**
|
|
|
|
* suffix
|
|
|
|
*
|
|
|
|
* File suffix to use when writing class definitions
|
|
|
|
*
|
|
|
|
* @var string $suffix
|
|
|
|
*/
|
2007-11-02 02:54:27 +03:00
|
|
|
protected $_suffix = '.php';
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-10-15 18:18:31 +04:00
|
|
|
/**
|
|
|
|
* generateBaseClasses
|
|
|
|
*
|
|
|
|
* Bool true/false for whether or not to generate base classes
|
|
|
|
*
|
|
|
|
* @var string $suffix
|
|
|
|
*/
|
2007-10-26 08:13:29 +04:00
|
|
|
protected $_generateBaseClasses = true;
|
2007-10-25 01:21:45 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* generateTableClasses
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2007-10-26 08:13:29 +04:00
|
|
|
protected $_generateTableClasses = true;
|
2007-10-25 01:21:45 +04:00
|
|
|
|
2007-10-15 18:18:31 +04:00
|
|
|
/**
|
|
|
|
* baseClassesDirectory
|
|
|
|
*
|
|
|
|
* Directory to put the generate base classes in
|
|
|
|
*
|
|
|
|
* @var string $suffix
|
|
|
|
*/
|
2007-10-26 08:13:29 +04:00
|
|
|
protected $_baseClassesDirectory = 'generated';
|
2007-10-25 19:22:34 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* baseClassName
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2007-10-26 08:13:29 +04:00
|
|
|
protected $_baseClassName = 'Doctrine_Record';
|
2007-10-25 19:22:34 +04:00
|
|
|
|
2007-10-15 18:18:31 +04:00
|
|
|
/**
|
|
|
|
* tpl
|
|
|
|
*
|
|
|
|
* Class template used for writing classes
|
|
|
|
*
|
2007-10-26 08:13:29 +04:00
|
|
|
* @var $_tpl
|
2007-10-15 18:18:31 +04:00
|
|
|
*/
|
2007-10-26 08:13:29 +04:00
|
|
|
protected static $_tpl;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-10-15 18:18:31 +04:00
|
|
|
/**
|
|
|
|
* __construct
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-10-14 10:44:49 +04:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->loadTemplate();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* setTargetPath
|
|
|
|
*
|
|
|
|
* @param string path the path where imported files are being generated
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public function setTargetPath($path)
|
|
|
|
{
|
2007-10-25 01:21:45 +04:00
|
|
|
Doctrine::makeDirectories($path);
|
|
|
|
|
2007-11-10 22:54:34 +03:00
|
|
|
if ( ! $this->_packagesPath) {
|
|
|
|
$this->setPackagesPath($path . DIRECTORY_SEPARATOR . 'packages');
|
2007-10-14 10:44:49 +04:00
|
|
|
}
|
|
|
|
|
2007-10-26 08:13:29 +04:00
|
|
|
$this->_path = $path;
|
2007-10-14 10:44:49 +04:00
|
|
|
}
|
2007-10-25 01:21:45 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* setPackagePath
|
|
|
|
*
|
2007-10-25 19:22:34 +04:00
|
|
|
* @param string $packagesPrefix
|
2007-10-25 01:21:45 +04:00
|
|
|
* @return void
|
|
|
|
*/
|
2007-10-25 19:22:34 +04:00
|
|
|
public function setPackagesPrefix($packagesPrefix)
|
2007-10-25 01:21:45 +04:00
|
|
|
{
|
2007-10-26 08:13:29 +04:00
|
|
|
$this->_packagesPrefix = $packagesPrefix;
|
2007-10-25 01:21:45 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-10-25 01:21:45 +04:00
|
|
|
/**
|
|
|
|
* setPackagesPath
|
|
|
|
*
|
|
|
|
* @param string $packagesPath
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setPackagesPath($packagesPath)
|
|
|
|
{
|
2007-11-10 22:54:34 +03:00
|
|
|
Doctrine::makeDirectories($packagesPath);
|
|
|
|
|
2007-10-26 08:13:29 +04:00
|
|
|
$this->_packagesPath = $packagesPath;
|
2007-10-25 01:21:45 +04:00
|
|
|
}
|
|
|
|
|
2007-10-14 10:44:49 +04:00
|
|
|
/**
|
|
|
|
* generateBaseClasses
|
|
|
|
*
|
|
|
|
* Specify whether or not to generate classes which extend from generated base classes
|
|
|
|
*
|
|
|
|
* @param string $bool
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function generateBaseClasses($bool = null)
|
|
|
|
{
|
2007-10-25 01:21:45 +04:00
|
|
|
if ($bool !== null) {
|
2007-10-26 08:13:29 +04:00
|
|
|
$this->_generateBaseClasses = $bool;
|
2007-10-25 01:21:45 +04:00
|
|
|
}
|
|
|
|
|
2007-10-26 08:13:29 +04:00
|
|
|
return $this->_generateBaseClasses;
|
2007-10-25 01:21:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* generateTableClasses
|
|
|
|
*
|
|
|
|
* Specify whether or not to generate table classes which extend from Doctrine_Table
|
|
|
|
*
|
|
|
|
* @param string $bool
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function generateTableClasses($bool = null)
|
|
|
|
{
|
|
|
|
if ($bool !== null) {
|
2007-10-26 08:13:29 +04:00
|
|
|
$this->_generateTableClasses = $bool;
|
2007-10-25 01:21:45 +04:00
|
|
|
}
|
|
|
|
|
2007-10-26 08:13:29 +04:00
|
|
|
return $this->_generateTableClasses;
|
2007-10-25 01:21:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* setBaseClassesDirectory
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setBaseClassesDirectory($baseClassesDirectory)
|
|
|
|
{
|
2007-10-26 08:13:29 +04:00
|
|
|
$this->_baseClassesDirectory;
|
2007-10-25 01:21:45 +04:00
|
|
|
}
|
|
|
|
|
2007-10-25 19:22:34 +04:00
|
|
|
/**
|
|
|
|
* setBaseClassName
|
|
|
|
*
|
|
|
|
* @package default
|
|
|
|
*/
|
|
|
|
public function setBaseClassName($className)
|
|
|
|
{
|
2007-10-26 08:13:29 +04:00
|
|
|
$this->_baseClassName = $className;
|
2007-10-25 19:22:34 +04:00
|
|
|
}
|
|
|
|
|
2007-10-25 01:21:45 +04:00
|
|
|
/**
|
|
|
|
* setSuffix
|
|
|
|
*
|
|
|
|
* @param string $suffix
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setSuffix($suffix)
|
|
|
|
{
|
2007-10-26 08:13:29 +04:00
|
|
|
$this->_suffix = $suffix;
|
2007-10-14 10:44:49 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-10-14 10:44:49 +04:00
|
|
|
/**
|
|
|
|
* getTargetPath
|
|
|
|
*
|
|
|
|
* @return string the path where imported files are being generated
|
|
|
|
*/
|
|
|
|
public function getTargetPath()
|
|
|
|
{
|
2007-10-26 08:13:29 +04:00
|
|
|
return $this->_path;
|
2007-10-14 10:44:49 +04:00
|
|
|
}
|
|
|
|
|
2007-11-02 02:54:27 +03:00
|
|
|
/**
|
|
|
|
* setOptions
|
|
|
|
*
|
|
|
|
* @param string $options
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setOptions($options)
|
|
|
|
{
|
|
|
|
if (!empty($options)) {
|
|
|
|
foreach ($options as $key => $value) {
|
|
|
|
$this->setOption($key, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* setOption
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @param string $value
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setOption($key, $value)
|
|
|
|
{
|
2007-11-10 22:54:34 +03:00
|
|
|
$name = 'set' . Doctrine::classify($key);
|
|
|
|
|
|
|
|
if (method_exists($this, $name)) {
|
|
|
|
$this->$name($value);
|
|
|
|
} else {
|
|
|
|
$key = '_' . $key;
|
|
|
|
$this->$key = $value;
|
|
|
|
}
|
2007-11-02 02:54:27 +03:00
|
|
|
}
|
|
|
|
|
2007-10-14 10:44:49 +04:00
|
|
|
/**
|
2007-10-15 18:18:31 +04:00
|
|
|
* loadTemplate
|
|
|
|
*
|
|
|
|
* Loads the class template used for generating classes
|
2007-10-14 10:44:49 +04:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function loadTemplate()
|
|
|
|
{
|
2007-10-26 08:13:29 +04:00
|
|
|
if (isset(self::$_tpl)) {
|
2007-10-14 10:44:49 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-10-26 08:13:29 +04:00
|
|
|
self::$_tpl =<<<END
|
2007-10-14 10:44:49 +04:00
|
|
|
/**
|
|
|
|
* This class has been auto-generated by the Doctrine ORM Framework
|
|
|
|
*/
|
|
|
|
%sclass %s extends %s
|
|
|
|
{
|
|
|
|
%s
|
|
|
|
%s
|
2007-10-14 23:47:38 +04:00
|
|
|
%s
|
2007-10-14 10:44:49 +04:00
|
|
|
}
|
|
|
|
END;
|
|
|
|
}
|
|
|
|
|
2007-10-14 23:47:38 +04:00
|
|
|
/*
|
|
|
|
* Build the accessors
|
|
|
|
*
|
|
|
|
* @param string $table
|
2007-10-15 18:18:31 +04:00
|
|
|
* @param array $columns
|
2007-10-14 23:47:38 +04:00
|
|
|
*/
|
|
|
|
public function buildAccessors(array $options, array $columns)
|
|
|
|
{
|
2007-10-15 18:18:31 +04:00
|
|
|
$ret = '';
|
|
|
|
foreach ($columns as $name => $column) {
|
|
|
|
// getters
|
2007-10-29 15:22:53 +03:00
|
|
|
$ret .= "\n public function get".Doctrine::classify($name)."(\$load = true)\n";
|
|
|
|
$ret .= " {\n";
|
|
|
|
$ret .= " return \$this->get('{$name}', \$load);\n";
|
|
|
|
$ret .= " }\n";
|
2007-10-15 18:18:31 +04:00
|
|
|
|
|
|
|
// setters
|
2007-10-29 15:22:53 +03:00
|
|
|
$ret .= "\n public function set".Doctrine::classify($name)."(\${$name}, \$load = true)\n";
|
|
|
|
$ret .= " {\n";
|
|
|
|
$ret .= " return \$this->set('{$name}', \${$name}, \$load);\n";
|
|
|
|
$ret .= " }\n";
|
2007-10-15 18:18:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $ret;
|
2007-10-14 23:47:38 +04:00
|
|
|
}
|
|
|
|
|
2007-10-14 10:44:49 +04:00
|
|
|
/*
|
|
|
|
* Build the table definition of a Doctrine_Record object
|
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
* @param array $tableColumns
|
|
|
|
*/
|
2007-11-11 03:51:13 +03:00
|
|
|
public function buildTableDefinition(array $options, array $columns, array $relations, array $indexes, array $attributes, array $templates, array $actAs, array $tableOptions)
|
2007-10-14 10:44:49 +04:00
|
|
|
{
|
|
|
|
$ret = array();
|
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
|
2007-10-25 19:22:34 +04:00
|
|
|
if (isset($options['inheritance']['extends']) && !(isset($options['override_parent']) && $options['override_parent'] == false)) {
|
2007-10-29 15:22:53 +03:00
|
|
|
$ret[$i] = " parent::setTableDefinition();";
|
2007-10-14 10:44:49 +04:00
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['tableName']) && !empty($options['tableName'])) {
|
2007-10-29 15:22:53 +03:00
|
|
|
$ret[$i] = " ".'$this->setTableName(\''. $options['tableName'].'\');';
|
2007-10-14 10:44:49 +04:00
|
|
|
|
|
|
|
$i++;
|
|
|
|
}
|
2007-10-17 13:18:59 +04:00
|
|
|
|
2007-10-14 10:44:49 +04:00
|
|
|
foreach ($columns as $name => $column) {
|
2007-10-29 15:22:53 +03:00
|
|
|
$ret[$i] = " ".'$this->hasColumn(\'' . $name . '\', \'' . $column['type'] . '\'';
|
2007-10-14 10:44:49 +04:00
|
|
|
|
|
|
|
if ($column['length']) {
|
|
|
|
$ret[$i] .= ', ' . $column['length'];
|
|
|
|
} else {
|
|
|
|
$ret[$i] .= ', null';
|
|
|
|
}
|
|
|
|
|
2007-11-11 03:51:13 +03:00
|
|
|
$options = $column;
|
|
|
|
$unset = array('name', 'type', 'length', 'ptype');
|
|
|
|
foreach ($options as $key => $value) {
|
|
|
|
if (in_array($key, $unset) || $value === null) {
|
|
|
|
unset($options[$key]);
|
|
|
|
}
|
2007-10-14 10:44:49 +04:00
|
|
|
}
|
|
|
|
|
2007-11-11 03:51:13 +03:00
|
|
|
$ret[$i] .= ', ' . var_export($options, true);
|
|
|
|
|
2007-10-14 10:44:49 +04:00
|
|
|
$ret[$i] .= ');';
|
|
|
|
|
|
|
|
if ($i < (count($columns) - 1)) {
|
|
|
|
$ret[$i] .= PHP_EOL;
|
|
|
|
}
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
2007-10-17 03:33:14 +04:00
|
|
|
$ret[$i] = $this->buildIndexes($indexes);
|
|
|
|
$i++;
|
|
|
|
|
|
|
|
$ret[$i] = $this->buildAttributes($attributes);
|
|
|
|
$i++;
|
|
|
|
|
2007-10-17 23:21:06 +04:00
|
|
|
$ret[$i] = $this->buildTemplates($templates);
|
|
|
|
$i++;
|
|
|
|
|
|
|
|
$ret[$i] = $this->buildActAs($actAs);
|
2007-11-11 03:51:13 +03:00
|
|
|
$i++;
|
|
|
|
|
|
|
|
$ret[$i] = $this->buildTableOptions($tableOptions);
|
2007-10-17 23:21:06 +04:00
|
|
|
|
2007-10-25 19:22:34 +04:00
|
|
|
$code = implode("\n", $ret);
|
|
|
|
$code = trim($code);
|
|
|
|
|
|
|
|
if ($code) {
|
2007-10-29 15:22:53 +03:00
|
|
|
return "\n public function setTableDefinition()"."\n {\n ".$code."\n }";
|
2007-10-17 03:33:14 +04:00
|
|
|
}
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* buildTemplates
|
|
|
|
*
|
|
|
|
* @param string $array
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-10-17 23:21:06 +04:00
|
|
|
public function buildTemplates(array $templates)
|
|
|
|
{
|
|
|
|
$build = '';
|
|
|
|
foreach ($templates as $name => $options) {
|
|
|
|
|
2007-10-18 03:14:28 +04:00
|
|
|
if (is_array($options) && !empty($options)) {
|
2007-11-11 03:51:13 +03:00
|
|
|
$optionsPhp = var_export($options, true);
|
2007-10-17 23:21:06 +04:00
|
|
|
|
2007-10-29 15:22:53 +03:00
|
|
|
$build .= " \$this->loadTemplate('" . $name . "', " . $optionsPhp . ");\n";
|
2007-10-17 23:21:06 +04:00
|
|
|
} else {
|
2007-10-18 03:14:28 +04:00
|
|
|
if (isset($templates[0])) {
|
2007-10-29 15:22:53 +03:00
|
|
|
$build .= " \$this->loadTemplate('" . $options . "');\n";
|
2007-10-18 03:14:28 +04:00
|
|
|
} else {
|
2007-10-29 15:22:53 +03:00
|
|
|
$build .= " \$this->loadTemplate('" . $name . "');\n";
|
2007-10-18 03:14:28 +04:00
|
|
|
}
|
2007-10-17 23:21:06 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $build;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* buildActAs
|
|
|
|
*
|
|
|
|
* @param string $array
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-10-17 23:21:06 +04:00
|
|
|
public function buildActAs(array $actAs)
|
|
|
|
{
|
|
|
|
$build = '';
|
|
|
|
foreach ($actAs as $name => $options) {
|
2007-10-18 03:14:28 +04:00
|
|
|
if (is_array($options) && !empty($options)) {
|
2007-11-11 03:51:13 +03:00
|
|
|
$optionsPhp = var_export($options, true);
|
2007-10-18 03:14:28 +04:00
|
|
|
|
2007-10-29 15:22:53 +03:00
|
|
|
$build .= " \$this->actAs('" . $name . "', " . $optionsPhp . ");\n";
|
2007-10-18 03:14:28 +04:00
|
|
|
} else {
|
|
|
|
if (isset($actAs[0])) {
|
2007-10-29 15:22:53 +03:00
|
|
|
$build .= " \$this->actAs('" . $options . "');\n";
|
2007-10-18 03:14:28 +04:00
|
|
|
} else {
|
2007-10-29 15:22:53 +03:00
|
|
|
$build .= " \$this->actAs('" . $name . "');\n";
|
2007-10-18 03:14:28 +04:00
|
|
|
}
|
|
|
|
}
|
2007-10-17 23:21:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $build;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* buildAttributes
|
|
|
|
*
|
|
|
|
* @param string $array
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-10-17 03:33:14 +04:00
|
|
|
public function buildAttributes(array $attributes)
|
|
|
|
{
|
|
|
|
$build = "\n";
|
|
|
|
foreach ($attributes as $key => $value) {
|
2007-10-26 01:01:29 +04:00
|
|
|
|
|
|
|
if (is_bool($value))
|
|
|
|
{
|
|
|
|
$values = $value ? 'true':'false';
|
|
|
|
} else {
|
|
|
|
if ( ! is_array($value)) {
|
|
|
|
$value = array($value);
|
|
|
|
}
|
2007-10-17 03:33:14 +04:00
|
|
|
|
2007-10-26 01:01:29 +04:00
|
|
|
$values = '';
|
|
|
|
foreach ($value as $attr) {
|
|
|
|
$values .= "Doctrine::" . strtoupper($key) . "_" . strtoupper($attr) . ' ^ ';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trim last ^
|
|
|
|
$values = substr($values, 0, strlen($values) - 3);
|
2007-10-17 03:33:14 +04:00
|
|
|
}
|
|
|
|
|
2007-10-29 15:22:53 +03:00
|
|
|
$build .= " \$this->setAttribute(Doctrine::ATTR_" . strtoupper($key) . ", " . $values . ");\n";
|
2007-10-17 03:33:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $build;
|
|
|
|
}
|
2007-11-11 03:51:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* buildTableOptions
|
|
|
|
*
|
|
|
|
* @param string $array
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function buildTableOptions(array $options)
|
|
|
|
{
|
|
|
|
$build = '';
|
|
|
|
foreach ($options as $name => $value) {
|
|
|
|
$build .= " \$this->option('$name', " . var_export($value, true) . ");\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $build;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* buildIndexes
|
|
|
|
*
|
|
|
|
* @param string $array
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-10-17 03:33:14 +04:00
|
|
|
public function buildIndexes(array $indexes)
|
|
|
|
{
|
|
|
|
$build = '';
|
|
|
|
|
|
|
|
foreach ($indexes as $indexName => $definitions) {
|
2007-11-11 03:51:13 +03:00
|
|
|
$build .= "\n \$this->index('" . $indexName . "'";
|
|
|
|
$build .= ', ' . var_export($definitions, true);
|
|
|
|
$build .= ');';
|
2007-10-17 03:33:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $build;
|
2007-10-14 10:44:49 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* buildSetUp
|
|
|
|
*
|
|
|
|
* @param array $options
|
|
|
|
* @param array $columns
|
|
|
|
* @param array $relations
|
|
|
|
* @return string
|
|
|
|
*/
|
2007-10-14 10:44:49 +04:00
|
|
|
public function buildSetUp(array $options, array $columns, array $relations)
|
|
|
|
{
|
|
|
|
$ret = array();
|
|
|
|
$i = 0;
|
|
|
|
|
2007-10-25 19:22:34 +04:00
|
|
|
if (isset($options['inheritance']['extends']) && !(isset($options['override_parent']) && $options['override_parent'] == false)) {
|
2007-10-29 15:22:53 +03:00
|
|
|
$ret[$i] = " parent::setUp();";
|
2007-10-14 10:44:49 +04:00
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($relations as $name => $relation) {
|
|
|
|
$class = isset($relation['class']) ? $relation['class']:$name;
|
|
|
|
$alias = (isset($relation['alias']) && $relation['alias'] !== $relation['class']) ? ' as ' . $relation['alias'] : '';
|
|
|
|
|
|
|
|
if ( ! isset($relation['type'])) {
|
|
|
|
$relation['type'] = Doctrine_Relation::ONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($relation['type'] === Doctrine_Relation::ONE ||
|
|
|
|
$relation['type'] === Doctrine_Relation::ONE_COMPOSITE) {
|
2007-10-29 15:22:53 +03:00
|
|
|
$ret[$i] = " ".'$this->hasOne(\'' . $class . $alias . '\'';
|
2007-10-14 10:44:49 +04:00
|
|
|
} else {
|
2007-10-29 15:22:53 +03:00
|
|
|
$ret[$i] = " ".'$this->hasMany(\'' . $class . $alias . '\'';
|
2007-10-14 10:44:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$a = array();
|
|
|
|
|
|
|
|
if (isset($relation['refClass'])) {
|
|
|
|
$a[] = '\'refClass\' => ' . var_export($relation['refClass'], true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($relation['deferred']) && $relation['deferred']) {
|
|
|
|
$a[] = '\'default\' => ' . var_export($relation['deferred'], true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($relation['local']) && $relation['local']) {
|
|
|
|
$a[] = '\'local\' => ' . var_export($relation['local'], true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($relation['foreign']) && $relation['foreign']) {
|
|
|
|
$a[] = '\'foreign\' => ' . var_export($relation['foreign'], true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($relation['onDelete']) && $relation['onDelete']) {
|
|
|
|
$a[] = '\'onDelete\' => ' . var_export($relation['onDelete'], true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($relation['onUpdate']) && $relation['onUpdate']) {
|
|
|
|
$a[] = '\'onUpdate\' => ' . var_export($relation['onUpdate'], true);
|
|
|
|
}
|
|
|
|
|
2007-10-18 23:36:18 +04:00
|
|
|
if (isset($relation['equal']) && $relation['equal']) {
|
|
|
|
$a[] = '\'equal\' => ' . var_export($relation['equal'], true);
|
|
|
|
}
|
|
|
|
|
2007-10-14 10:44:49 +04:00
|
|
|
if ( ! empty($a)) {
|
|
|
|
$ret[$i] .= ', ' . 'array(';
|
|
|
|
$length = strlen($ret[$i]);
|
|
|
|
$ret[$i] .= implode(',' . PHP_EOL . str_repeat(' ', $length), $a) . ')';
|
|
|
|
}
|
|
|
|
|
2007-10-17 23:21:06 +04:00
|
|
|
$ret[$i] .= ');'."\n";
|
2007-10-14 10:44:49 +04:00
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['inheritance']['keyField']) && isset($options['inheritance']['keyValue'])) {
|
|
|
|
$i++;
|
2007-11-13 19:33:17 +03:00
|
|
|
$ret[$i] = " ".'$this->setInheritanceMap(array(\''.$options['inheritance']['keyField'].'\' => \''.$options['inheritance']['keyValue'].'\'));';
|
2007-10-14 10:44:49 +04:00
|
|
|
}
|
|
|
|
|
2007-10-25 19:22:34 +04:00
|
|
|
$code = implode("\n", $ret);
|
|
|
|
$code = trim($code);
|
|
|
|
|
|
|
|
if ($code) {
|
2007-10-29 15:22:53 +03:00
|
|
|
return "\n public function setUp()\n {\n ".$code."\n }";
|
2007-10-14 10:44:49 +04:00
|
|
|
}
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* buildDefinition
|
|
|
|
*
|
|
|
|
* @param array $options
|
|
|
|
* @param array $columns
|
|
|
|
* @param array $relations
|
|
|
|
* @param array $indexes
|
|
|
|
* @param array $attributes
|
|
|
|
* @param array $templates
|
|
|
|
* @param array $actAs
|
|
|
|
* @return string
|
|
|
|
*/
|
2007-11-11 03:51:13 +03:00
|
|
|
public function buildDefinition(array $options, array $columns, array $relations = array(), array $indexes = array(), $attributes = array(), array $templates = array(), array $actAs = array(), array $tableOptions = array())
|
2007-10-14 10:44:49 +04:00
|
|
|
{
|
|
|
|
if ( ! isset($options['className'])) {
|
|
|
|
throw new Doctrine_Import_Builder_Exception('Missing class name.');
|
|
|
|
}
|
2007-10-14 23:47:38 +04:00
|
|
|
|
2007-10-15 18:18:31 +04:00
|
|
|
$abstract = isset($options['abstract']) && $options['abstract'] === true ? 'abstract ':null;
|
2007-10-14 10:44:49 +04:00
|
|
|
$className = $options['className'];
|
2007-10-26 08:13:29 +04:00
|
|
|
$extends = isset($options['inheritance']['extends']) ? $options['inheritance']['extends']:$this->_baseClassName;
|
2007-10-14 23:47:38 +04:00
|
|
|
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! (isset($options['no_definition']) && $options['no_definition'] === true)) {
|
2007-11-11 03:51:13 +03:00
|
|
|
$definition = $this->buildTableDefinition($options, $columns, $relations, $indexes, $attributes, $templates, $actAs, $tableOptions);
|
2007-10-14 23:47:38 +04:00
|
|
|
$setUp = $this->buildSetUp($options, $columns, $relations);
|
2007-10-15 19:01:54 +04:00
|
|
|
} else {
|
|
|
|
$definition = null;
|
|
|
|
$setUp = null;
|
2007-10-14 23:47:38 +04:00
|
|
|
}
|
|
|
|
|
2007-10-15 18:19:51 +04:00
|
|
|
$accessors = (isset($options['generate_accessors']) && $options['generate_accessors'] === true) ? $this->buildAccessors($options, $columns):null;
|
2007-10-14 10:44:49 +04:00
|
|
|
|
2007-10-26 08:13:29 +04:00
|
|
|
$content = sprintf(self::$_tpl, $abstract,
|
2007-10-14 10:44:49 +04:00
|
|
|
$className,
|
|
|
|
$extends,
|
|
|
|
$definition,
|
2007-10-14 23:47:38 +04:00
|
|
|
$setUp,
|
|
|
|
$accessors);
|
2007-10-14 10:44:49 +04:00
|
|
|
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
2007-10-21 10:23:59 +04:00
|
|
|
/**
|
|
|
|
* buildRecord
|
|
|
|
*
|
|
|
|
* @param array $options
|
|
|
|
* @param array $columns
|
|
|
|
* @param array $relations
|
|
|
|
* @param array $indexes
|
|
|
|
* @param array $attributes
|
|
|
|
* @param array $templates
|
|
|
|
* @param array $actAs
|
|
|
|
* @return void=
|
|
|
|
*/
|
2007-11-11 03:51:13 +03:00
|
|
|
public function buildRecord(array $options, array $columns, array $relations = array(), array $indexes = array(), array $attributes = array(), array $templates = array(), array $actAs = array(), array $tableOptions = array())
|
2007-10-14 10:44:49 +04:00
|
|
|
{
|
|
|
|
if ( !isset($options['className'])) {
|
|
|
|
throw new Doctrine_Import_Builder_Exception('Missing class name.');
|
|
|
|
}
|
|
|
|
|
2007-10-25 01:21:45 +04:00
|
|
|
if ($this->generateBaseClasses()) {
|
|
|
|
$options['is_package'] = (isset($options['package']) && $options['package']) ? true:false;
|
|
|
|
|
|
|
|
if ($options['is_package']) {
|
|
|
|
$e = explode('.', $options['package']);
|
|
|
|
$options['package_name'] = $e[0];
|
|
|
|
unset($e[0]);
|
|
|
|
|
|
|
|
$options['package_path'] = implode(DIRECTORY_SEPARATOR, $e);
|
2007-10-14 10:44:49 +04:00
|
|
|
}
|
|
|
|
|
2007-10-25 01:21:45 +04:00
|
|
|
// Top level definition that extends from all the others
|
|
|
|
$topLevel = $options;
|
|
|
|
unset($topLevel['tableName']);
|
|
|
|
|
|
|
|
// If we have a package then we need to make this extend the package definition and not the base definition
|
|
|
|
// The package definition will then extends the base definition
|
2007-10-26 08:13:29 +04:00
|
|
|
$topLevel['inheritance']['extends'] = (isset($topLevel['package']) && $topLevel['package']) ? $this->_packagesPrefix . $topLevel['className']:'Base' . $topLevel['className'];
|
2007-10-25 01:21:45 +04:00
|
|
|
$topLevel['no_definition'] = true;
|
|
|
|
$topLevel['generate_once'] = true;
|
|
|
|
$topLevel['is_main_class'] = true;
|
|
|
|
|
|
|
|
// Package level definition that extends from the base definition
|
|
|
|
if (isset($options['package'])) {
|
|
|
|
|
|
|
|
$packageLevel = $options;
|
|
|
|
$packageLevel['className'] = $topLevel['inheritance']['extends'];
|
|
|
|
$packageLevel['inheritance']['extends'] = 'Base' . $topLevel['className'];
|
|
|
|
$packageLevel['no_definition'] = true;
|
|
|
|
$packageLevel['abstract'] = true;
|
|
|
|
$packageLevel['override_parent'] = true;
|
|
|
|
$packageLevel['generate_once'] = true;
|
|
|
|
$packageLevel['is_package_class'] = true;
|
2007-10-14 10:44:49 +04:00
|
|
|
}
|
|
|
|
|
2007-10-25 01:21:45 +04:00
|
|
|
$baseClass = $options;
|
|
|
|
$baseClass['className'] = 'Base' . $baseClass['className'];
|
|
|
|
$baseClass['abstract'] = true;
|
|
|
|
$baseClass['override_parent'] = true;
|
|
|
|
$baseClass['is_base_class'] = true;
|
|
|
|
|
2007-11-11 03:51:13 +03:00
|
|
|
$this->writeDefinition($baseClass, $columns, $relations, $indexes, $attributes, $templates, $actAs, $tableOptions);
|
2007-10-14 10:44:49 +04:00
|
|
|
|
2007-10-25 01:21:45 +04:00
|
|
|
if (!empty($packageLevel)) {
|
|
|
|
$this->writeDefinition($packageLevel);
|
|
|
|
}
|
2007-10-14 10:44:49 +04:00
|
|
|
|
2007-10-25 01:21:45 +04:00
|
|
|
$this->writeDefinition($topLevel);
|
2007-10-14 10:44:49 +04:00
|
|
|
} else {
|
2007-11-11 03:51:13 +03:00
|
|
|
$this->writeDefinition($options, $columns, $relations, $indexes, $attributes, $templates, $actAs, $tableOptions);
|
2007-10-14 10:44:49 +04:00
|
|
|
}
|
|
|
|
}
|
2007-10-25 01:21:45 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* writeTableDefinition
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function writeTableDefinition($className, $path, $options = array())
|
|
|
|
{
|
|
|
|
$className = $className . 'Table';
|
|
|
|
|
|
|
|
$content = '<?php' . PHP_EOL;
|
2007-10-26 08:13:29 +04:00
|
|
|
$content .= sprintf(self::$_tpl, false,
|
2007-10-25 01:21:45 +04:00
|
|
|
$className,
|
|
|
|
isset($options['extends']) ? $options['extends']:'Doctrine_Table',
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null
|
|
|
|
);
|
|
|
|
|
|
|
|
Doctrine::makeDirectories($path);
|
|
|
|
|
2007-10-26 08:13:29 +04:00
|
|
|
$writePath = $path . DIRECTORY_SEPARATOR . $className . $this->_suffix;
|
2007-10-25 01:21:45 +04:00
|
|
|
|
|
|
|
if (!file_exists($writePath)) {
|
|
|
|
file_put_contents($writePath, $content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-21 10:23:59 +04:00
|
|
|
/**
|
|
|
|
* writeDefinition
|
|
|
|
*
|
|
|
|
* @param array $options
|
|
|
|
* @param array $columns
|
|
|
|
* @param array $relations
|
|
|
|
* @param array $indexes
|
|
|
|
* @param array $attributes
|
|
|
|
* @param array $templates
|
|
|
|
* @param array $actAs
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-11-11 03:51:13 +03:00
|
|
|
public function writeDefinition(array $options, array $columns = array(), array $relations = array(), array $indexes = array(), array $attributes = array(), array $templates = array(), array $actAs = array(), array $tableOptions = array())
|
2007-10-14 10:44:49 +04:00
|
|
|
{
|
2007-11-11 03:51:13 +03:00
|
|
|
$definition = $this->buildDefinition($options, $columns, $relations, $indexes, $attributes, $templates, $actAs, $tableOptions);
|
2007-10-25 01:21:45 +04:00
|
|
|
|
2007-10-26 08:13:29 +04:00
|
|
|
$fileName = $options['className'] . $this->_suffix;
|
2007-10-25 01:21:45 +04:00
|
|
|
|
2007-10-26 08:13:29 +04:00
|
|
|
$packagesPath = $this->_packagesPath ? $this->_packagesPath:$this->_path;
|
2007-10-25 01:21:45 +04:00
|
|
|
|
|
|
|
// If this is a main class that either extends from Base or Package class
|
|
|
|
if (isset($options['is_main_class']) && $options['is_main_class']) {
|
|
|
|
// If is package then we need to put it in a package subfolder
|
|
|
|
if (isset($options['is_package']) && $options['is_package']) {
|
2007-10-26 08:13:29 +04:00
|
|
|
$writePath = $this->_path . DIRECTORY_SEPARATOR . $options['package_name'];
|
2007-10-25 01:21:45 +04:00
|
|
|
|
|
|
|
$this->writeTableDefinition($options['className'], $writePath, array('extends' => $options['inheritance']['extends'] . 'Table'));
|
|
|
|
// Otherwise lets just put it in the root of the path
|
|
|
|
} else {
|
2007-10-26 08:13:29 +04:00
|
|
|
$writePath = $this->_path;
|
2007-10-25 01:21:45 +04:00
|
|
|
|
|
|
|
$this->writeTableDefinition($options['className'], $writePath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If is the package class then we need to make the path to the complete package
|
|
|
|
if (isset($options['is_package_class']) && $options['is_package_class']) {
|
2007-11-08 03:29:15 +03:00
|
|
|
$path = str_replace('.', DIRECTORY_SEPARATOR, trim($options['package']));
|
2007-10-25 01:21:45 +04:00
|
|
|
|
|
|
|
$writePath = $packagesPath . DIRECTORY_SEPARATOR . $path;
|
|
|
|
|
|
|
|
$this->writeTableDefinition($options['className'], $writePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it is the base class of the doctrine record definition
|
|
|
|
if (isset($options['is_base_class']) && $options['is_base_class']) {
|
|
|
|
// If it is a part of a package then we need to put it in a package subfolder
|
|
|
|
if (isset($options['is_package']) && $options['is_package']) {
|
2007-10-26 08:13:29 +04:00
|
|
|
$writePath = $this->_path . DIRECTORY_SEPARATOR . $options['package_name'] . DIRECTORY_SEPARATOR . $this->_baseClassesDirectory;
|
2007-10-25 01:21:45 +04:00
|
|
|
// Otherwise lets just put it in the root generated folder
|
|
|
|
} else {
|
2007-10-26 08:13:29 +04:00
|
|
|
$writePath = $this->_path . DIRECTORY_SEPARATOR . $this->_baseClassesDirectory;
|
2007-10-25 01:21:45 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($writePath)) {
|
|
|
|
Doctrine::makeDirectories($writePath);
|
|
|
|
|
|
|
|
$writePath .= DIRECTORY_SEPARATOR . $fileName;
|
|
|
|
} else {
|
2007-10-26 08:13:29 +04:00
|
|
|
Doctrine::makeDirectories($this->_path);
|
2007-10-25 01:21:45 +04:00
|
|
|
|
2007-10-26 08:13:29 +04:00
|
|
|
$writePath = $this->_path . DIRECTORY_SEPARATOR . $fileName;
|
2007-10-25 01:21:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$code = "<?php" . PHP_EOL;
|
2007-10-17 23:21:06 +04:00
|
|
|
|
2007-10-15 18:09:59 +04:00
|
|
|
if (isset($options['requires'])) {
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! is_array($options['requires'])) {
|
2007-10-15 18:09:59 +04:00
|
|
|
$options['requires'] = array($options['requires']);
|
|
|
|
}
|
2007-10-25 01:21:45 +04:00
|
|
|
|
2007-10-15 18:09:59 +04:00
|
|
|
foreach ($options['requires'] as $require) {
|
2007-10-15 18:18:31 +04:00
|
|
|
$code .= "require_once('".$require."');\n";
|
2007-10-15 18:09:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-17 23:21:06 +04:00
|
|
|
if (isset($options['connection']) && $options['connection']) {
|
|
|
|
$code .= "// Connection Component Binding\n";
|
|
|
|
$code .= "Doctrine_Manager::getInstance()->bindComponent('" . $options['connectionClassName'] . "', '" . $options['connection'] . "');\n";
|
|
|
|
}
|
2007-10-15 18:09:59 +04:00
|
|
|
|
2007-10-25 01:21:45 +04:00
|
|
|
$code .= PHP_EOL . $definition;
|
2007-10-25 19:22:34 +04:00
|
|
|
|
2007-10-25 01:21:45 +04:00
|
|
|
if (isset($options['generate_once']) && $options['generate_once'] === true) {
|
|
|
|
if (!file_exists($writePath)) {
|
|
|
|
$bytes = file_put_contents($writePath, $code);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$bytes = file_put_contents($writePath, $code);
|
|
|
|
}
|
2007-10-15 18:09:59 +04:00
|
|
|
|
2007-10-25 01:21:45 +04:00
|
|
|
if (isset($bytes) && $bytes === false) {
|
|
|
|
throw new Doctrine_Import_Builder_Exception("Couldn't write file " . $writePath);
|
2007-10-15 18:09:59 +04:00
|
|
|
}
|
2007-10-14 10:44:49 +04:00
|
|
|
}
|
2007-11-13 19:33:17 +03:00
|
|
|
}
|