From 99ae59fd417e1111cc15143e2de7dea169bed834 Mon Sep 17 00:00:00 2001 From: "Jonathan.Wage" Date: Thu, 16 Aug 2007 22:42:35 +0000 Subject: [PATCH] Changed import/export methods to importSchema() and exportSchema() --- lib/Doctrine.php | 12 +++--- lib/Doctrine/Export.php | 4 +- lib/Doctrine/Export/Schema.php | 50 +++++++++++++++++++++- lib/Doctrine/Export/Schema/Xml.php | 45 ++++++++++++++++++++ lib/Doctrine/Export/Schema/Yml.php | 28 ++++++++++++ lib/Doctrine/Import.php | 4 +- lib/Doctrine/Import/Schema.php | 34 ++++++++++----- lib/Doctrine/Import/Schema/Xml.php | 66 ++++++++++++++++++----------- lib/Doctrine/Import/Schema/Yml.php | 39 +++++++++++------ tests/Export/RecordTestCase.php | 2 +- tests/Export/Schema/XmlTestCase.php | 35 +++++++++++++++ tests/Export/Schema/YmlTestCase.php | 35 +++++++++++++++ tests/Import/Schema/XmlTestCase.php | 52 +++++++++++++++++++++++ tests/Import/Schema/YmlTestCase.php | 52 +++++++++++++++++++++++ tests/run.php | 9 ++++ tests/schema.xml | 40 +++++++++++++++++ tests/schema.yml | 0 17 files changed, 448 insertions(+), 59 deletions(-) create mode 100644 tests/Export/Schema/XmlTestCase.php create mode 100644 tests/Export/Schema/YmlTestCase.php create mode 100644 tests/Import/Schema/XmlTestCase.php create mode 100644 tests/Import/Schema/YmlTestCase.php create mode 100755 tests/schema.xml create mode 100644 tests/schema.yml diff --git a/lib/Doctrine.php b/lib/Doctrine.php index 8e98762e9..64fa08cc1 100644 --- a/lib/Doctrine.php +++ b/lib/Doctrine.php @@ -408,26 +408,26 @@ final class Doctrine } } /** - * imprt + * importSchema * method for importing existing schema to Doctrine_Record classes * * @param string $directory * @param array $info * @return boolean */ - public static function imprt($directory, array $databases = array()) + public static function importSchema($directory, array $databases = array()) { - return Doctrine_Manager::connection()->imprt->imprt($directory, $databases); + return Doctrine_Manager::connection()->import->importSchema($directory, $databases); } /** - * export + * exportSchema * method for exporting Doctrine_Record classes to a schema * * @param string $directory */ - public static function export($directory = null) + public static function exportSchema($directory = null) { - return Doctrine_Manager::connection()->export->export($directory); + return Doctrine_Manager::connection()->export->exportSchema($directory); } /** * exportSql diff --git a/lib/Doctrine/Export.php b/lib/Doctrine/Export.php index c63cd2fde..c10e516f9 100644 --- a/lib/Doctrine/Export.php +++ b/lib/Doctrine/Export.php @@ -958,7 +958,7 @@ class Doctrine_Export extends Doctrine_Connection_Module return ''; } /** - * export + * exportSchema * method for exporting Doctrine_Record classes to a schema * * if the directory parameter is given this method first iterates @@ -972,7 +972,7 @@ class Doctrine_Export extends Doctrine_Connection_Module * @param string $directory optional directory parameter * @return void */ - public function export($directory = null) + public function exportSchema($directory = null) { $sql = $this->exportSql($directory); diff --git a/lib/Doctrine/Export/Schema.php b/lib/Doctrine/Export/Schema.php index aa84e9d5f..bbe35b5c3 100644 --- a/lib/Doctrine/Export/Schema.php +++ b/lib/Doctrine/Export/Schema.php @@ -36,6 +36,54 @@ * @version $Revision: 1838 $ * @author Nicolas BĂ©rard-Nault */ -class Doctrine_Export_Schema +abstract class Doctrine_Export_Schema { + /** + * build + * + * Build the schema string to be dumped to file + * + * @param string $array + * @return void + */ + abstract function build($array); + + /** + * dump + * + * Dump the array to the schema file + * + * @param string $array + * @param string $schema + * @return void + */ + abstract function dump($array, $schema); + + /** + * buildSchema + * + * Build schema array that can be dumped to file + * + * @param string $directory + * @return void + */ + public function buildSchema($directory) + { + // we need to figure out how we can build all the model information for the passed directory/directories + return array(); + } + + /** + * exportSchema + * + * @param string $schema + * @param string $directory + * @return void + */ + public function exportSchema($schema, $directory) + { + $array = $this->buildSchema($directory); + + $this->dump($arr, $schema); + } } \ No newline at end of file diff --git a/lib/Doctrine/Export/Schema/Xml.php b/lib/Doctrine/Export/Schema/Xml.php index 0e9e2d144..fd062550f 100644 --- a/lib/Doctrine/Export/Schema/Xml.php +++ b/lib/Doctrine/Export/Schema/Xml.php @@ -31,4 +31,49 @@ */ class Doctrine_Export_Schema_Xml extends Doctrine_Export_Schema { + /** + * build + * + * Build the schema xml string to be dumped to file + * + * @param string $array + * @return void + */ + public function build($array) + { + $xml = new SimpleXMLElement(); + + foreach ($array as $tableName => $fields) { + $table = $xml->addChild('table'); + $name = $table->addChild('name', $tableName); + $declaration = $table->addChild('declaration'); + + foreach ($fields as $fieldName => $properties) { + $field = $declaration->addChild('field'); + $field->addChild('name', $fieldName); + + foreach ($properties as $key => $value) { + $field->addChild($key, $value); + } + } + } + + return $xml->asXml(); + } + + /** + * dump + * + * Dump the array to the schema file + * + * @param string $array + * @param string $schema + * @return void + */ + public function dump($array, $schema) + { + $xml = $this->build($array); + + file_put_contents($schema, $xml); + } } \ No newline at end of file diff --git a/lib/Doctrine/Export/Schema/Yml.php b/lib/Doctrine/Export/Schema/Yml.php index 896856dbd..271f1d496 100644 --- a/lib/Doctrine/Export/Schema/Yml.php +++ b/lib/Doctrine/Export/Schema/Yml.php @@ -31,4 +31,32 @@ */ class Doctrine_Export_Schema_Yml extends Doctrine_Export_Schema { + /** + * build + * + * Build the schema yml string to be dumped to file + * + * @param string $array + * @return void + */ + public function build($array) + { + return var_dump($array); + } + + /** + * dump + * + * Dump the array to the schema file + * + * @param string $arr + * @param string $schema + * @return void + */ + public function dump($arr, $schema) + { + $yml = $this->build($array); + + file_put_contents($schema, $yml); + } } \ No newline at end of file diff --git a/lib/Doctrine/Import.php b/lib/Doctrine/Import.php index edbab63af..9b40e8b96 100644 --- a/lib/Doctrine/Import.php +++ b/lib/Doctrine/Import.php @@ -175,7 +175,7 @@ class Doctrine_Import extends Doctrine_Connection_Module return $this->conn->fetchColumn($this->sql['listViews']); } /** - * imprt + * importSchema * * method for importing existing schema to Doctrine_Record classes * @@ -183,7 +183,7 @@ class Doctrine_Import extends Doctrine_Connection_Module * @param array $databases * @return array the names of the imported classes */ - public function imprt($directory, array $databases = array()) + public function importSchema($directory, array $databases = array()) { $builder = new Doctrine_Import_Builder(); $builder->setTargetPath($directory); diff --git a/lib/Doctrine/Import/Schema.php b/lib/Doctrine/Import/Schema.php index e59760662..f53fc11b1 100644 --- a/lib/Doctrine/Import/Schema.php +++ b/lib/Doctrine/Import/Schema.php @@ -40,15 +40,27 @@ abstract class Doctrine_Import_Schema { /** - * Import the schema and return it in an array + * parse + * + * Function to do the actual parsing of the file + * + * @param string $schema + * @return void + * @author Jonathan H. Wage + */ + + abstract function parse($schema); + + /** + * Parse the schema and return it in an array * * @param string $schema * @access public */ - abstract function importSchema($schema); + abstract function parseSchema($schema); /** - * import + * importSchema * * A method to import a Schema and translate it into a Doctrine_Record object * @@ -57,18 +69,20 @@ abstract class Doctrine_Import_Schema * be written * @access public */ - public function imprt($schema, $directory) + public function importSchema($schema, $directory) { $builder = new Doctrine_Import_Builder(); $builder->setTargetPath($directory); - $arr = $this->importSchema($schema); - - foreach ($arr as $name => $columns) { - $options['className'] = $name; - $options['fileName'] = $directory.DIRECTORY_SEPARATOR.$name.'.class.php'; + $array = $this->parseSchema($schema); + + foreach ($array as $name => $properties) { + $options['className'] = $properties['class']; + $options['fileName'] = $directory.DIRECTORY_SEPARATOR.$properties['class'].'.class.php'; + + $columns = $properties['columns']; $builder->buildRecord($options, $columns, array()); } } -} +} \ No newline at end of file diff --git a/lib/Doctrine/Import/Schema/Xml.php b/lib/Doctrine/Import/Schema/Xml.php index 155fe7399..4e72c7c7e 100644 --- a/lib/Doctrine/Import/Schema/Xml.php +++ b/lib/Doctrine/Import/Schema/Xml.php @@ -39,16 +39,13 @@ */ class Doctrine_Import_Schema_Xml extends Doctrine_Import_Schema { - /** - * importSchema - * - * A method to import a XML Schema and translate it into a property array. - * The function returns that property array. - * - * @param string $schema Path to the file containing the XML schema - * @return array - */ - public function importSchema($schema) + /** + * parse + * + * @param string $schema + * @return void + */ + public function parse($schema) { if (!is_readable($schema)) { throw new Doctrine_Import_Exception('Could not read schema file '. $schema); @@ -58,31 +55,50 @@ class Doctrine_Import_Schema_Xml extends Doctrine_Import_Schema throw new Doctrine_Import_Exception('Schema file '. $schema . ' is empty'); } - $xmlObj = simplexml_load_string($xmlString); + return simplexml_load_string($xmlString); + } + + /** + * parseSchema + * + * A method to parse a XML Schema and translate it into a property array. + * The function returns that property array. + * + * @param string $schema Path to the file containing the XML schema + * @return array + */ + public function parseSchema($schema) + { + $xmlObj = $this->parse($schema); // Go through all tables... foreach ($xmlObj->table as $table) { // Go through all columns... - foreach ($table->declaration->column as $column) { + foreach ($table->declaration->field as $field) { $colDesc = array( - 'name' => (string) $column->name, - 'type' => (string) $column->type, - 'ptype' => (string) $column->type, - 'length' => (int) $column->length, - 'fixed' => (int) $column->fixed, - 'unsigned' => (bool) $column->unsigned, - 'primary' => (bool) (isset($column->primary) && $column->primary), - 'default' => (string) $column->default, - 'notnull' => (bool) (isset($column->notnull) && $column->notnull), - 'autoinc' => (bool) (isset($column->autoincrement) && $column->autoincrement), + 'name' => (string) $field->name, + 'type' => (string) $field->type, + 'ptype' => (string) $field->type, + 'length' => (int) $field->length, + 'fixed' => (int) $field->fixed, + 'unsigned' => (bool) $field->unsigned, + 'primary' => (bool) (isset($field->primary) && $field->primary), + 'default' => (string) $field->default, + 'notnull' => (bool) (isset($field->notnull) && $field->notnull), + 'autoinc' => (bool) (isset($field->autoincrement) && $field->autoincrement), ); - $columns[(string) $column->name] = $colDesc; + $columns[(string) $field->name] = $colDesc; } - $tables[(string) $table->name] = $columns; + $class = $table->class ? (string) $table->class:(string) $table->name; + + $tables[(string) $table->name]['name'] = (string) $table->name; + $tables[(string) $table->name]['class'] = (string) $class; + + $tables[(string) $table->name]['columns'] = $columns; } return $tables; } -} +} \ No newline at end of file diff --git a/lib/Doctrine/Import/Schema/Yml.php b/lib/Doctrine/Import/Schema/Yml.php index d16d8d140..9d86e7273 100644 --- a/lib/Doctrine/Import/Schema/Yml.php +++ b/lib/Doctrine/Import/Schema/Yml.php @@ -39,26 +39,40 @@ */ class Doctrine_Import_Schema_Yml extends Doctrine_Import_Schema { + /** + * parse + * + * @param string $schema + * @return void + */ + public function parse($schema) + { + if (!is_readable($schema)) { + throw new Doctrine_Import_Exception('Could not read schema file '. $schema); + } + + return array(); + } + /** - * importSchema + * parseSchema * - * A method to import a Yml Schema and translate it into a property array. + * A method to parse a Yml Schema and translate it into a property array. * The function returns that property array. * * @param string $schema Path to the file containing the XML schema * @return array */ - public function importSchema($schema) - { - if (!is_readable($schema)) { - throw new Doctrine_Import_Exception('Could not read schema file '. $schema); - } - - // Need to figure out best way to have yaml loading/dumping in Doctrine - // $yamlArr = YamlLoad($schema); - + public function parseSchema($schema) + { + $array = $this->parse($schema); + + $tables = array(); + + // Not working yet + /* // Go through all tables... - foreach ($yamlArr['table'] as $table) { + foreach ($array['table'] as $table) { // Go through all columns... foreach ($table['declaration']['field'] as $field) { $colDesc = array( @@ -79,6 +93,7 @@ class Doctrine_Import_Schema_Yml extends Doctrine_Import_Schema $tables[(string) $table['name']] = $columns; } + */ return $tables; } diff --git a/tests/Export/RecordTestCase.php b/tests/Export/RecordTestCase.php index 583b71cb9..327d0f174 100644 --- a/tests/Export/RecordTestCase.php +++ b/tests/Export/RecordTestCase.php @@ -88,7 +88,7 @@ class Doctrine_Export_Record_TestCase extends Doctrine_UnitTestCase public function testExportModelFromDirectory() { - Doctrine::export(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files2'); + Doctrine::exportSchema(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files2'); $this->assertEqual($this->adapter->pop(), 'COMMIT'); $this->assertEqual($this->adapter->pop(), 'ALTER TABLE cms__category_languages ADD CONSTRAINT FOREIGN KEY (category_id) REFERENCES cms__category(id) ON DELETE CASCADE'); diff --git a/tests/Export/Schema/XmlTestCase.php b/tests/Export/Schema/XmlTestCase.php new file mode 100644 index 000000000..8756b171e --- /dev/null +++ b/tests/Export/Schema/XmlTestCase.php @@ -0,0 +1,35 @@ +. + */ + +/** + * Doctrine_Export_Schema_Xml_TestCase + * + * @package Doctrine + * @author Konsta Vesterinen + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @category Object Relational Mapping + * @link www.phpdoctrine.com + * @since 1.0 + * @version $Revision$ + */ +class Doctrine_Export_Schema_Xml_TestCase extends Doctrine_UnitTestCase +{ +} diff --git a/tests/Export/Schema/YmlTestCase.php b/tests/Export/Schema/YmlTestCase.php new file mode 100644 index 000000000..89d975042 --- /dev/null +++ b/tests/Export/Schema/YmlTestCase.php @@ -0,0 +1,35 @@ +. + */ + +/** + * Doctrine_Export_Schema_Yml_TestCase + * + * @package Doctrine + * @author Konsta Vesterinen + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @category Object Relational Mapping + * @link www.phpdoctrine.com + * @since 1.0 + * @version $Revision$ + */ +class Doctrine_Export_Schema_Yml_TestCase extends Doctrine_UnitTestCase +{ +} diff --git a/tests/Import/Schema/XmlTestCase.php b/tests/Import/Schema/XmlTestCase.php new file mode 100644 index 000000000..d957f3879 --- /dev/null +++ b/tests/Import/Schema/XmlTestCase.php @@ -0,0 +1,52 @@ +. + */ + +/** + * Doctrine_Import_Schema_Xml_TestCase + * + * @package Doctrine + * @author Konsta Vesterinen + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @category Object Relational Mapping + * @link www.phpdoctrine.com + * @since 1.0 + * @version $Revision$ + */ +class Doctrine_Import_Schema_Xml_TestCase extends Doctrine_UnitTestCase +{ + public function testXmlImport() + { + $import = new Doctrine_Import_Schema_Xml(); + $import->importSchema('schema.xml', 'classes'); + + if (!file_exists('classes/User.class.php')) { + $this->fail(); + } else { + unlink('classes/User.class.php'); + } + + if (!file_exists('classes/Group.class.php')) { + $this->fail(); + } else { + unlink('classes/Group.class.php'); + } + } +} \ No newline at end of file diff --git a/tests/Import/Schema/YmlTestCase.php b/tests/Import/Schema/YmlTestCase.php new file mode 100644 index 000000000..e982354d5 --- /dev/null +++ b/tests/Import/Schema/YmlTestCase.php @@ -0,0 +1,52 @@ +. + */ + +/** + * Doctrine_Import_Schema_Yml_TestCase + * + * @package Doctrine + * @author Konsta Vesterinen + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @category Object Relational Mapping + * @link www.phpdoctrine.com + * @since 1.0 + * @version $Revision$ + */ +class Doctrine_Import_Schema_Yml_TestCase extends Doctrine_UnitTestCase +{ + public function testYmlImport() + { + $import = new Doctrine_Import_Schema_Yml(); + $import->importSchema('schema.yml', 'classes'); + + if (!file_exists('classes/User.class.php')) { + $this->fail(); + } else { + unlink('classes/User.class.php'); + } + + if (!file_exists('classes/Group.class.php')) { + $this->fail(); + } else { + unlink('classes/Group.class.php'); + } + } +} \ No newline at end of file diff --git a/tests/run.php b/tests/run.php index 96b4b4d16..da67faa9f 100644 --- a/tests/run.php +++ b/tests/run.php @@ -326,6 +326,15 @@ $test->addTestCase(new Doctrine_Query_Cache_TestCase()); $test->addTestCase(new Doctrine_Cache_Apc_TestCase()); $test->addTestCase(new Doctrine_Query_SelectExpression_TestCase()); + +$test->addTestCase(new Doctrine_Import_Schema_Yml_TestCase()); + +$test->addTestCase(new Doctrine_Import_Schema_Xml_TestCase()); + +$test->addTestCase(new Doctrine_Export_Schema_Yml_TestCase()); + +$test->addTestCase(new Doctrine_Export_Schema_Xml_TestCase()); + /** $test->addTestCase(new Doctrine_Cache_Memcache_TestCase()); diff --git a/tests/schema.xml b/tests/schema.xml new file mode 100755 index 000000000..553510187 --- /dev/null +++ b/tests/schema.xml @@ -0,0 +1,40 @@ + + + + user + User + + + id + integer + true + true + + + username + string + 20 + true + + +
+ + + group + Group + + + id + integer + true + true + + + name + string + 20 + true + + +
+
\ No newline at end of file diff --git a/tests/schema.yml b/tests/schema.yml new file mode 100644 index 000000000..e69de29bb