Commit e2e38849 authored by Jonathan.Wage's avatar Jonathan.Wage

Added support for connection binding, templates, act as, and attributes.

parent f8ee0975
...@@ -193,45 +193,25 @@ END; ...@@ -193,45 +193,25 @@ END;
* @param string $table * @param string $table
* @param array $tableColumns * @param array $tableColumns
*/ */
public function buildTableDefinition(array $options, array $columns, array $relations, array $indexes, array $attributes) public function buildTableDefinition(array $options, array $columns, array $relations, array $indexes, array $attributes, array $templates, array $actAs)
{ {
$ret = array(); $ret = array();
$i = 0; $i = 0;
if (isset($options['inheritance']['extends']) && !isset($options['override_parent'])) { if (isset($options['inheritance']['extends']) && !isset($options['override_parent'])) {
$ret[$i] = "\t\t\t\tparent::setTableDefinition();"; $ret[$i] = "\t\tparent::setTableDefinition();";
$i++; $i++;
} }
if (isset($options['tableName']) && !empty($options['tableName'])) { if (isset($options['tableName']) && !empty($options['tableName'])) {
$ret[$i] = str_repeat(' ', 8) . '$this->setTableName(\''. $options['tableName'].'\');'; $ret[$i] = "\t\t".'$this->setTableName(\''. $options['tableName'].'\');';
$i++; $i++;
} }
if (isset($options['templates']) && !empty($options['templates'])) {
$ret[$i] = PHP_EOL .str_repeat(' ', 8) . '// Load Class Template(s)';
$i++;
if (!is_array($options['templates'])) {
// explode to extract each template separated by commas
$templatesArray = explode(',', $options['templates']);
} else {
// set the existing array to templatesArray to be traversed through
$templatesArray = $options['templates'];
}
foreach ($templatesArray as $templateName) {
$ret[$i] = str_repeat(' ', 8) . '$this->loadTemplate(\''. trim($templateName) .'\');';
$i++;
}
$ret[$i] = '';
$i++;
}
foreach ($columns as $name => $column) { foreach ($columns as $name => $column) {
$ret[$i] = ' $this->hasColumn(\'' . $name . '\', \'' . $column['type'] . '\''; $ret[$i] = "\t\t".'$this->hasColumn(\'' . $name . '\', \'' . $column['type'] . '\'';
if ($column['length']) { if ($column['length']) {
$ret[$i] .= ', ' . $column['length']; $ret[$i] .= ', ' . $column['length'];
...@@ -283,11 +263,73 @@ END; ...@@ -283,11 +263,73 @@ END;
$ret[$i] = $this->buildAttributes($attributes); $ret[$i] = $this->buildAttributes($attributes);
$i++; $i++;
$ret[$i] = $this->buildTemplates($templates);
$i++;
$ret[$i] = $this->buildActAs($actAs);
if (!empty($ret)) { if (!empty($ret)) {
return "\n\tpublic function setTableDefinition()"."\n\t{\n".implode("\n", $ret)."\n\t}"; return "\n\tpublic function setTableDefinition()"."\n\t{\n".implode("\n", $ret)."\n\t}";
} }
} }
public function buildTemplates(array $templates)
{
$build = '';
foreach ($templates as $name => $options) {
if (is_array($options)) {
$optionsPhp = $this->arrayToPhpArrayCode($options);
$build .= "\t\t\$this->loadTemplate('" . $name . "', " . $optionsPhp . ");\n";
} else {
$build .= "\t\t\$this->loadTemplate('" . $options . "');\n";
}
}
return $build;
}
public function buildActAs(array $actAs)
{
$build = '';
foreach ($actAs as $name => $options) {
$optionsPhp = $this->arrayToPhpArrayCode($options);
$build .= "\t\t\$this->actAs('" . $name . "', " . $optionsPhp . ");\n";
}
return $build;
}
protected function arrayToPhpArrayCode($array)
{
$build = 'array(';
foreach ($array as $key => $value) {
if (is_array($value)) {
$build .= $this->arrayToPhpArrayCode($value);
} else {
$build .= "'" . $key . "' => ";
if (is_integer($value)) {
$build .= "$value";
} else if (is_string($value)) {
$build .= "'" . $value . "'";
} else if (is_bool($value)) {
$build .= $value ? "true":"false";
}
$build .= ", ";
}
}
$build = substr($build, 0, strlen($build) - 2);
$build .= ')';
return $build;
}
public function buildAttributes(array $attributes) public function buildAttributes(array $attributes)
{ {
$build = "\n"; $build = "\n";
...@@ -315,7 +357,7 @@ END; ...@@ -315,7 +357,7 @@ END;
$build = ''; $build = '';
foreach ($indexes as $indexName => $definitions) { foreach ($indexes as $indexName => $definitions) {
$build = "\n".' $this->index(\'' . $indexName . '\', array('; $build = "\n\t\t".'$this->index(\'' . $indexName . '\', array(';
foreach ($definitions as $name => $value) { foreach ($definitions as $name => $value) {
...@@ -376,7 +418,7 @@ END; ...@@ -376,7 +418,7 @@ END;
$i = 0; $i = 0;
if (! (isset($options['override_parent']) && $options['override_parent'] === true)) { if (! (isset($options['override_parent']) && $options['override_parent'] === true)) {
$ret[$i] = "\t\t\t\tparent::setUp();"; $ret[$i] = "\t\tparent::setUp();";
$i++; $i++;
} }
...@@ -390,9 +432,9 @@ END; ...@@ -390,9 +432,9 @@ END;
if ($relation['type'] === Doctrine_Relation::ONE || if ($relation['type'] === Doctrine_Relation::ONE ||
$relation['type'] === Doctrine_Relation::ONE_COMPOSITE) { $relation['type'] === Doctrine_Relation::ONE_COMPOSITE) {
$ret[$i] = ' $this->hasOne(\'' . $class . $alias . '\''; $ret[$i] = "\t\t".'$this->hasOne(\'' . $class . $alias . '\'';
} else { } else {
$ret[$i] = ' $this->hasMany(\'' . $class . $alias . '\''; $ret[$i] = "\t\t".'$this->hasMany(\'' . $class . $alias . '\'';
} }
$a = array(); $a = array();
...@@ -421,17 +463,13 @@ END; ...@@ -421,17 +463,13 @@ END;
$a[] = '\'onUpdate\' => ' . var_export($relation['onUpdate'], true); $a[] = '\'onUpdate\' => ' . var_export($relation['onUpdate'], true);
} }
if (isset($relation['equal']) && $relation['equal']) {
$a[] = '\'equal\' => ' . var_export($relation['equal'], true);
}
if ( ! empty($a)) { if ( ! empty($a)) {
$ret[$i] .= ', ' . 'array('; $ret[$i] .= ', ' . 'array(';
$length = strlen($ret[$i]); $length = strlen($ret[$i]);
$ret[$i] .= implode(',' . PHP_EOL . str_repeat(' ', $length), $a) . ')'; $ret[$i] .= implode(',' . PHP_EOL . str_repeat(' ', $length), $a) . ')';
} }
$ret[$i] .= ');'; $ret[$i] .= ');'."\n";
$i++; $i++;
} }
...@@ -445,7 +483,7 @@ END; ...@@ -445,7 +483,7 @@ END;
} }
} }
public function buildDefinition(array $options, array $columns, array $relations = array(), array $indexes = array(), $attributes = array()) public function buildDefinition(array $options, array $columns, array $relations = array(), array $indexes = array(), $attributes = array(), array $templates = array(), array $actAs = array())
{ {
if ( ! isset($options['className'])) { if ( ! isset($options['className'])) {
throw new Doctrine_Import_Builder_Exception('Missing class name.'); throw new Doctrine_Import_Builder_Exception('Missing class name.');
...@@ -456,7 +494,7 @@ END; ...@@ -456,7 +494,7 @@ END;
$extends = isset($options['inheritance']['extends']) ? $options['inheritance']['extends']:'Doctrine_Record'; $extends = isset($options['inheritance']['extends']) ? $options['inheritance']['extends']:'Doctrine_Record';
if (!(isset($options['no_definition']) && $options['no_definition'] === true)) { if (!(isset($options['no_definition']) && $options['no_definition'] === true)) {
$definition = $this->buildTableDefinition($options, $columns, $relations, $indexes, $attributes); $definition = $this->buildTableDefinition($options, $columns, $relations, $indexes, $attributes, $templates, $actAs);
$setUp = $this->buildSetUp($options, $columns, $relations); $setUp = $this->buildSetUp($options, $columns, $relations);
} else { } else {
$definition = null; $definition = null;
...@@ -475,7 +513,7 @@ END; ...@@ -475,7 +513,7 @@ END;
return $content; return $content;
} }
public function buildRecord(array $options, array $columns, array $relations = array(), array $indexes = array(), array $attributes = array()) public function buildRecord(array $options, array $columns, array $relations = array(), array $indexes = array(), array $attributes = array(), array $templates = array(), array $actAs = array())
{ {
if ( !isset($options['className'])) { if ( !isset($options['className'])) {
throw new Doctrine_Import_Builder_Exception('Missing class name.'); throw new Doctrine_Import_Builder_Exception('Missing class name.');
...@@ -521,17 +559,17 @@ END; ...@@ -521,17 +559,17 @@ END;
$options['fileName'] = $generatedPath . DIRECTORY_SEPARATOR . $options['className'] . $this->suffix; $options['fileName'] = $generatedPath . DIRECTORY_SEPARATOR . $options['className'] . $this->suffix;
$options['override_parent'] = true; $options['override_parent'] = true;
$this->writeDefinition($options, $columns, $relations, $indexes, $attributes); $this->writeDefinition($options, $columns, $relations, $indexes, $attributes, $templates, $actAs);
} else { } else {
$this->writeDefinition($options, $columns, $relations, $indexes, $attributes); $this->writeDefinition($options, $columns, $relations, $indexes, $attributes, $templates, $actAs);
} }
} }
public function writeDefinition(array $options, array $columns = array(), array $relations = array(), array $indexes = array(), array $attributes = array()) public function writeDefinition(array $options, array $columns = array(), array $relations = array(), array $indexes = array(), array $attributes = array(), array $templates = array(), array $actAs = array())
{ {
$content = $this->buildDefinition($options, $columns, $relations, $indexes, $attributes); $content = $this->buildDefinition($options, $columns, $relations, $indexes, $attributes, $templates, $actAs);
$code = "<?php\n"; $code = "<?php\n";
if (isset($options['requires'])) { if (isset($options['requires'])) {
if (!is_array($options['requires'])) { if (!is_array($options['requires'])) {
$options['requires'] = array($options['requires']); $options['requires'] = array($options['requires']);
...@@ -542,6 +580,11 @@ END; ...@@ -542,6 +580,11 @@ END;
} }
} }
if (isset($options['connection']) && $options['connection']) {
$code .= "// Connection Component Binding\n";
$code .= "Doctrine_Manager::getInstance()->bindComponent('" . $options['connectionClassName'] . "', '" . $options['connection'] . "');\n";
}
$code .= PHP_EOL . $content; $code .= PHP_EOL . $content;
$bytes = file_put_contents($options['fileName'], $code); $bytes = file_put_contents($options['fileName'], $code);
...@@ -550,4 +593,4 @@ END; ...@@ -550,4 +593,4 @@ END;
throw new Doctrine_Import_Builder_Exception("Couldn't write file " . $options['fileName']); throw new Doctrine_Import_Builder_Exception("Couldn't write file " . $options['fileName']);
} }
} }
} }
\ No newline at end of file
...@@ -121,8 +121,10 @@ class Doctrine_Import_Schema ...@@ -121,8 +121,10 @@ class Doctrine_Import_Schema
$relations = $this->getRelations($properties); $relations = $this->getRelations($properties);
$indexes = $this->getIndexes($properties); $indexes = $this->getIndexes($properties);
$attributes = $this->getAttributes($properties); $attributes = $this->getAttributes($properties);
$templates = $this->getTemplates($properties);
$actAs = $this->getActAs($properties);
$builder->buildRecord($options, $columns, $relations, $indexes, $attributes); $builder->buildRecord($options, $columns, $relations, $indexes, $attributes, $templates, $actAs);
} }
} }
...@@ -139,8 +141,9 @@ class Doctrine_Import_Schema ...@@ -139,8 +141,9 @@ class Doctrine_Import_Schema
$options['className'] = $properties['className']; $options['className'] = $properties['className'];
$options['fileName'] = $directory.DIRECTORY_SEPARATOR.$properties['className'].'.class.php'; $options['fileName'] = $directory.DIRECTORY_SEPARATOR.$properties['className'].'.class.php';
$options['tableName'] = isset($properties['tableName']) ? $properties['tableName']:null; $options['tableName'] = isset($properties['tableName']) ? $properties['tableName']:null;
$options['templates'] = isset($properties['templates']) ? $properties['templates']:null; $options['connection'] = isset($properties['connection']) ? $properties['connection']:null;
$options['connectionClassName'] = isset($properties['connection']) ? $properties['className']:null;
if (isset($properties['inheritance'])) { if (isset($properties['inheritance'])) {
$options['inheritance'] = $properties['inheritance']; $options['inheritance'] = $properties['inheritance'];
} }
...@@ -192,6 +195,28 @@ class Doctrine_Import_Schema ...@@ -192,6 +195,28 @@ class Doctrine_Import_Schema
return isset($properties['attributes']) ? $properties['attributes']:array(); return isset($properties['attributes']) ? $properties['attributes']:array();
} }
/**
* getTemplates
*
* @param string $properties
* @return void
*/
public function getTemplates($properties)
{
return isset($properties['templates']) ? $properties['templates']:array();
}
/**
* getActAs
*
* @param string $properties
* @return void
*/
public function getActAs($properties)
{
return isset($properties['actAs']) ? $properties['actAs']:array();
}
/** /**
* parseSchema * parseSchema
* *
...@@ -213,8 +238,6 @@ class Doctrine_Import_Schema ...@@ -213,8 +238,6 @@ class Doctrine_Import_Schema
$className = isset($table['className']) ? (string) $table['className']:(string) $className; $className = isset($table['className']) ? (string) $table['className']:(string) $className;
$tableName = isset($table['tableName']) ? (string) $table['tableName']:(string) Doctrine::tableize($className); $tableName = isset($table['tableName']) ? (string) $table['tableName']:(string) Doctrine::tableize($className);
$build[$className]['className'] = $className;
$columns = isset($table['columns']) ? $table['columns']:array(); $columns = isset($table['columns']) ? $table['columns']:array();
$columns = isset($table['fields']) ? $table['fields']:$columns; $columns = isset($table['fields']) ? $table['fields']:$columns;
...@@ -232,18 +255,22 @@ class Doctrine_Import_Schema ...@@ -232,18 +255,22 @@ class Doctrine_Import_Schema
$colDesc['default'] = isset($field['default']) ? (string) $field['default']:null; $colDesc['default'] = isset($field['default']) ? (string) $field['default']:null;
$colDesc['notnull'] = isset($field['notnull']) ? (bool) (isset($field['notnull']) && $field['notnull']):null; $colDesc['notnull'] = isset($field['notnull']) ? (bool) (isset($field['notnull']) && $field['notnull']):null;
$colDesc['autoincrement'] = isset($field['autoincrement']) ? (bool) (isset($field['autoincrement']) && $field['autoincrement']):null; $colDesc['autoincrement'] = isset($field['autoincrement']) ? (bool) (isset($field['autoincrement']) && $field['autoincrement']):null;
$colDesc['autoincrement'] = isset($field['autoinc']) ? (bool) (isset($field['autoinc']) && $field['autoinc']):$colDesc['autoincrement'];
$colDesc['unique'] = isset($field['unique']) ? (bool) (isset($field['unique']) && $field['unique']):null; $colDesc['unique'] = isset($field['unique']) ? (bool) (isset($field['unique']) && $field['unique']):null;
$colDesc['values'] = isset($field['values']) ? (array) $field['values']: null; $colDesc['values'] = isset($field['values']) ? (array) $field['values']: null;
$columns[(string) $colDesc['name']] = $colDesc; $columns[(string) $colDesc['name']] = $colDesc;
} }
$build[$className]['connection'] = isset($table['connection']) ? $table['connection']:null;
$build[$className]['className'] = $className;
$build[$className]['tableName'] = $tableName; $build[$className]['tableName'] = $tableName;
$build[$className]['columns'] = $columns; $build[$className]['columns'] = $columns;
$build[$className]['templates'] = isset($table['templates']) ? $table['templates']:array();
$build[$className]['relations'] = isset($table['relations']) ? $table['relations']:array(); $build[$className]['relations'] = isset($table['relations']) ? $table['relations']:array();
$build[$className]['indexes'] = isset($table['indexes']) ? $table['indexes']:array(); $build[$className]['indexes'] = isset($table['indexes']) ? $table['indexes']:array();
$build[$className]['attributes'] = isset($table['attributes']) ? $table['attributes']:array(); $build[$className]['attributes'] = isset($table['attributes']) ? $table['attributes']:array();
$build[$className]['templates'] = isset($table['templates']) ? $table['templates']:array();
$build[$className]['actAs'] = isset($table['actAs']) ? $table['actAs']:array();
} }
if (isset($table['inheritance'])) { if (isset($table['inheritance'])) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment