Development

Changeset 17980 for plugins/sfWorkbenchPlugin

You must first sign up to be able to contribute.

Show
Ignore:
Timestamp:
05/06/09 15:34:28 (4 years ago)
Author:
COil
Message:

[sfWorkbenchPlugin] Basic parsing + external tables

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfWorkbenchPlugin/branches/1.2/lib/task/sfWorkbenchBaseTask.php

    r17953 r17980  
    22 
    33/** 
    4  * Base MWB task.  
     4 * Base MWB task. 
    55 *  
    66 * @author  COil - <qrf_coil]at[yahoo]dot[.com>  
    7  * @since   0.5.0 - 4 apr 2009 
     7 * @since   0.8.0 - 6 may 2009 
    88 */ 
    99 
    1010abstract class sfWorkbenchBaseTask extends sfBaseTask 
    1111{ 
     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 
    1244  /** 
    1345   * Check parameters and raise at the first incorrect one. 
     
    1850 
    1951  /** 
    20    * Main transformation function
     52   * Delete external tables from final YML file
    2153   */ 
    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() 
    3055  { 
    3156    // Delete external tables 
     
    3560 
    3661      $this->log(' - Removing external tables'); 
    37       foreach ($this->external_tables_array as $external_table) 
     62      foreach ($this->tables as $key => $value) 
    3863      { 
    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        } 
    4269      } 
    4370    } 
     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); 
    4481     
    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     
    5388    $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  }   
    5893 
    5994  /** 
     
    78113        continue; 
    79114      } 
     115 
    80116      $this->$option = $options[$option]; 
    81117    } 
    82118  } 
     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  }   
    83138} 
  • plugins/sfWorkbenchPlugin/branches/1.2/lib/task/sfWorkbenchToPropelTask.class.php

    r17959 r17980  
    22 
    33/** 
    4  * Task that transforms a MySQL Workbench file to a valid Propel schema.yml file. 
     4 * Task that transforms a MySQL Workbench file to a valid Propel YML file. 
    55 *  
    6  * Automatically transforms: 
    7  *  - TODO 
     6 * Automatically handles: 
     7 *  - External tables 
    88 *  
    99 * @author  COil - <qrf_coil]at[yahoo]dot[.com>  
    10  * @since   0.5.0 - 4 apr 2009 
     10 * @since   0.5.0 - 4 may 2009 
    1111 */ 
    1212 
    1313class sfWorkbenchToPropelTask extends sfWorkbenchBaseTask 
    1414{ 
    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 
    4517  /** 
    4618   * @see sfTask 
     
    5123      new sfCommandArgument('application', sfCommandArgument::REQUIRED, 'The application name'), 
    5224    )); 
    53      
     25 
    5426    $this->addOptions(array( 
    5527      new sfCommandOption('env',        null, sfCommandOption::PARAMETER_OPTIONAL, 'The environment name', self::DEFAULT_ENV_OPTION), 
    5628      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), 
    5933      new sfCommandOption('output',     null, sfCommandOption::PARAMETER_OPTIONAL, 'Output base path', self::DEFAULT_OUTPUT_OPTION), 
    6034      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), 
    6135      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 exemple', 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), 
    6337    )); 
    6438 
     
    11892    $this->log($message); 
    11993 
    120     // 1 - Extract xml wb schema 
     94    // 1 - Extract xml file from WB schema 
    12195    $this->extractWbXml(); 
    12296     
    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(); 
    128111         
    129     // 4 - End 
     112    // 6 - End 
    130113    $this->logSection('END'); 
    131114    $message = ' - End time: '. $this->getCurrentDateTime(); 
     
    134117   
    135118  /** 
    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   /** 
    157119   * Transforms the MWB schema to a Propel one. 
    158120   */ 
    159   function doConvert() 
     121  protected function extractTables() 
    160122  {     
    161123    $dom = new domDocument(); 
     
    197159    foreach ($tables as $key => $value) 
    198160    { 
    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')); 
    202165       
     166      $cpt2 = 0; 
    203167      foreach ($nodes as $node) 
    204168      { 
    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  
    206173        foreach(self::$column_keys as $column_key) 
    207174        { 
    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; 
    210178        } 
    211         //myTools::dump2('column', 'column', 0); 
     179         
     180       $cpt2++; 
    212181      } 
    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   
    219224    // Automatised i18n 
    220225    $i18nlist = array(); 
     
    233238    } 
    234239 
    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 */