protected function schemaToXML($checkSchema = self::CHECK_SCHEMA, $prefix = '', $verbose = false)
 {
     $finder = sfFinder::type('file')->name('*schema.yml')->prune('doctrine');
     $dirs = array_merge(array(sfConfig::get('sf_config_dir')), $this->configuration->getPluginSubPaths('/config'));
     $schemas = $finder->in($dirs);
     if (self::CHECK_SCHEMA === $checkSchema && !count($schemas)) {
         throw new sfCommandException('You must create a schema.yml file.');
     }
     $dbSchema = new sfPropelDatabaseSchema();
     foreach ($schemas as $schema) {
         $this->logSection('convert-xml', $schema);
         $schemaArray = sfYaml::load($schema);
         if (!is_array($schemaArray)) {
             continue;
             // No defined schema here, skipping
         }
         if (!isset($schemaArray['classes'])) {
             // Old schema syntax: we convert it
             $schemaArray = $dbSchema->convertOldToNewYaml($schemaArray);
         }
         $customSchemaFilename = str_replace(array(str_replace(DIRECTORY_SEPARATOR, '/', sfConfig::get('sf_root_dir')) . '/', 'plugins/', 'config/', '/', 'schema.yml'), array('', '', '', '_', 'schema.custom.yml'), $schema);
         $customSchemas = sfFinder::type('file')->name($customSchemaFilename)->in($dirs);
         foreach ($customSchemas as $customSchema) {
             if ($verbose) {
                 $this->logSection('schema', sprintf('  found custom schema %s', $customSchema), null, 'COMMENT');
             }
             $customSchemaArray = sfYaml::load($customSchema);
             if (!isset($customSchemaArray['classes'])) {
                 // Old schema syntax: we convert it
                 $customSchemaArray = $dbSchema->convertOldToNewYaml($customSchemaArray);
             }
             $schemaArray = sfToolkit::arrayDeepMerge($schemaArray, $customSchemaArray);
         }
         $dbSchema->loadArray($schemaArray);
         if ($verbose) {
             $this->logSection('schema', sprintf('  converting "%s" to XML', $schema), null, 'COMMENT');
         }
         $localprefix = $prefix;
         // change prefix for plugins
         foreach ($this->configuration->getPlugins() as $plugin) {
             $plugin_config = $this->configuration->getPluginConfiguration((string) $plugin);
             $plugin_root_dir = rtrim($plugin_config->getRootDir(), '/') . '/';
             // if $schema starts with $plugin_root_dir
             if (substr($schema, 0, strlen($plugin_root_dir)) === $plugin_root_dir) {
                 $localprefix = $prefix . $plugin_config->getName() . '-';
                 break;
             }
         }
         // save converted xml files in original directories
         $xml_file_name = str_replace('.yml', '.xml', basename($schema));
         $file = str_replace(basename($schema), $localprefix . $xml_file_name, $schema);
         if ($verbose) {
             $this->logSection('schema', sprintf('  putting %s', $file), null, 'COMMENT');
         }
         file_put_contents($file, $dbSchema->asXML());
     }
 }
