/** * @see sfTask */ protected function execute($arguments = array(), $options = array()) { $config = $this->getCliConfig(); $pluginSchemaDirectories = glob(sfConfig::get('sf_plugins_dir') . DIRECTORY_SEPARATOR . '*' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'doctrine'); $pluginSchemas = sfFinder::type('file')->name('*.yml')->in($pluginSchemaDirectories); $tmpPath = sfConfig::get('sf_cache_dir') . DIRECTORY_SEPARATOR . 'tmp'; if (!file_exists($tmpPath)) { Doctrine_Lib::makeDirectories($tmpPath); } foreach ($pluginSchemas as $schema) { $schema = str_replace('/', DIRECTORY_SEPARATOR, $schema); $plugin = str_replace(sfConfig::get('sf_plugins_dir') . DIRECTORY_SEPARATOR, '', $schema); $e = explode(DIRECTORY_SEPARATOR, $plugin); $plugin = $e[0]; $name = basename($schema); $tmpSchemaPath = $tmpPath . DIRECTORY_SEPARATOR . $plugin . '-' . $name; $models = Doctrine_Parser::load($schema, 'yml'); if (!isset($models['package'])) { $models['package'] = $plugin . '.lib.model.doctrine'; } Doctrine_Parser::dump($models, 'yml', $tmpSchemaPath); } $import = new Doctrine_Import_Schema(); $import->setOption('generateBaseClasses', true); $import->setOption('generateTableClasses', true); $import->setOption('packagesPath', sfConfig::get('sf_plugins_dir')); $import->setOption('packagesPrefix', 'Plugin'); $import->setOption('suffix', '.class.php'); $import->setOption('baseClassesDirectory', 'generated'); $import->setOption('baseClassName', 'sfDoctrineRecord'); $import->importSchema(array($tmpPath, $config['yaml_schema_path']), 'yml', $config['models_path']); $this->dispatcher->notify(new sfEvent($this, 'command.log', array($this->formatter->formatSection('doctrine', 'Generated models successfully')))); }
public function testImportOfHieriarchyOfPluginGeneration() { $yml = <<<END --- WikiTest: actAs: I18n: fields: [title, content] actAs: Versionable: fields: [title, content] Searchable: fields: [title, content] Sluggable: fields: [title] columns: title: string(255) content: string END; file_put_contents('wiki.yml', $yml); $path = dirname(__FILE__) . '/tmp/import_builder_test'; $import = new Doctrine_Import_Schema(); $import->setOption('generateTableClasses', true); $import->importSchema('wiki.yml', 'yml', $path); // check that the plugin hierarchy will produce the right sql statements // this is almost an end-to-end testing :-) $models = Doctrine::loadModels($path, Doctrine::MODEL_LOADING_CONSERVATIVE); $sql = $this->conn->export->exportClassesSql(array('WikiTest')); $result = array( 0 => 'CREATE TABLE wiki_test_translation_version (id INTEGER, lang CHAR(2), title VARCHAR(255), content VARCHAR(2147483647), version INTEGER, PRIMARY KEY(id, lang, version))', 1 => 'CREATE TABLE wiki_test_translation_index (id INTEGER, lang CHAR(2), keyword VARCHAR(200), field VARCHAR(50), position INTEGER, PRIMARY KEY(id, lang, keyword, field, position))', 2 => 'CREATE TABLE wiki_test_translation (id INTEGER, title VARCHAR(255), content VARCHAR(2147483647), lang CHAR(2), version INTEGER, slug VARCHAR(255), PRIMARY KEY(id, lang))', 3 => 'CREATE TABLE wiki_test (id INTEGER PRIMARY KEY AUTOINCREMENT)', 4 => 'CREATE INDEX sluggable_idx ON wiki_test_translation (slug)', ); foreach($sql as $idx => $req) { $this->assertEqual($req, $result[$idx]); } Doctrine_Lib::removeDirectories($path); unlink('wiki.yml'); }
public function testInheritanceGeneration() { $path = dirname(__FILE__) . '/import_builder_test'; $import = new Doctrine_Import_Schema(); $import->setOption('generateTableClasses', true); $import->importSchema('schema.yml', 'yml', $path); $models = Doctrine_Core::loadModels($path, Doctrine_Core::MODEL_LOADING_CONSERVATIVE); $schemaTestInheritanceParent = new ReflectionClass('SchemaTestInheritanceParent'); $schemaTestInheritanceChild1 = new ReflectionClass('SchemaTestInheritanceChild1'); $schemaTestInheritanceChild2 = new ReflectionClass('SchemaTestInheritanceChild2'); $schemaTestInheritanceParentTable = new ReflectionClass('SchemaTestInheritanceParentTable'); $schemaTestInheritanceChild1Table = new ReflectionClass('SchemaTestInheritanceChild1Table'); $schemaTestInheritanceChild2Table = new ReflectionClass('SchemaTestInheritanceChild2Table'); $this->assertTrue($schemaTestInheritanceParent->isSubClassOf('Doctrine_Record')); $this->assertTrue($schemaTestInheritanceParent->isSubClassOf('BaseSchemaTestInheritanceParent')); $this->assertTrue($schemaTestInheritanceParent->isSubClassOf('PackageSchemaTestInheritanceParent')); $this->assertTrue($schemaTestInheritanceChild1->isSubClassOf('BaseSchemaTestInheritanceChild1')); $this->assertTrue($schemaTestInheritanceChild2->isSubClassOf('BaseSchemaTestInheritanceChild2')); $this->assertTrue($schemaTestInheritanceChild1->isSubClassOf('SchemaTestInheritanceParent')); $this->assertTrue($schemaTestInheritanceChild1->isSubClassOf('BaseSchemaTestInheritanceParent')); $this->assertTrue($schemaTestInheritanceChild2->isSubClassOf('SchemaTestInheritanceParent')); $this->assertTrue($schemaTestInheritanceChild2->isSubClassOf('BaseSchemaTestInheritanceParent')); $this->assertTrue($schemaTestInheritanceChild2->isSubClassOf('SchemaTestInheritanceChild1')); $this->assertTrue($schemaTestInheritanceChild2->isSubClassOf('BaseSchemaTestInheritanceChild1')); $this->assertTrue($schemaTestInheritanceChild2->isSubClassOf('PackageSchemaTestInheritanceParent')); $this->assertTrue($schemaTestInheritanceParentTable->isSubClassOf('Doctrine_Table')); $this->assertTrue($schemaTestInheritanceChild1Table->isSubClassOf('SchemaTestInheritanceParentTable')); $this->assertTrue($schemaTestInheritanceChild1Table->isSubClassOf('PackageSchemaTestInheritanceParentTable')); $this->assertTrue($schemaTestInheritanceChild2Table->isSubClassOf('SchemaTestInheritanceParentTable')); $this->assertTrue($schemaTestInheritanceChild2Table->isSubClassOf('PackageSchemaTestInheritanceParentTable')); $this->assertTrue($schemaTestInheritanceChild2Table->isSubClassOf('SchemaTestInheritanceChild1Table')); $this->assertTrue($schemaTestInheritanceChild2Table->isSubClassOf('PackageSchemaTestInheritanceChild1Table')); # Simple Inheritance $schemaTestSimpleInheritanceParent = new ReflectionClass('SchemaTestSimpleInheritanceParent'); $schemaTestSimpleInheritanceChild = new ReflectionClass('SchemaTestSimpleInheritanceChild'); $this->assertTrue($schemaTestSimpleInheritanceParent->hasMethod('setTableDefinition')); $this->assertTrue($schemaTestSimpleInheritanceChild->isSubClassOf('SchemaTestSimpleInheritanceParent')); # Class Table Inheritance $schemaTestClassTableInheritanceParent = new ReflectionClass('SchemaTestClassTableInheritanceParent'); $schemaTestClassTableInheritanceChild = new ReflectionClass('SchemaTestClassTableInheritanceChild'); # Concrete Inheritance $schemaTestConcreteInheritanceParent = new ReflectionClass('SchemaTestConcreteInheritanceParent'); $schemaTestConcreteInheritanceChild = new ReflectionClass('SchemaTestConcreteInheritanceChild'); # Column Aggregation Inheritance $schemaTestColumnAggregationInheritanceParent = new ReflectionClass('SchemaTestColumnAggregationInheritanceParent'); $schemaTestColumnAggregationInheritanceChild = new ReflectionClass('SchemaTestColumnAggregationInheritanceChild'); $sql = Doctrine_Core::generateSqlFromArray(array('SchemaTestSimpleInheritanceParent', 'SchemaTestSimpleInheritanceChild')); $this->assertEqual(count($sql), 1); $this->assertEqual($sql[0], 'CREATE TABLE schema_test_simple_inheritance_parent (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(255), description VARCHAR(255))'); $sql = Doctrine_Core::generateSqlFromArray(array('SchemaTestClassTableInheritanceParent', 'SchemaTestClassTableInheritanceChild')); $this->assertEqual(count($sql), 2); $this->assertEqual($sql[0], 'CREATE TABLE schema_test_class_table_inheritance_parent (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(255))'); $this->assertEqual($sql[1], 'CREATE TABLE schema_test_class_table_inheritance_child (id INTEGER, title VARCHAR(255), description VARCHAR(255), PRIMARY KEY(id))'); $sql = Doctrine_Core::generateSqlFromArray(array('SchemaTestConcreteInheritanceParent', 'SchemaTestConcreteInheritanceChild')); $this->assertEqual(count($sql), 2); $this->assertEqual($sql[0], 'CREATE TABLE schema_test_concrete_inheritance_parent (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(255))'); $this->assertEqual($sql[1], 'CREATE TABLE schema_test_concrete_inheritance_child (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(255), title VARCHAR(255), description VARCHAR(255))'); $sql = Doctrine_Core::generateSqlFromArray(array('SchemaTestColumnAggregationInheritanceParent', 'SchemaTestColumnAggregationInheritanceChild')); $this->assertEqual(count($sql), 2); $this->assertEqual($sql[0], 'CREATE TABLE schema_test_column_aggregation_inheritance_parent (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(255), type VARCHAR(255), title VARCHAR(255), description VARCHAR(255))'); $this->assertEqual($sql[1], 'CREATE INDEX schema_test_column_aggregation_inheritance_parent_type_idx ON schema_test_column_aggregation_inheritance_parent (type)'); Doctrine_Lib::removeDirectories($path); }