00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 if (!class_exists('File')) {
00028 uses('file');
00029 }
00030
00031
00032
00033
00034
00035
00036 class DbConfigTask extends Shell {
00037
00038
00039
00040
00041
00042
00043 var $path = null;
00044
00045
00046
00047
00048
00049
00050 var $__defaultConfig = array(
00051 'name' => 'default', 'driver'=> 'mysql', 'persistent'=> 'false', 'host'=> 'localhost',
00052 'login'=> 'root', 'password'=> 'password', 'database'=> 'project_name',
00053 'schema'=> null, 'prefix'=> null, 'encoding' => null, 'port' => null
00054 );
00055
00056
00057
00058
00059
00060
00061 function initialize() {
00062 $this->path = $this->params['working'] . DS . 'config' . DS;
00063 }
00064
00065
00066
00067
00068
00069 function execute() {
00070 if (empty($this->args)) {
00071 $this->__interactive();
00072 $this->_stop();
00073 }
00074 }
00075
00076
00077
00078
00079
00080 function __interactive() {
00081 $this->hr();
00082 $this->out('Database Configuration:');
00083 $this->hr();
00084 $done = false;
00085 $dbConfigs = array();
00086
00087 while ($done == false) {
00088 $name = '';
00089
00090 while ($name == '') {
00091 $name = $this->in("Name:", null, 'default');
00092 if (preg_match('/[^a-z0-9_]/i', $name)) {
00093 $name = '';
00094 $this->out('The name may only contain unaccented latin characters, numbers or underscores');
00095 }
00096 else if (preg_match('/^[^a-z_]/i', $name)) {
00097 $name = '';
00098 $this->out('The name must start with an unaccented latin character or an underscore');
00099 }
00100 }
00101 $driver = '';
00102
00103 while ($driver == '') {
00104 $driver = $this->in('Driver:', array('db2', 'firebird', 'mssql', 'mysql', 'mysqli', 'odbc', 'oracle', 'postgres', 'sqlite', 'sybase'), 'mysql');
00105 }
00106 $persistent = '';
00107
00108 while ($persistent == '') {
00109 $persistent = $this->in('Persistent Connection?', array('y', 'n'), 'n');
00110 }
00111
00112 if (low($persistent) == 'n') {
00113 $persistent = 'false';
00114 } else {
00115 $persistent = 'true';
00116 }
00117 $host = '';
00118
00119 while ($host == '') {
00120 $host = $this->in('Database Host:', null, 'localhost');
00121 }
00122 $port = '';
00123
00124 while ($port == '') {
00125 $port = $this->in('Port?', null, 'n');
00126 }
00127
00128 if (low($port) == 'n') {
00129 $port = null;
00130 }
00131 $login = '';
00132
00133 while ($login == '') {
00134 $login = $this->in('User:', null, 'root');
00135 }
00136 $password = '';
00137 $blankPassword = false;
00138
00139 while ($password == '' && $blankPassword == false) {
00140 $password = $this->in('Password:');
00141
00142 if ($password == '') {
00143 $blank = $this->in('The password you supplied was empty. Use an empty password?', array('y', 'n'), 'n');
00144 if ($blank == 'y')
00145 {
00146 $blankPassword = true;
00147 }
00148 }
00149 }
00150 $database = '';
00151
00152 while ($database == '') {
00153 $database = $this->in('Database Name:', null, 'cake');
00154 }
00155 $prefix = '';
00156
00157 while ($prefix == '') {
00158 $prefix = $this->in('Table Prefix?', null, 'n');
00159 }
00160
00161 if (low($prefix) == 'n') {
00162 $prefix = null;
00163 }
00164 $encoding = '';
00165
00166 while ($encoding == '') {
00167 $encoding = $this->in('Table encoding?', null, 'n');
00168 }
00169
00170 if (low($encoding) == 'n') {
00171 $encoding = null;
00172 }
00173 $schema = '';
00174
00175 if ($driver == 'postgres') {
00176 while ($schema == '') {
00177 $schema = $this->in('Table schema?', null, 'n');
00178 }
00179 }
00180
00181 if (low($schema) == 'n') {
00182 $schema = null;
00183 }
00184
00185 $config = compact('name', 'driver', 'persistent', 'host', 'login', 'password', 'database', 'prefix', 'encoding', 'port', 'schema');
00186
00187 while ($this->__verify($config) == false) {
00188 $this->__interactive();
00189 }
00190 $dbConfigs[] = $config;
00191 $doneYet = $this->in('Do you wish to add another database configuration?', null, 'n');
00192
00193 if (low($doneYet == 'n')) {
00194 $done = true;
00195 }
00196 }
00197
00198 $this->bake($dbConfigs);
00199 config('database');
00200 return true;
00201 }
00202
00203
00204
00205
00206
00207
00208 function __verify($config) {
00209 $config = array_merge($this->__defaultConfig, $config);
00210 extract($config);
00211 $this->out('');
00212 $this->hr();
00213 $this->out('The following database configuration will be created:');
00214 $this->hr();
00215 $this->out("Name: $name");
00216 $this->out("Driver: $driver");
00217 $this->out("Persistent: $persistent");
00218 $this->out("Host: $host");
00219
00220 if ($port) {
00221 $this->out("Port: $port");
00222 }
00223
00224 $this->out("User: $login");
00225 $this->out("Pass: " . str_repeat('*', strlen($password)));
00226 $this->out("Database: $database");
00227
00228 if ($prefix) {
00229 $this->out("Table prefix: $prefix");
00230 }
00231
00232 if ($schema) {
00233 $this->out("Schema: $schema");
00234 }
00235
00236 if ($encoding) {
00237 $this->out("Encoding: $encoding");
00238 }
00239
00240 $this->hr();
00241 $looksGood = $this->in('Look okay?', array('y', 'n'), 'y');
00242
00243 if (strtolower($looksGood) == 'y' || strtolower($looksGood) == 'yes') {
00244 return $config;
00245 }
00246 return false;
00247 }
00248
00249
00250
00251
00252
00253
00254
00255 function bake($configs) {
00256 if (!is_dir($this->path)) {
00257 $this->err($this->path . ' not found');
00258 return false;
00259 }
00260
00261 $filename = $this->path . 'database.php';
00262 $oldConfigs = array();
00263
00264 if (file_exists($filename)) {
00265 $db = new DATABASE_CONFIG;
00266 $temp = get_class_vars(get_class($db));
00267
00268 foreach ($temp as $configName => $info) {
00269 $info = array_merge($this->__defaultConfig, $info);
00270
00271 if (!isset($info['schema'])) {
00272 $info['schema'] = null;
00273 }
00274 if (!isset($info['encoding'])) {
00275 $info['encoding'] = null;
00276 }
00277 if (!isset($info['port'])) {
00278 $info['port'] = null;
00279 }
00280
00281 if ($info['persistent'] === false) {
00282 $info['persistent'] = 'false';
00283 } else {
00284 $info['persistent'] = ($info['persistent'] == true) ? 'true' : 'false';
00285 }
00286
00287 $oldConfigs[] = array(
00288 'name' => $configName,
00289 'driver' => $info['driver'],
00290 'persistent' => $info['persistent'],
00291 'host' => $info['host'],
00292 'port' => $info['port'],
00293 'login' => $info['login'],
00294 'password' => $info['password'],
00295 'database' => $info['database'],
00296 'prefix' => $info['prefix'],
00297 'schema' => $info['schema'],
00298 'encoding' => $info['encoding']
00299 );
00300 }
00301 }
00302
00303 foreach ($oldConfigs as $key => $oldConfig) {
00304 foreach ($configs as $key1 => $config) {
00305 if ($oldConfig['name'] == $config['name']) {
00306 unset($oldConfigs[$key]);
00307 }
00308 }
00309 }
00310
00311 $configs = array_merge($oldConfigs, $configs);
00312 $out = "<?php\n";
00313 $out .= "class DATABASE_CONFIG {\n\n";
00314
00315 foreach ($configs as $config) {
00316 $config = array_merge($this->__defaultConfig, $config);
00317 extract($config);
00318
00319 $out .= "\tvar \${$name} = array(\n";
00320 $out .= "\t\t'driver' => '{$driver}',\n";
00321 $out .= "\t\t'persistent' => {$persistent},\n";
00322 $out .= "\t\t'host' => '{$host}',\n";
00323
00324 if ($port) {
00325 $out .= "\t\t'port' => {$port},\n";
00326 }
00327
00328 $out .= "\t\t'login' => '{$login}',\n";
00329 $out .= "\t\t'password' => '{$password}',\n";
00330 $out .= "\t\t'database' => '{$database}',\n";
00331
00332 if ($schema) {
00333 $out .= "\t\t'schema' => '{$schema}',\n";
00334 }
00335
00336 if ($prefix) {
00337 $out .= "\t\t'prefix' => '{$prefix}',\n";
00338 }
00339
00340 if ($encoding) {
00341 $out .= "\t\t'encoding' => '{$encoding}'\n";
00342 }
00343
00344 $out .= "\t);\n";
00345 }
00346
00347 $out .= "}\n";
00348 $out .= "?>";
00349 $filename = $this->path.'database.php';
00350 return $this->createFile($filename, $out);
00351 }
00352 }
00353 ?>