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

Fixes for schema importing.

parent 29ff0dee
......@@ -442,7 +442,7 @@ final class Doctrine
*/
public static function loadModels($directory)
{
$declared = get_declared_classes();
$declared = array();
if ($directory !== null) {
foreach ((array) $directory as $dir) {
......@@ -457,7 +457,7 @@ final class Doctrine
}
}
$declared = array_diff(get_declared_classes(), $declared);
$declared = get_declared_classes();
}
return self::getLoadedModels($declared);
......
......@@ -36,7 +36,7 @@
* @version $Revision: 1838 $
* @author Nicolas Bérard-Nault <nicobn@gmail.com>
*/
abstract class Doctrine_Export_Schema
class Doctrine_Export_Schema
{
/**
* build
......@@ -46,7 +46,10 @@ abstract class Doctrine_Export_Schema
* @param string $array
* @return void
*/
abstract function build($array);
public function build($array)
{
throw new Doctrine_Export_Exception('This functionality is implemented by the driver');
}
/**
* dump
......@@ -64,43 +67,6 @@ abstract class Doctrine_Export_Schema
file_put_contents($schema, $data);
}
public function getDirectoryTables($directory)
{
$parent = new ReflectionClass('Doctrine_Record');
$declared = get_declared_classes();
if ($directory !== null) {
foreach ((array) $directory as $dir) {
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir),
RecursiveIteratorIterator::LEAVES_ONLY);
foreach ($it as $file) {
$e = explode('.', $file->getFileName());
if (end($e) === 'php' && strpos($file->getFileName(), '.inc') === false) {
require_once $file->getPathName();
}
}
}
$declared = get_declared_classes();
$tables = array();
foreach($declared as $name)
{
$class = new ReflectionClass($name);
if ($class->isSubclassOf($parent) AND !$class->isAbstract()) {
$tables[$name] = $name;
}
}
return $tables;
}
}
/**
* buildSchema
*
......@@ -109,11 +75,11 @@ abstract class Doctrine_Export_Schema
* @param string $directory
* @return void
*/
public function buildSchema($directory)
public function buildSchema($directory, $models = array())
{
$array = array('tables' => array());
$array = array();
$tables = $this->getDirectoryTables($directory);
$loadedModels = Doctrine::loadModels($directory);
$parent = new ReflectionClass('Doctrine_Record');
......@@ -122,7 +88,11 @@ abstract class Doctrine_Export_Schema
// we iterate trhough the diff of previously declared classes
// and currently declared classes
foreach ($tables as $name) {
foreach ($loadedModels as $name) {
if (!empty($models) && !in_array($name, $models)) {
continue;
}
$class = new ReflectionClass($name);
// check if class is an instance of Doctrine_Record and not abstract
......@@ -146,22 +116,47 @@ abstract class Doctrine_Export_Schema
}
$record = new $name();
$table = $record->getTable();
$recordTable = $record->getTable();
$data = $table->getExportableFormat();
$data = $recordTable->getExportableFormat();
$table = array();
$table['name'] = $data['tableName'];
$table['class'] = get_class($record);
$table['tableName'] = $data['tableName'];
$table['className'] = get_class($record);
foreach ($data['columns'] AS $name => $column)
{
foreach ($data['columns'] AS $name => $column) {
$data['columns'][$name]['name'] = $name;
}
$table['columns'] = $data['columns'];
$array['tables'][$data['tableName']] = $table;
$relations = $recordTable->getRelations();
foreach ($relations as $key => $relation) {
$relationData = $relation->toArray();
$relationKey = $relationData['alias'];
if (isset($relationData['refTable']) && $relationData['refTable']) {
$table['relations'][$relationKey]['refClass'] = $relationData['refTable']->getComponentName();
}
if (isset($relationData['class']) && $relationData['class'] && $relation['class'] != $relationKey) {
$table['relations'][$relationKey]['class'] = $relationData['class'];
}
$table['relations'][$relationKey]['local'] = $relationData['local'];
$table['relations'][$relationKey]['foreign'] = $relationData['foreign'];
if ($relationData['type'] === Doctrine_Relation::ONE) {
$table['relations'][$relationKey]['type'] = 'one';
} else if($relationData['type'] === Doctrine_Relation::MANY) {
$table['relations'][$relationKey]['type'] = 'many';
} else {
$table['relations'][$relationKey]['type'] = 'one';
}
}
$array[$table['className']] = $table;
}
return $array;
......@@ -174,10 +169,13 @@ abstract class Doctrine_Export_Schema
* @param string $directory
* @return void
*/
public function exportSchema($schema, $directory)
public function exportSchema($schema, $format, $directory, $models = array())
{
$array = $this->buildSchema($directory);
$className = 'Doctrine_Export_Schema_'.ucwords($format);
$export = new $className();
$array = $export->buildSchema($directory, $models);
return $this->dump($array, $schema);
return $export->dump($array, $schema);
}
}
\ No newline at end of file
......@@ -37,7 +37,7 @@
* @author Nicolas Bérard-Nault <nicobn@gmail.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
abstract class Doctrine_Import_Schema
class Doctrine_Import_Schema
{
public $relations = array();
......@@ -47,7 +47,10 @@ abstract class Doctrine_Import_Schema
* @param string $schema
* @access public
*/
abstract function parseSchema($schema);
public function parseSchema($schema)
{
throw new Doctrine_Import_Exception('This functionality is implemented by the driver');
}
/**
* parse
......@@ -72,30 +75,39 @@ abstract class Doctrine_Import_Schema
* A method to import a Schema and translate it into a Doctrine_Record object
*
* @param string $schema The file containing the XML schema
* @param string $directory The directory where the Doctrine_Record class will
* be written
* @param string $directory The directory where the Doctrine_Record class will be written
* @param array $models Optional array of models to import
*
* @access public
*/
public function importSchema($schema, $directory)
public function importSchema($schema, $format, $directory, $models = array())
{
$className = 'Doctrine_Import_Schema_'.ucwords($format);
$import = new $className();
$builder = new Doctrine_Import_Builder();
$builder->setTargetPath($directory);
$array = array();
foreach ((array) $schema AS $s) {
$array = array_merge($array, $this->parseSchema($s));
$array = array_merge($array, $import->parseSchema($s));
}
$this->buildRelationships($array);
$import->buildRelationships($array);
foreach ($array as $name => $properties) {
if (!empty($models) && !in_array($properties['className'], $models)) {
continue;
}
$options = array();
$options['className'] = $properties['className'];
$options['fileName'] = $directory.DIRECTORY_SEPARATOR.$properties['className'].'.class.php';
$columns = $properties['columns'];
$relations = isset($this->relations[$options['className']]) ? $this->relations[$options['className']]:array();
$relations = isset($import->relations[$options['className']]) ? $import->relations[$options['className']]:array();
$builder->buildRecord($options, $columns, $relations);
}
......
......@@ -52,11 +52,11 @@ class Doctrine_Import_Schema_Yml extends Doctrine_Import_Schema
{
$array = $this->parse($schema);
foreach ($array as $tableName => $table) {
foreach ($array as $className => $table) {
$columns = array();
$tableName = isset($table['tableName']) ? (string) $table['tableName']:(string) $tableName;
$className = isset($table['className']) ? (string) $table['className']:(string) $tableName;
$className = isset($table['className']) ? (string) $table['className']:(string) $className;
$tableName = isset($table['tableName']) ? (string) $table['tableName']:(string) $className;
foreach ($table['columns'] as $columnName => $field) {
......@@ -75,11 +75,11 @@ class Doctrine_Import_Schema_Yml extends Doctrine_Import_Schema
$columns[(string) $colDesc['name']] = $colDesc;
}
$tables[$tableName]['tableName'] = $tableName;
$tables[$tableName]['className'] = $className;
$tables[$className]['tableName'] = $tableName;
$tables[$className]['className'] = $className;
$tables[$tableName]['columns'] = $columns;
$tables[$tableName]['relations'] = isset($table['relations']) ? $table['relations']:array();
$tables[$className]['columns'] = $columns;
$tables[$className]['relations'] = isset($table['relations']) ? $table['relations']:array();
}
return $tables;
......
......@@ -34,8 +34,8 @@ class Doctrine_Import_Schema_Yml_TestCase extends Doctrine_UnitTestCase
{
public function testYmlImport()
{
$import = new Doctrine_Import_Schema_Yml();
$import->importSchema('schema.yml', 'classes');
$import = new Doctrine_Import_Schema();
$import->importSchema('schema.yml', 'yml', 'classes');
if ( ! file_exists('classes/User.class.php')) {
$this->fail();
......
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