Changeset 17980 for plugins/sfWorkbenchPlugin
- Timestamp:
- 05/06/09 15:34:28 (4 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfWorkbenchPlugin/branches/1.2/lib/task/sfWorkbenchBaseTask.php
r17953 r17980 2 2 3 3 /** 4 * Base MWB task. 4 * Base MWB task. 5 5 * 6 6 * @author COil - <qrf_coil]at[yahoo]dot[.com> 7 * @since 0. 5.0 - 4 apr20097 * @since 0.8.0 - 6 may 2009 8 8 */ 9 9 10 10 abstract class sfWorkbenchBaseTask extends sfBaseTask 11 11 { 12 protected $database = array(); 13 14 // Default env options 15 const DEFAULT_ENV_OPTION = 'cli'; 16 const DEFAULT_DEBUG_OPTION = true; 17 18 // Default task options 19 const DEFAULT_FILE_DIR_OPTION = '/doc/database'; 20 const DEFAULT_FILE_OPTION = 'schema.mwb'; 21 const DEFAULT_OUTPUT_OPTION = 'schema'; 22 const DEFAULT_OUTPUT_DIR_OPTION = '/config'; 23 const DEFAULT_PACKAGE_OPTION = 'lib.model'; 24 const DEFAULT_EXTERNAL_TABLES_OPTION = ''; 25 26 // Infos to retrieve about a column 27 public static $column_keys = array( 28 'autoIncrement', 29 'characterSetName', 30 'checks', 31 'collationName', 32 'datatypeExplicitParams', 33 'defaultValue', 34 'defaultValueIsNull', 35 'flags', 36 'isNotNull', 37 'length', 38 'precision', 39 'scale', 40 'simpleType', 41 'comment' 42 ); 43 12 44 /** 13 45 * Check parameters and raise at the first incorrect one. … … 18 50 19 51 /** 20 * Main transformation function.52 * Delete external tables from final YML file. 21 53 */ 22 abstract protected function doConvert(); 23 24 /** 25 * Delete external tables from final xml. 26 * 27 * @return String 28 */ 29 protected function removeExternalTablesFromSchema($xmlstr) 54 protected function removeExternalTables() 30 55 { 31 56 // Delete external tables … … 35 60 36 61 $this->log(' - Removing external tables'); 37 foreach ($this-> external_tables_array as $external_table)62 foreach ($this->tables as $key => $value) 38 63 { 39 $this->log(' > '. $external_table); 40 $reg_exp = '/(<table name="'. trim($external_table). ')(((.)*(\s)*)*?)(<\/table>)/'; 41 $xmlstr = preg_replace($reg_exp, '', $xmlstr); 64 if (in_array($value['physical_name'], $this->external_tables_array)) 65 { 66 unset($this->tables[$key]); 67 $this->log(' > '. $value['physical_name']); 68 } 42 69 } 43 70 } 71 } 72 73 /** 74 * Extract the xml from the wb schema file. 75 */ 76 protected function extractWbXml() 77 { 78 $zip = zip_open($this->file_path); 79 $entry = zip_read($zip); 80 zip_entry_open($zip, $entry); 44 81 45 return $xmlstr; 46 } 47 48 /** 49 * Save xml schema. 50 */ 51 public function writeSchema() 52 { 82 $xml = ''; 83 while ($read = zip_entry_read($entry, 0)) 84 { 85 $xml .= $read; 86 } 87 53 88 $filesystem = $this->getFilesystem(); 54 $filesystem->touch($this-> output_path);55 @file_put_contents($this-> output_path, $this->xmlstr);56 $this->log(' - Database yml schema saved.');57 } 89 $filesystem->touch($this->file_path. '.xml'); 90 @file_put_contents($this->file_path. '.xml', $xml); 91 $this->log(' - MWB xml schema saved.'); 92 } 58 93 59 94 /** … … 78 113 continue; 79 114 } 115 80 116 $this->$option = $options[$option]; 81 117 } 82 118 } 119 120 /** 121 * see sfPropelDatabaseSchema 122 */ 123 public function asYAML() 124 { 125 return sfYaml::dump(array($this->connection_name => $this->database), 3); 126 } 127 128 /** 129 * see sfPropelDatabaseSchema 130 */ 131 protected function setIfNotSet(&$entry, $key, $value) 132 { 133 if (!isset($entry[$key])) 134 { 135 $entry[$key] = $value; 136 } 137 } 83 138 } plugins/sfWorkbenchPlugin/branches/1.2/lib/task/sfWorkbenchToPropelTask.class.php
r17959 r17980 2 2 3 3 /** 4 * Task that transforms a MySQL Workbench file to a valid Propel schema.ymlfile.4 * Task that transforms a MySQL Workbench file to a valid Propel YML file. 5 5 * 6 * Automatically transforms:7 * - TODO6 * Automatically handles: 7 * - External tables 8 8 * 9 9 * @author COil - <qrf_coil]at[yahoo]dot[.com> 10 * @since 0.5.0 - 4 apr200910 * @since 0.5.0 - 4 may 2009 11 11 */ 12 12 13 13 class sfWorkbenchToPropelTask extends sfWorkbenchBaseTask 14 14 { 15 // Default env options 16 const DEFAULT_ENV_OPTION = 'cli'; 17 const DEFAULT_DEBUG_OPTION = true; 18 19 // Default task options 20 const DEFAULT_FILE_DIR_OPTION = '/doc/database'; 21 const DEFAULT_FILE_OPTION = 'schema.mwb'; 22 const DEFAULT_OUTPUT_OPTION = 'schema'; 23 const DEFAULT_OUTPUT_DIR_OPTION = '/config'; 24 const DEFAULT_PACKAGE_OPTION = 'lib.model'; 25 const DEFAULT_EXTERNAL_TABLES_OPTION = ''; 26 27 // Infos to retrieve about a column 28 public static $column_keys = array( 29 'autoIncrement', 30 'characterSetName', 31 'checks', 32 'collationName', 33 'datatypeExplicitParams', 34 'defaultValue', 35 'defaultValueIsNull', 36 'flags', 37 'isNotNull', 38 'length', 39 'precision', 40 'scale', 41 'simpleType', 42 'comment' 43 ); 44 15 const DEFAULT_CONNECTION_NAME = 'propel'; 16 45 17 /** 46 18 * @see sfTask … … 51 23 new sfCommandArgument('application', sfCommandArgument::REQUIRED, 'The application name'), 52 24 )); 53 25 54 26 $this->addOptions(array( 55 27 new sfCommandOption('env', null, sfCommandOption::PARAMETER_OPTIONAL, 'The environment name', self::DEFAULT_ENV_OPTION), 56 28 new sfCommandOption('debug', null, sfCommandOption::PARAMETER_OPTIONAL, 'Enable debug', self::DEFAULT_DEBUG_OPTION), 57 new sfCommandOption('file_dir', null, sfCommandOption::PARAMETER_OPTIONAL, 'The base path for the db4 schema', self::DEFAULT_FILE_DIR_OPTION), 58 new sfCommandOption('file', null, sfCommandOption::PARAMETER_OPTIONAL, 'The name of the db4 file', self::DEFAULT_FILE_OPTION), 29 30 new sfCommandOption('connection_name', null, sfCommandOption::PARAMETER_OPTIONAL, 'Name of the database connection', self::DEFAULT_CONNECTION_NAME), 31 new sfCommandOption('file_dir', null, sfCommandOption::PARAMETER_OPTIONAL, 'The base path for the WB schema', self::DEFAULT_FILE_DIR_OPTION), 32 new sfCommandOption('file', null, sfCommandOption::PARAMETER_OPTIONAL, 'The name of the WB file', self::DEFAULT_FILE_OPTION), 59 33 new sfCommandOption('output', null, sfCommandOption::PARAMETER_OPTIONAL, 'Output base path', self::DEFAULT_OUTPUT_OPTION), 60 34 new sfCommandOption('output_dir', null, sfCommandOption::PARAMETER_OPTIONAL, 'Output base file name, to have schema_client.yml, put "schema_client" for this parameter', self::DEFAULT_OUTPUT_DIR_OPTION), 61 35 new sfCommandOption('package', null, sfCommandOption::PARAMETER_OPTIONAL, 'Package to use if you want to store model files in another directory than lib.model', self::DEFAULT_PACKAGE_OPTION), 62 new sfCommandOption('external_tables', null, sfCommandOption::PARAMETER_OPTIONAL, 'Coma separated list of tables that will be excluded from the final generated schema, useful for using FK to a plugin schema table for ex emple', self::DEFAULT_EXTERNAL_TABLES_OPTION),36 new sfCommandOption('external_tables', null, sfCommandOption::PARAMETER_OPTIONAL, 'Coma separated list of tables that will be excluded from the final generated schema, useful for using FK to a plugin schema table for example', self::DEFAULT_EXTERNAL_TABLES_OPTION), 63 37 )); 64 38 … … 118 92 $this->log($message); 119 93 120 // 1 - Extract xml wbschema94 // 1 - Extract xml file from WB schema 121 95 $this->extractWbXml(); 122 96 123 // 1 - Convert wb schema to Propel 124 $this->doConvert(); 125 126 // 2 - Write new Propel shema.xml 127 //$this->writeSchema(); 97 // 2 - Retrieve tables, their column and all the options from WB schema 98 $this->extractTables(); 99 100 // 4 - Delete external tables 101 $this->removeExternalTables(); 102 103 // 5 - Transform the big array to a typical schema one. 104 //$this->getYamlArray(); 105 106 // 4 - Set automatic options for i18n tables 107 //$this->processI18n(); 108 109 // 5 - Write new Propel shema.yml 110 $this->writeSchema(); 128 111 129 // 4- End112 // 6 - End 130 113 $this->logSection('END'); 131 114 $message = ' - End time: '. $this->getCurrentDateTime(); … … 134 117 135 118 /** 136 * Extract the xml from the wb schema file.137 */138 protected function extractWbXml()139 {140 $zip = zip_open($this->file_path);141 $entry = zip_read($zip);142 zip_entry_open($zip, $entry);143 144 $xml = '';145 while ($read = zip_entry_read($entry, 0))146 {147 $xml .= $read;148 }149 150 $filesystem = $this->getFilesystem();151 $filesystem->touch($this->file_path. '.xml');152 @file_put_contents($this->file_path. '.xml', $xml);153 $this->log(' - MWB xml schema saved.');154 }155 156 /**157 119 * Transforms the MWB schema to a Propel one. 158 120 */ 159 function doConvert()121 protected function extractTables() 160 122 { 161 123 $dom = new domDocument(); … … 197 159 foreach ($tables as $key => $value) 198 160 { 199 $column_path = "//value[@id='". $value['id']. "']//value[@struct-name='db.mysql.Column']"; 200 $path = $column_path. "/value[@key='%s']"; 201 $nodes = $xpath->query(sprintf($path, 'name')); 161 // Main column name 162 $column_path = "//value[@id='". $value['id']. "']//value[@struct-name='db.mysql.Column']"; 163 $keys_path = $column_path. "/value[@key='%s']"; 164 $nodes = $xpath->query(sprintf($keys_path, 'name')); 202 165 166 $cpt2 = 0; 203 167 foreach ($nodes as $node) 204 168 { 205 // Rerieve all informations about the column 169 $parent = $xpath->query(sprintf($keys_path, 'name'). '/parent::*'); 170 $tables[$cpt]['columns'][$node->textContent]['id'] = $parent->item($cpt2)->getAttribute('id'); 171 172 // Retrieve all informations about the column 206 173 foreach(self::$column_keys as $column_key) 207 174 { 208 $node_key = $xpath->query(sprintf($path, $column_key)); 209 $tables[$cpt]['columns'][$node->textContent][$column_key] = $node_key->item(0)->textContent; 175 $keys_path = $column_path. "/value[@key='%s']"; 176 $node_key = $xpath->query(sprintf($keys_path, $column_key)); 177 $tables[$cpt]['columns'][$node->textContent][$column_key] = $node_key->item($cpt2)->textContent; 210 178 } 211 //myTools::dump2('column', 'column', 0); 179 180 $cpt2++; 212 181 } 213 214 $cpt++; 215 } 216 217 myTools::dump2($tables, $tables, 1); 218 182 183 $cpt++; 184 } 185 186 $this->tables = $tables; 187 } 188 189 /** 190 * Save xml schema. 191 */ 192 public function writeSchema() 193 { 194 // Fixe database schema 195 $this->fixYAMLDatabase(); 196 197 // YML output file 198 $this->output_path = sfConfig::get('sf_root_dir'). $this->output_dir. '/'. $this->output. '.yml'; 199 200 $filesystem = $this->getFilesystem(); 201 $filesystem->touch($this->output_path); 202 @file_put_contents($this->output_path, $this->asYAML()); 203 $this->log(' - Database YML schema saved.'); 204 } 205 206 /** 207 * @see sfPropelDatabaseSchema 208 */ 209 protected function fixYAMLDatabase() 210 { 211 if (!isset($this->database['_attributes'])) 212 { 213 $this->database['_attributes'] = array(); 214 } 215 216 // conventions for database attributes 217 $this->setIfNotSet($this->database['_attributes'], 'defaultIdMethod', 'native'); 218 $this->setIfNotSet($this->database['_attributes'], 'package', $this->package); 219 } 220 } 221 222 /* 223 219 224 // Automatised i18n 220 225 $i18nlist = array(); … … 233 238 } 234 239 235 // Others replacments 236 $xmlstr = str_replace( 237 array ( 238 // Add culture attribute 239 'name="culture"', 240 241 // Model package 242 'package="lib.model"' 243 ), 244 array ( 245 // 246 'name="culture" isCulture="true"', 247 248 // 249 sprintf('package="%s"', $this->package), 250 ), 251 $xmlstr 252 ); 253 254 // Remove exernal tables 255 $xmlstr = $this->removeExternalTablesFromSchema(); 256 257 // Xml file save 258 $this->xmlstr = $xmlstr; 259 $this->output_path = sfConfig::get('sf_root_dir'). $this->output_dir. '/'. $this->output. '.xml'; 260 } 261 } 240 241 */