| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
pake_desc('patch a database generated from DB to use i18n'); |
|---|
| 11 |
pake_task('patch-db-i18n'); |
|---|
| 12 |
pake_alias('patch', 'patch-db-i18n'); |
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
function run_patch_db_i18n($task, $args) { |
|---|
| 44 |
|
|---|
| 45 |
if(count($args) < 1) |
|---|
| 46 |
throw new Exception('Syntax is: "patch-db-i18n <application> [<suffix> [<column>]]"'); |
|---|
| 47 |
|
|---|
| 48 |
$app = $args[0]; |
|---|
| 49 |
|
|---|
| 50 |
define('SF_ROOT_DIR', sfConfig::get("sf_root_dir")); |
|---|
| 51 |
define('SF_APP', $app); |
|---|
| 52 |
define('SF_ENVIRONMENT', 'dev'); |
|---|
| 53 |
define('SF_DEBUG', false); |
|---|
| 54 |
|
|---|
| 55 |
$appDir = SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP; |
|---|
| 56 |
if(!is_dir($appDir)) |
|---|
| 57 |
throw new Exception("unknown application : $appDir"); |
|---|
| 58 |
|
|---|
| 59 |
require_once($appDir.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php'); |
|---|
| 60 |
$context = sfContext::getInstance(); |
|---|
| 61 |
|
|---|
| 62 |
if(!$context->getI18n()) |
|---|
| 63 |
throw new Exception('i18n not set in config/settings.yml'); |
|---|
| 64 |
|
|---|
| 65 |
$suffix = isset($args[1])? $args[1]: "_i18n"; |
|---|
| 66 |
|
|---|
| 67 |
$column = isset($args[2])? $args[2]: "culture"; |
|---|
| 68 |
|
|---|
| 69 |
$verbose = pakeApp::get_instance()->get_verbose(); |
|---|
| 70 |
|
|---|
| 71 |
if($verbose) echo '>> app ' . pakeApp::excerpt('using application ' . $app) . "\n"; |
|---|
| 72 |
if($verbose) echo '>> parsing schema.yml' . "\n"; |
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
$schema_path = sfConfig::get('sf_config_dir') . DIRECTORY_SEPARATOR . 'schema.yml'; |
|---|
| 78 |
if(!file_exists($schema_path)) |
|---|
| 79 |
throw new Exception("You must first generate schema.yml from the database!"); |
|---|
| 80 |
|
|---|
| 81 |
$schema = sfYaml::load($schema_path); |
|---|
| 82 |
|
|---|
| 83 |
if($verbose) echo '>> finished parsing `schema.yml`' . "\n"; |
|---|
| 84 |
|
|---|
| 85 |
if($verbose) echo '>> scanning for i18n tables' . "\n"; |
|---|
| 86 |
|
|---|
| 87 |
$table_names = array_keys($schema['propel']); |
|---|
| 88 |
$pairs = array(); |
|---|
| 89 |
|
|---|
| 90 |
foreach($table_names as $name) { |
|---|
| 91 |
if(in_array($name . $suffix, $table_names)) { |
|---|
| 92 |
$pairs[] = array("parent" => $name, "child" => $name . $suffix); |
|---|
| 93 |
} |
|---|
| 94 |
} |
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
if($verbose) { |
|---|
| 98 |
echo '>> tables to patch:' . "\n"; |
|---|
| 99 |
foreach($pairs as $pair) { |
|---|
| 100 |
echo ' - "' . $pair['parent'] . '" and "' . $pair['child'] . '"' . "\n"; |
|---|
| 101 |
} |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
if($verbose) echo '>> appending i18n parameters to tables' . "\n"; |
|---|
| 105 |
|
|---|
| 106 |
$parent = null; |
|---|
| 107 |
$child = null; |
|---|
| 108 |
|
|---|
| 109 |
foreach($pairs as $pair) { |
|---|
| 110 |
$parent =& $schema['propel'][$pair["parent"]]; |
|---|
| 111 |
$child =& $schema['propel'][$pair["child"]]; |
|---|
| 112 |
|
|---|
| 113 |
$parent['_attributes']['isI18n'] = 'true'; |
|---|
| 114 |
$parent['_attributes']['i18nTable'] = $pair['child']; |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
$parent['_attributes']['baseClass'] = 'i18nBaseClass'; |
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
if(!isset($child[$column])) |
|---|
| 121 |
$child[$column] = array(); |
|---|
| 122 |
$child[$column]['isCulture'] = 'true'; |
|---|
| 123 |
|
|---|
| 124 |
$child[$column]['type'] = 'varchar'; |
|---|
| 125 |
$child[$column]['size'] = 7; |
|---|
| 126 |
$child[$column]['required'] = 'true'; |
|---|
| 127 |
$child[$column]['primaryKey'] = 'true'; |
|---|
| 128 |
*/ |
|---|
| 129 |
} |
|---|
| 130 |
|
|---|
| 131 |
if($verbose) echo '>> finished appending, writing files' . "\n"; |
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
if(!copy($schema_path, $schema_path . '.bak')) |
|---|
| 135 |
throw new Exception("Could not back up the original `schema.yml`!"); |
|---|
| 136 |
else |
|---|
| 137 |
if($verbose) echo 'file+ ' . pakeApp::excerpt($schema_path . '.bak') . "\n"; |
|---|
| 138 |
|
|---|
| 139 |
if(file_put_contents($schema_path, sfYaml::dump($schema)) === false) |
|---|
| 140 |
throw new Exception("Could not write modified `schema.yml` file!"); |
|---|
| 141 |
else |
|---|
| 142 |
if($verbose) echo 'file* ' . pakeApp::excerpt($schema_path) . "\n"; |
|---|
| 143 |
|
|---|
| 144 |
if($verbose) echo '>> finished modifying schema.yml' . "\n"; |
|---|
| 145 |
|
|---|
| 146 |
echo 'Please run `propel-build-model` and `symfony cc` to generate the modified Propel classes.' . "\n"; |
|---|
| 147 |
} |
|---|