. */ /** * Doctrine_Import_Builder * Import builder is responsible of building Doctrine ActiveRecord classes * 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 * @author Jukka Hassinen * @author Nicolas BĂ©rard-Nault */ class Doctrine_Import_Builder { /** * @var string $path the path where imported files are being generated */ private $path = ''; private $suffix = '.php'; private static $tpl; public function __construct() { $this->loadTemplate(); } /** * setTargetPath * * @param string path the path where imported files are being generated * @return */ public function setTargetPath($path) { if ( ! file_exists($path)) { mkdir($path, 0777); } $this->path = $path; } /** * getTargetPath * * @return string the path where imported files are being generated */ public function getTargetPath() { return $this->path; } /** * 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() { if (isset(self::$tpl)) { return; } self::$tpl =<<setTableName(\''. $options['tableName'].'\');'; $i++; } foreach ($columns as $name => $column) { $ret[$i] = ' $this->hasColumn(\'' . $name . '\', \'' . $column['type'] . '\''; if ($column['length']) { $ret[$i] .= ', ' . $column['length']; } else { $ret[$i] .= ', null'; } $a = array(); if (isset($column['default']) && $column['default']) { $a[] = '\'default\' => ' . var_export($column['default'], true); } if (isset($column['notnull']) && $column['notnull']) { $a[] = '\'notnull\' => true'; } if (isset($column['primary']) && $column['primary']) { $a[] = '\'primary\' => true'; } if (isset($column['autoinc']) && $column['autoinc']) { $a[] = '\'autoincrement\' => true'; } if (isset($column['unique']) && $column['unique']) { $a[] = '\'unique\' => true'; } if (isset($column['unsigned']) && $column['unsigned']) { $a[] = '\'unsigned\' => true'; } if ($column['type'] == 'enum' && isset($column['values']) ) { $a[] = '\'values\' => array(\'' . implode('\',\'', $column['values']) . '\')'; } if ( ! empty($a)) { $ret[$i] .= ', ' . 'array('; $length = strlen($ret[$i]); $ret[$i] .= implode(',' . PHP_EOL . str_repeat(' ', $length), $a) . ')'; } $ret[$i] .= ');'; if ($i < (count($columns) - 1)) { $ret[$i] .= PHP_EOL; } $i++; } return implode("\n", $ret); } public function buildSetUp(array $options, array $columns, array $relations) { $ret = array(); if (isset($options['inheritance']) && isset($options['inheritance']['extends'])) { $ret[1] = "\t\tparent::setUp();"; $i = 1; } else { $i = 0; } 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 . '\''; } $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); } if ( ! empty($a)) { $ret[$i] .= ', ' . 'array('; $length = strlen($ret[$i]); $ret[$i] .= implode(',' . PHP_EOL . str_repeat(' ', $length), $a) . ')'; } $ret[$i] .= ');'; $i++; } if (isset($options['inheritance']['keyField']) && isset($options['inheritance']['keyValue'])) { $i++; $ret[$i] = "\t\t".'$this->setInheritanceMap(array(\''.$options['inheritance']['keyField'].'\' => '.$options['inheritance']['keyValue'].'));'; } return implode("\n", $ret); } public function buildDefinition(array $options, array $columns, array $relations = array()) { if ( ! isset($options['className'])) { throw new Doctrine_Import_Builder_Exception('Missing class name.'); } $className = $options['className']; $extends = isset($options['inheritance']['extends']) ? $options['inheritance']['extends']:'Doctrine_Record'; $content = sprintf(self::$tpl, $className, $extends, $this->buildTableDefinition($options, $columns, $relations), $this->buildSetUp($options, $columns, $relations)); return $content; } public function buildRecord(array $options, array $columns, array $relations = array()) { if ( ! isset($options['className'])) { throw new Doctrine_Import_Builder_Exception('Missing class name.'); } if ( ! isset($options['fileName'])) { if (empty($this->path)) { $errMsg = 'No build target directory set.'; throw new Doctrine_Import_Builder_Exception($errMsg); } if (is_writable($this->path) === false) { $errMsg = 'Build target directory ' . $this->path . ' is not writable.'; throw new Doctrine_Import_Builder_Exception($errMsg); } $options['fileName'] = $this->path . DIRECTORY_SEPARATOR . $options['className'] . $this->suffix; } $content = $this->buildDefinition($options, $columns, $relations); $bytes = file_put_contents($options['fileName'], '