/** * @see sfTask */ protected function execute($arguments = array(), $options = array()) { $this->logSection('doctrine', 'generating model classes'); $config = $this->getCliConfig(); $this->_checkForPackageParameter($config['yaml_schema_path']); $tmpPath = sfConfig::get('sf_cache_dir') . DIRECTORY_SEPARATOR . 'tmp'; if (!file_exists($tmpPath)) { Doctrine_Lib::makeDirectories($tmpPath); } $plugins = $this->configuration->getPlugins(); foreach ($this->configuration->getAllPluginPaths() as $plugin => $path) { if (!in_array($plugin, $plugins)) { continue; } $schemas = sfFinder::type('file')->name('*.yml')->in($path . '/config/doctrine'); foreach ($schemas as $schema) { $tmpSchemaPath = $tmpPath . DIRECTORY_SEPARATOR . $plugin . '-' . basename($schema); $models = Doctrine_Parser::load($schema, 'yml'); if (!isset($models['package'])) { $models['package'] = $plugin . '.lib.model.doctrine'; $models['package_custom_path'] = $path . '/lib/model/doctrine'; } Doctrine_Parser::dump($models, 'yml', $tmpSchemaPath); } } $options = array('generateBaseClasses' => true, 'generateTableClasses' => true, 'packagesPath' => sfConfig::get('sf_plugins_dir'), 'packagesPrefix' => 'Plugin', 'suffix' => '.class.php', 'baseClassesDirectory' => 'base', 'baseClassName' => 'sfDoctrineRecord'); $options = array_merge($options, sfConfig::get('doctrine_model_builder_options', array())); $import = new Doctrine_Import_Schema(); $import->setOptions($options); $import->importSchema(array($tmpPath, $config['yaml_schema_path']), 'yml', $config['models_path']); }
/** * @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')))); }
/** * writeTableClassDefinition * * @return void */ public function writeTableClassDefinition(array $definition, $path, $options = array()) { $className = $definition['tableClassName']; $pos = strpos($className, "Model_"); $fileName = substr($className, $pos + 6) . $this->_suffix; $writePath = $path . DIRECTORY_SEPARATOR . $fileName; $content = $this->buildTableClassDefinition($className, $definition, $options); Doctrine_Lib::makeDirectories(dirname($writePath)); Doctrine_Core::loadModel($className, $writePath); if (!file_exists($writePath)) { file_put_contents($writePath, $content); } }
/** * Get array of configuration variables for the Doctrine cli * * @return array $config */ public function getCliConfig() { $pluginDirs = glob(sfConfig::get('sf_root_dir') . '/plugins/*/data'); $fixtures = sfFinder::type('dir')->name('fixtures')->in(array_merge(array(sfConfig::get('sf_data_dir')), $pluginDirs)); $models = sfConfig::get('sf_lib_dir') . DIRECTORY_SEPARATOR . 'model' . DIRECTORY_SEPARATOR . 'doctrine'; $migrations = sfConfig::get('sf_lib_dir') . DIRECTORY_SEPARATOR . 'migration' . DIRECTORY_SEPARATOR . 'doctrine'; $sql = sfConfig::get('sf_data_dir') . DIRECTORY_SEPARATOR . 'sql'; $yaml = sfConfig::get('sf_config_dir') . DIRECTORY_SEPARATOR . 'doctrine'; $config = array('data_fixtures_path' => $fixtures, 'models_path' => $models, 'migrations_path' => $migrations, 'sql_path' => $sql, 'yaml_schema_path' => $yaml); foreach ($config as $dir) { $dirs = (array) $dir; foreach ($dirs as $dir) { Doctrine_Lib::makeDirectories($dir); } } return $config; }
/** * @see sfTask */ protected function execute($arguments = array(), $options = array()) { $databaseManager = new sfDatabaseManager($this->configuration); $config = $this->getCliConfig(); $dir = sfConfig::get('sf_data_dir') . DIRECTORY_SEPARATOR . 'fixtures'; Doctrine_Lib::makeDirectories($dir); $args = array(); if (isset($arguments['target'])) { $filename = $arguments['target']; if (!sfToolkit::isPathAbsolute($filename)) { $filename = $dir . DIRECTORY_SEPARATOR . $filename; } Doctrine_Lib::makeDirectories(dirname($filename)); $args = array('data_fixtures_path' => array($filename)); $this->logSection('doctrine', sprintf('dumping data to fixtures to "%s"', $filename)); } else { $this->logSection('doctrine', sprintf('dumping data to fixtures to "%s"', $config['data_fixtures_path'][0])); } $this->callDoctrineCli('dump-data', $args); }
public function testTest() { $from = dirname(__FILE__) . '/Diff/schema/from.yml'; $to = dirname(__FILE__) . '/Diff/schema/to.yml'; $migrationsPath = dirname(__FILE__) . '/Diff/migrations'; Doctrine_Lib::makeDirectories($migrationsPath); $diff = new Doctrine_Migration_Diff($from, $to, $migrationsPath); $changes = $diff->generateChanges(); $this->assertEqual($changes['dropped_tables']['homepage']['tableName'], 'homepage'); $this->assertEqual($changes['created_tables']['blog_post']['tableName'], 'blog_post'); $this->assertEqual($changes['created_columns']['profile']['user_id'], array('type' => 'integer', 'length' => 8)); $this->assertEqual($changes['dropped_columns']['user']['homepage_id'], array('type' => 'integer', 'length' => 8)); $this->assertEqual($changes['dropped_columns']['user']['profile_id'], array('type' => 'integer', 'length' => 8)); $this->assertEqual($changes['changed_columns']['user']['username'], array('type' => 'string', 'length' => 255, 'unique' => true, 'notnull' => true)); $this->assertEqual($changes['created_foreign_keys']['profile']['profile_user_id_user_id']['local'], 'user_id'); $this->assertEqual($changes['created_foreign_keys']['blog_post']['blog_post_user_id_user_id']['local'], 'user_id'); $this->assertEqual($changes['dropped_foreign_keys']['user']['user_profile_id_profile_id']['local'], 'profile_id'); $this->assertEqual($changes['dropped_foreign_keys']['user']['user_homepage_id_homepage_id']['local'], 'homepage_id'); $this->assertEqual($changes['created_indexes']['blog_post']['blog_post_user_id'], array('fields' => array('user_id'))); $this->assertEqual($changes['created_indexes']['profile']['profile_user_id'], array('fields' => array('user_id'))); $this->assertEqual($changes['dropped_indexes']['user']['is_active'], array('fields' => array('is_active'))); $diff->generateMigrationClasses(); $files = glob($migrationsPath . '/*.php'); $this->assertEqual(count($files), 2); $this->assertTrue(strpos($files[0], '_version1.php')); $this->assertTrue(strpos($files[1], '_version2.php')); $code1 = file_get_contents($files[0]); $this->assertTrue(strpos($code1, 'this->dropTable')); $this->assertTrue(strpos($code1, 'this->createTable')); $this->assertTrue(strpos($code1, 'this->removeColumn')); $this->assertTrue(strpos($code1, 'this->addColumn')); $this->assertTrue(strpos($code1, 'this->changeColumn')); $code2 = file_get_contents($files[1]); $this->assertTrue(strpos($code2, 'this->dropForeignKey')); $this->assertTrue(strpos($code2, 'this->removeIndex')); $this->assertTrue(strpos($code2, 'this->addIndex')); $this->assertTrue(strpos($code2, 'this->createForeignKey')); Doctrine_Lib::removeDirectories($migrationsPath); }
/** * Get array of configuration variables for the Doctrine cli * * @return array $config */ public function getCliConfig() { $fixtures = array(); $fixtures[] = sfConfig::get('sf_root_dir') . '/data/fixtures'; $pluginPaths = $this->configuration->getPluginPaths(); foreach ($pluginPaths as $pluginPath) { if (is_dir($dir = $pluginPath . '/data/fixtures')) { $fixtures[] = $dir; } } $models = sfConfig::get('sf_lib_dir') . DIRECTORY_SEPARATOR . 'model' . DIRECTORY_SEPARATOR . 'doctrine'; $migrations = sfConfig::get('sf_lib_dir') . DIRECTORY_SEPARATOR . 'migration' . DIRECTORY_SEPARATOR . 'doctrine'; $sql = sfConfig::get('sf_data_dir') . DIRECTORY_SEPARATOR . 'sql'; $yaml = sfConfig::get('sf_config_dir') . DIRECTORY_SEPARATOR . 'doctrine'; $config = array('data_fixtures_path' => $fixtures, 'models_path' => $models, 'migrations_path' => $migrations, 'sql_path' => $sql, 'yaml_schema_path' => $yaml); foreach ($config as $dir) { $dirs = (array) $dir; foreach ($dirs as $dir) { Doctrine_Lib::makeDirectories($dir); } } return $config; }
/** * writeDefinition * * @param array $options * @param array $columns * @param array $relations * @param array $indexes * @param array $attributes * @param array $templates * @param array $actAs * @return void */ public function writeDefinition(array $definition) { $definitionCode = $this->buildDefinition($definition); $fileName = $definition['className'] . $this->_suffix; $packagesPath = $this->_packagesPath ? $this->_packagesPath : $this->_path; // If this is a main class that either extends from Base or Package class if (isset($definition['is_main_class']) && $definition['is_main_class']) { // If is package then we need to put it in a package subfolder if (isset($definition['is_package']) && $definition['is_package']) { $writePath = $this->_path . DIRECTORY_SEPARATOR . $definition['package_name']; // Otherwise lets just put it in the root of the path } else { $writePath = $this->_path; } if ($this->generateTableClasses()) { $this->writeTableDefinition($definition['tableClassName'], $writePath, array('extends' => $definition['inheritance']['tableExtends'])); } } else { if (isset($definition['is_package_class']) && $definition['is_package_class']) { if (isset($definition['package_custom_path'])) { $writePath = $definition['package_custom_path']; } else { $writePath = $packagesPath . DIRECTORY_SEPARATOR . $definition['package_path']; } if ($this->generateTableClasses()) { $this->writeTableDefinition($definition['tableClassName'], $writePath, array('extends' => $definition['inheritance']['tableExtends'])); } } else { if (isset($definition['is_base_class']) && $definition['is_base_class']) { // If it is a part of a package then we need to put it in a package subfolder if (isset($definition['is_package']) && $definition['is_package']) { $basePath = $this->_path . DIRECTORY_SEPARATOR . $definition['package_name']; $writePath = $basePath . DIRECTORY_SEPARATOR . $this->_baseClassesDirectory; // Otherwise lets just put it in the root generated folder } else { $writePath = $this->_path . DIRECTORY_SEPARATOR . $this->_baseClassesDirectory; } } } } // If we have a writePath from the if else conditionals above then use it if (isset($writePath)) { Doctrine_Lib::makeDirectories($writePath); $writePath .= DIRECTORY_SEPARATOR . $fileName; // Otherwise none of the conditions were met and we aren't generating base classes } else { Doctrine_Lib::makeDirectories($this->_path); $writePath = $this->_path . DIRECTORY_SEPARATOR . $fileName; } $code = "<?php" . PHP_EOL; if (isset($definition['connection']) && $definition['connection']) { $code .= "// Connection Component Binding" . PHP_EOL; $code .= "Doctrine_Manager::getInstance()->bindComponent('" . $definition['connectionClassName'] . "', '" . $definition['connection'] . "');" . PHP_EOL; } $code .= PHP_EOL . $definitionCode; if (isset($definition['generate_once']) && $definition['generate_once'] === true) { if (!file_exists($writePath)) { $bytes = file_put_contents($writePath, $code); } } else { $bytes = file_put_contents($writePath, $code); } if (isset($bytes) && $bytes === false) { throw new Doctrine_Import_Builder_Exception("Couldn't write file " . $writePath); } Doctrine::loadModel($definition['className'], $writePath); }
/** * setMigrationsPath * * @param string path the path where migration classes are stored and being generated * @return */ public function setMigrationsPath($path) { Doctrine_Lib::makeDirectories($path); $this->migrationsPath = $path; }
/** * writeDefinition * * @param array $options * @param array $columns * @param array $relations * @param array $indexes * @param array $attributes * @param array $templates * @param array $actAs * @return void */ public function writeDefinition(array $options, array $columns = array(), array $relations = array(), array $indexes = array(), array $attributes = array(), array $templates = array(), array $actAs = array(), array $tableOptions = array()) { $definition = $this->buildDefinition($options, $columns, $relations, $indexes, $attributes, $templates, $actAs, $tableOptions); $fileName = $options['className'] . $this->_suffix; $packagesPath = $this->_packagesPath ? $this->_packagesPath : $this->_path; // If this is a main class that either extends from Base or Package class if (isset($options['is_main_class']) && $options['is_main_class']) { // If is package then we need to put it in a package subfolder if (isset($options['is_package']) && $options['is_package']) { $writePath = $this->_path . DIRECTORY_SEPARATOR . $options['package_name']; $this->writeTableDefinition($options['className'], $writePath, array('extends' => $options['inheritance']['extends'] . 'Table')); // Otherwise lets just put it in the root of the path } else { $writePath = $this->_path; $this->writeTableDefinition($options['className'], $writePath); } } // If is the package class then we need to make the path to the complete package if (isset($options['is_package_class']) && $options['is_package_class']) { $path = str_replace('.', DIRECTORY_SEPARATOR, trim($options['package'])); $writePath = $packagesPath . DIRECTORY_SEPARATOR . $path; $this->writeTableDefinition($options['className'], $writePath); } // If it is the base class of the doctrine record definition if (isset($options['is_base_class']) && $options['is_base_class']) { // If it is a part of a package then we need to put it in a package subfolder if (isset($options['is_package']) && $options['is_package']) { $writePath = $this->_path . DIRECTORY_SEPARATOR . $options['package_name'] . DIRECTORY_SEPARATOR . $this->_baseClassesDirectory; // Otherwise lets just put it in the root generated folder } else { $writePath = $this->_path . DIRECTORY_SEPARATOR . $this->_baseClassesDirectory; } } if (isset($writePath)) { Doctrine_Lib::makeDirectories($writePath); $writePath .= DIRECTORY_SEPARATOR . $fileName; } else { Doctrine_Lib::makeDirectories($this->_path); $writePath = $this->_path . DIRECTORY_SEPARATOR . $fileName; } $code = "<?php" . PHP_EOL; if (isset($options['connection']) && $options['connection']) { $code .= "// Connection Component Binding\n"; $code .= "Doctrine_Manager::getInstance()->bindComponent('" . $options['connectionClassName'] . "', '" . $options['connection'] . "');\n"; } $code .= PHP_EOL . $definition; if (isset($options['generate_once']) && $options['generate_once'] === true) { if (!file_exists($writePath)) { $bytes = file_put_contents($writePath, $code); } } else { $bytes = file_put_contents($writePath, $code); } if (isset($bytes) && $bytes === false) { throw new Doctrine_Import_Builder_Exception("Couldn't write file " . $writePath); } }
/** * makeDirectories * * Makes the directories for a path recursively * * @param string $path * @return void */ public static function makeDirectories($path, $mode = 0777) { return Doctrine_Lib::makeDirectories($path, $mode); }