3
* The DbConfig Task handles creating and updating the database.php
7
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
10
* Licensed under The MIT License
11
* Redistributions of files must retain the above copyright notice.
13
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14
* @link http://cakephp.org CakePHP(tm) Project
16
* @subpackage cake.cake.console.libs.tasks
17
* @since CakePHP(tm) v 1.2
18
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
22
* Task class for creating and updating the database configuration file.
25
* @subpackage cake.cake.console.libs.tasks
27
class DbConfigTask extends Shell {
30
* path to CONFIG directory
38
* Default configuration settings to use
43
var $__defaultConfig = array(
44
'name' => 'default', 'driver'=> 'mysql', 'persistent'=> 'false', 'host'=> 'localhost',
45
'login'=> 'root', 'password'=> 'password', 'database'=> 'project_name',
46
'schema'=> null, 'prefix'=> null, 'encoding' => null, 'port' => null
50
* String name of the database config class name.
55
var $databaseClassName = 'DATABASE_CONFIG';
58
* initialization callback
63
function initialize() {
64
$this->path = $this->params['working'] . DS . 'config' . DS;
68
* Execution method always used for tasks
73
if (empty($this->args)) {
74
$this->__interactive();
80
* Interactive interface
84
function __interactive() {
86
$this->out('Database Configuration:');
91
while ($done == false) {
95
$name = $this->in("Name:", null, 'default');
96
if (preg_match('/[^a-z0-9_]/i', $name)) {
98
$this->out('The name may only contain unaccented latin characters, numbers or underscores');
99
} else if (preg_match('/^[^a-z_]/i', $name)) {
101
$this->out('The name must start with an unaccented latin character or an underscore');
105
$driver = $this->in('Driver:', array('db2', 'firebird', 'mssql', 'mysql', 'mysqli', 'odbc', 'oracle', 'postgres', 'sqlite', 'sybase'), 'mysql');
107
$persistent = $this->in('Persistent Connection?', array('y', 'n'), 'n');
108
if (strtolower($persistent) == 'n') {
109
$persistent = 'false';
111
$persistent = 'true';
115
while ($host == '') {
116
$host = $this->in('Database Host:', null, 'localhost');
120
while ($port == '') {
121
$port = $this->in('Port?', null, 'n');
124
if (strtolower($port) == 'n') {
129
while ($login == '') {
130
$login = $this->in('User:', null, 'root');
133
$blankPassword = false;
135
while ($password == '' && $blankPassword == false) {
136
$password = $this->in('Password:');
138
if ($password == '') {
139
$blank = $this->in('The password you supplied was empty. Use an empty password?', array('y', 'n'), 'n');
141
$blankPassword = true;
147
while ($database == '') {
148
$database = $this->in('Database Name:', null, 'cake');
152
while ($prefix == '') {
153
$prefix = $this->in('Table Prefix?', null, 'n');
155
if (strtolower($prefix) == 'n') {
160
while ($encoding == '') {
161
$encoding = $this->in('Table encoding?', null, 'n');
163
if (strtolower($encoding) == 'n') {
168
if ($driver == 'postgres') {
169
while ($schema == '') {
170
$schema = $this->in('Table schema?', null, 'n');
173
if (strtolower($schema) == 'n') {
177
$config = compact('name', 'driver', 'persistent', 'host', 'login', 'password', 'database', 'prefix', 'encoding', 'port', 'schema');
179
while ($this->__verify($config) == false) {
180
$this->__interactive();
182
$dbConfigs[] = $config;
183
$doneYet = $this->in('Do you wish to add another database configuration?', null, 'n');
185
if (strtolower($doneYet == 'n')) {
190
$this->bake($dbConfigs);
196
* Output verification message and bake if it looks good
198
* @return boolean True if user says it looks good, false otherwise
201
function __verify($config) {
202
$config = array_merge($this->__defaultConfig, $config);
206
$this->out('The following database configuration will be created:');
208
$this->out("Name: $name");
209
$this->out("Driver: $driver");
210
$this->out("Persistent: $persistent");
211
$this->out("Host: $host");
214
$this->out("Port: $port");
217
$this->out("User: $login");
218
$this->out("Pass: " . str_repeat('*', strlen($password)));
219
$this->out("Database: $database");
222
$this->out("Table prefix: $prefix");
226
$this->out("Schema: $schema");
230
$this->out("Encoding: $encoding");
234
$looksGood = $this->in('Look okay?', array('y', 'n'), 'y');
236
if (strtolower($looksGood) == 'y') {
243
* Assembles and writes database.php
245
* @param array $configs Configuration settings to use
246
* @return boolean Success
249
function bake($configs) {
250
if (!is_dir($this->path)) {
251
$this->err($this->path . ' not found');
255
$filename = $this->path . 'database.php';
256
$oldConfigs = array();
258
if (file_exists($filename)) {
260
$db = new $this->databaseClassName;
261
$temp = get_class_vars(get_class($db));
263
foreach ($temp as $configName => $info) {
264
$info = array_merge($this->__defaultConfig, $info);
266
if (!isset($info['schema'])) {
267
$info['schema'] = null;
269
if (!isset($info['encoding'])) {
270
$info['encoding'] = null;
272
if (!isset($info['port'])) {
273
$info['port'] = null;
276
if ($info['persistent'] === false) {
277
$info['persistent'] = 'false';
279
$info['persistent'] = ($info['persistent'] == true) ? 'true' : 'false';
282
$oldConfigs[] = array(
283
'name' => $configName,
284
'driver' => $info['driver'],
285
'persistent' => $info['persistent'],
286
'host' => $info['host'],
287
'port' => $info['port'],
288
'login' => $info['login'],
289
'password' => $info['password'],
290
'database' => $info['database'],
291
'prefix' => $info['prefix'],
292
'schema' => $info['schema'],
293
'encoding' => $info['encoding']
298
foreach ($oldConfigs as $key => $oldConfig) {
299
foreach ($configs as $key1 => $config) {
300
if ($oldConfig['name'] == $config['name']) {
301
unset($oldConfigs[$key]);
306
$configs = array_merge($oldConfigs, $configs);
308
$out .= "class DATABASE_CONFIG {\n\n";
310
foreach ($configs as $config) {
311
$config = array_merge($this->__defaultConfig, $config);
314
$out .= "\tvar \${$name} = array(\n";
315
$out .= "\t\t'driver' => '{$driver}',\n";
316
$out .= "\t\t'persistent' => {$persistent},\n";
317
$out .= "\t\t'host' => '{$host}',\n";
320
$out .= "\t\t'port' => {$port},\n";
323
$out .= "\t\t'login' => '{$login}',\n";
324
$out .= "\t\t'password' => '{$password}',\n";
325
$out .= "\t\t'database' => '{$database}',\n";
328
$out .= "\t\t'schema' => '{$schema}',\n";
332
$out .= "\t\t'prefix' => '{$prefix}',\n";
336
$out .= "\t\t'encoding' => '{$encoding}'\n";
344
$filename = $this->path . 'database.php';
345
return $this->createFile($filename, $out);
349
* Get a user specified Connection name
353
function getConfig() {
354
App::import('Model', 'ConnectionManager', false);
356
$useDbConfig = 'default';
357
$configs = get_class_vars($this->databaseClassName);
358
if (!is_array($configs)) {
359
return $this->execute();
362
$connections = array_keys($configs);
363
if (count($connections) > 1) {
364
$useDbConfig = $this->in(__('Use Database Config', true) .':', $connections, 'default');