Exemplo n.º 2
0
 protected function schemaToXML($checkSchema = self::CHECK_SCHEMA, $prefix = '')
 {
     $finder = sfFinder::type('file')->name('*schema.yml');
     $dirs = array('config');
     if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir') . '/*/config')) {
         $dirs = array_merge($dirs, $pluginDirs);
     }
     $schemas = $finder->in($dirs);
     if (self::CHECK_SCHEMA === $checkSchema && !count($schemas)) {
         throw new sfCommandException('You must create a schema.yml file.');
     }
     $dbSchema = new sfPropelDatabaseSchema();
     foreach ($schemas as $schema) {
         $schemaArray = sfYaml::load($schema);
         if (!is_array($schemaArray)) {
             continue;
             // No defined schema here, skipping
         }
         if (!isset($schemaArray['classes'])) {
             // Old schema syntax: we convert it
             $schemaArray = $dbSchema->convertOldToNewYaml($schemaArray);
         }
         $customSchemaFilename = str_replace(array(str_replace(DIRECTORY_SEPARATOR, '/', sfConfig::get('sf_root_dir')) . '/', 'plugins/', 'config/', '/', 'schema.yml'), array('', '', '', '_', 'schema.custom.yml'), $schema);
         $customSchemas = sfFinder::type('file')->name($customSchemaFilename)->in($dirs);
         foreach ($customSchemas as $customSchema) {
             $this->logSection('schema', sprintf('found custom schema %s', $customSchema));
             $customSchemaArray = sfYaml::load($customSchema);
             if (!isset($customSchemaArray['classes'])) {
                 // Old schema syntax: we convert it
                 $customSchemaArray = $dbSchema->convertOldToNewYaml($customSchemaArray);
             }
             $schemaArray = sfToolkit::arrayDeepMerge($schemaArray, $customSchemaArray);
         }
         $dbSchema->loadArray($schemaArray);
         $this->logSection('schema', sprintf('converting "%s" to XML', $schema));
         $localprefix = $prefix;
         // change prefix for plugins
         if (preg_match('#plugins[/\\\\]([^/\\\\]+)[/\\\\]#', $schema, $match)) {
             $localprefix = $prefix . $match[1] . '-';
         }
         // save converted xml files in original directories
         $xml_file_name = str_replace('.yml', '.xml', basename($schema));
         $file = str_replace(basename($schema), $localprefix . $xml_file_name, $schema);
         $this->logSection('schema', sprintf('putting %s', $file));
         file_put_contents($file, $dbSchema->asXML());
     }
 }
$t = new my_lime_test(409);
$t->diag('Classical YAML to XML conversion');
$p = new sfPropelDatabaseSchema();
$p->loadYAML(dirname(__FILE__) . '/fixtures/schema.yml');
$target = file_get_contents(dirname(__FILE__) . '/fixtures/schema.xml');
$t->is_line_by_line($p->asXML(), $target);
$t->diag('New YAML to XML conversion');
$p = new sfPropelDatabaseSchema();
$p->loadYAML(dirname(__FILE__) . '/fixtures/new_schema.yml');
$target = file_get_contents(dirname(__FILE__) . '/fixtures/schema.xml');
$t->is_line_by_line($p->asXML(), $target);
$t->diag('New YAML to Old YAML conversion');
$old_yml_target = sfYaml::load(dirname(__FILE__) . '/fixtures/schema.yml');
$p = new sfPropelDatabaseSchema();
$new_yml_transformed = $p->convertNewToOldYaml(sfYaml::load(dirname(__FILE__) . '/fixtures/new_schema.yml'));
$t->is_array_explicit($new_yml_transformed, $old_yml_target);
$t->diag('Old YAML to New YAML conversion');
$new_yml_target = sfYaml::load(dirname(__FILE__) . '/fixtures/new_schema.yml');
$p = new sfPropelDatabaseSchema();
$old_yml_transformed = $p->convertOldToNewYaml(sfYaml::load(dirname(__FILE__) . '/fixtures/schema.yml'));
$t->is_array_explicit($old_yml_transformed, $new_yml_target);
$t->todo('XML and classical YAML internal representation');
$p1 = new sfPropelDatabaseSchema();
$p1->loadXML(dirname(__FILE__) . '/fixtures/schema.xml');
$p2 = new sfPropelDatabaseSchema();
$p2->loadYAML(dirname(__FILE__) . '/fixtures/schema.yml');
//$t->is_array_explicit($p1->asArray(), $p2->asArray());
$t->todo('XML and classical YAML compared as XML');
//$t->is_line_by_line($p1->asXML(), $p2->asXML());
$t->todo('XML and classical YAML compared as YAML');
//$t->is_line_by_line($p1->asYAML(), $p2->asYAML());
 /**
  * Getting schemas array from configs
  * 
  * @return array - Connections and tables
  * @author Sergey Startsev
  */
 private static function getSchemas()
 {
     $aPropelSchemaArray = array();
     $configuration = new ProjectConfiguration(null, new sfEventDispatcher());
     $db_schema = new sfPropelDatabaseSchema();
     $dirs = array_merge(array(sfConfig::get('sf_config_dir')), $configuration->getPluginSubPaths('/config'));
     foreach ($dirs as $k => $dir) {
         if (substr_count($dir, 'appFlower') > 0 || substr_count($dir, 'sfPropelPlugin') > 0 || substr_count($dir, 'sfProtoculousPlugin') > 0) {
             unset($dirs[$k]);
         }
     }
     $dirs = array_values($dirs);
     $schemas = sfFinder::type('file')->name('*schema.yml')->prune('doctrine')->in($dirs);
     foreach ($schemas as $schema_path) {
         $schema = DIRECTORY_SEPARATOR != '/' ? str_replace('/', DIRECTORY_SEPARATOR, $schema_path) : $schema_path;
         $aOriginalSchemaArray[$schema] = sfYaml::load($schema);
         if (!is_array($aOriginalSchemaArray[$schema])) {
             $aOriginalSchemaArray[$schema];
             continue;
             // No defined schema here, skipping
         }
         if (!isset($aOriginalSchemaArray[$schema]['classes'])) {
             // Old schema syntax: we convert it
             $aPropelSchemaArray[$schema] = $db_schema->convertOldToNewYaml($aOriginalSchemaArray[$schema]);
         }
         $customSchemaFilename = str_replace(array(str_replace(DIRECTORY_SEPARATOR, '/', sfConfig::get('sf_root_dir')) . '/', 'plugins/', 'config/', '/', 'schema.yml'), array('', '', '', '_', 'schema.custom.yml'), $schema);
         $customSchemas = sfFinder::type('file')->name($customSchemaFilename)->in($dirs);
         foreach ($customSchemas as $customSchema) {
             $aOriginalSchemaArray[$customSchema] = sfYaml::load($customSchema);
             if (!isset($aOriginalSchemaArray[$customSchema]['classes'])) {
                 // Old schema syntax: we convert it
                 $aPropelSchemaArray[$customSchema] = $db_schema->convertOldToNewYaml(${$aOriginalSchemaArray}[$customSchema]);
             }
         }
     }
     return $aPropelSchemaArray;
 }