2007-05-14 12:24:40 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
/**
|
|
|
|
* The DbConfig Task handles creating and updating the database.php
|
|
|
|
*
|
|
|
|
* Long description for file
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
|
|
|
|
* Copyright 2005-2007, Cake Software Foundation, Inc.
|
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
|
|
|
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
|
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.console.libs.tasks
|
|
|
|
* @since CakePHP(tm) v 1.2
|
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
|
*/
|
2007-06-20 06:15:35 +00:00
|
|
|
if (!class_exists('File')) {
|
2007-05-14 12:24:40 +00:00
|
|
|
uses('file');
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Task class for creating and updating the database configuration file.
|
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.console.libs.tasks
|
|
|
|
*/
|
|
|
|
class DbConfigTask extends Shell {
|
2007-10-15 07:48:19 +00:00
|
|
|
|
|
|
|
var $__defaultConfig = array('name' => 'default', 'driver'=> 'mysql', 'persistent'=> 'false', 'host'=> 'localhost',
|
|
|
|
'login'=> 'root', 'password'=> 'password', 'database'=> 'project_name',
|
|
|
|
'schema'=> null, 'prefix'=> null);
|
|
|
|
|
2007-05-14 12:24:40 +00:00
|
|
|
|
2007-06-03 23:56:33 +00:00
|
|
|
/**
|
|
|
|
* Execution method always used for tasks
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-05-14 12:24:40 +00:00
|
|
|
function execute() {
|
2007-06-20 06:15:35 +00:00
|
|
|
if (empty($this->args)) {
|
2007-05-14 12:24:40 +00:00
|
|
|
$this->__interactive();
|
|
|
|
}
|
|
|
|
}
|
2007-09-15 20:23:30 +00:00
|
|
|
|
2007-06-03 23:56:33 +00:00
|
|
|
/**
|
|
|
|
* Interactive interface
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-05-14 12:24:40 +00:00
|
|
|
function __interactive() {
|
|
|
|
$this->out('Database Configuration:');
|
2007-09-15 20:23:30 +00:00
|
|
|
$done = false;
|
|
|
|
$dbConfigs = array();
|
2007-05-14 12:24:40 +00:00
|
|
|
|
2007-09-15 20:23:30 +00:00
|
|
|
while ($done == false) {
|
|
|
|
$name = '';
|
2007-05-14 12:24:40 +00:00
|
|
|
|
2007-09-15 20:23:30 +00:00
|
|
|
while ($name == '') {
|
|
|
|
$name = $this->in("Name:", null, 'default');
|
|
|
|
}
|
|
|
|
$driver = '';
|
2007-05-14 12:24:40 +00:00
|
|
|
|
2007-09-15 20:23:30 +00:00
|
|
|
while ($driver == '') {
|
|
|
|
$driver = $this->in('Driver:', array('mysql','mysqli','mssql','sqlite','postgres', 'odbc', 'oracle', 'db2'), 'mysql');
|
|
|
|
}
|
|
|
|
$persistent = '';
|
|
|
|
|
|
|
|
while ($persistent == '') {
|
|
|
|
$persistent = $this->in('Persistent Connection?', array('y', 'n'), 'n');
|
|
|
|
}
|
2007-05-14 12:24:40 +00:00
|
|
|
|
2007-09-15 20:23:30 +00:00
|
|
|
if (low($persistent) == 'n') {
|
|
|
|
$persistent = 'false';
|
|
|
|
} else {
|
|
|
|
$persistent = 'true';
|
|
|
|
}
|
|
|
|
$host = '';
|
|
|
|
|
|
|
|
while ($host == '') {
|
|
|
|
$host = $this->in('Database Host:', null, 'localhost');
|
|
|
|
}
|
|
|
|
$login = '';
|
|
|
|
|
|
|
|
while ($login == '') {
|
|
|
|
$login = $this->in('User:', null, 'root');
|
|
|
|
}
|
|
|
|
$password = '';
|
|
|
|
$blankPassword = false;
|
|
|
|
|
|
|
|
while ($password == '' && $blankPassword == false) {
|
|
|
|
$password = $this->in('Password:');
|
|
|
|
|
|
|
|
if ($password == '') {
|
|
|
|
$blank = $this->in('The password you supplied was empty. Use an empty password?', array('y', 'n'), 'n');
|
|
|
|
if ($blank == 'y')
|
|
|
|
{
|
|
|
|
$blankPassword = true;
|
|
|
|
}
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
2007-09-15 20:23:30 +00:00
|
|
|
$database = '';
|
2007-05-14 12:24:40 +00:00
|
|
|
|
2007-09-15 20:23:30 +00:00
|
|
|
while ($database == '') {
|
|
|
|
$database = $this->in('Database Name:', null, 'cake');
|
|
|
|
}
|
|
|
|
$prefix = '';
|
2007-05-14 12:24:40 +00:00
|
|
|
|
2007-09-15 20:23:30 +00:00
|
|
|
while ($prefix == '') {
|
|
|
|
$prefix = $this->in('Table Prefix?', null, 'n');
|
|
|
|
}
|
2007-06-03 23:56:33 +00:00
|
|
|
|
2007-09-15 20:23:30 +00:00
|
|
|
if (low($prefix) == 'n') {
|
|
|
|
$prefix = null;
|
|
|
|
}
|
|
|
|
$config = compact('name', 'driver', 'persistent', 'host', 'login', 'password', 'database', 'prefix');
|
2007-06-03 23:56:33 +00:00
|
|
|
|
2007-09-15 20:23:30 +00:00
|
|
|
while ($this->__verify($config) == false) {
|
|
|
|
$this->__interactive();
|
|
|
|
}
|
|
|
|
$dbConfigs[] = $config;
|
|
|
|
$doneYet = $this->in('Do you wish to add another database configuration?', null, 'n');
|
|
|
|
|
|
|
|
if (low($doneYet == 'n')) {
|
|
|
|
$done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->bake($dbConfigs);
|
2007-05-15 04:21:44 +00:00
|
|
|
config('database');
|
|
|
|
return true;
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
2007-06-03 23:56:33 +00:00
|
|
|
/**
|
|
|
|
* Output verification message
|
|
|
|
* and bake if it looks good
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @return bool
|
|
|
|
*/
|
2007-05-14 12:24:40 +00:00
|
|
|
function __verify($config) {
|
2007-10-15 07:48:19 +00:00
|
|
|
$config = am($this->__defaultConfig, $config);
|
2007-05-14 12:24:40 +00:00
|
|
|
extract($config);
|
|
|
|
$this->out('');
|
|
|
|
$this->hr();
|
|
|
|
$this->out('The following database configuration will be created:');
|
|
|
|
$this->hr();
|
2007-09-15 20:23:30 +00:00
|
|
|
$this->out("Name: $name");
|
2007-05-14 12:24:40 +00:00
|
|
|
$this->out("Driver: $driver");
|
|
|
|
$this->out("Persistent: $persistent");
|
|
|
|
$this->out("Host: $host");
|
|
|
|
$this->out("User: $login");
|
|
|
|
$this->out("Pass: " . str_repeat('*', strlen($password)));
|
|
|
|
$this->out("Database: $database");
|
|
|
|
$this->out("Table prefix: $prefix");
|
2007-09-15 20:23:30 +00:00
|
|
|
$this->out("Schema: $schema");
|
2007-05-14 12:24:40 +00:00
|
|
|
$this->hr();
|
|
|
|
$looksGood = $this->in('Look okay?', array('y', 'n'), 'y');
|
|
|
|
|
|
|
|
if (low($looksGood) == 'y' || low($looksGood) == 'yes') {
|
2007-10-15 07:48:19 +00:00
|
|
|
return $config;
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
2007-09-15 20:23:30 +00:00
|
|
|
return false;
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
2007-06-03 23:56:33 +00:00
|
|
|
/**
|
|
|
|
* Assembles and writes database.php
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return bool
|
|
|
|
*/
|
2007-09-15 20:23:30 +00:00
|
|
|
function bake($configs) {
|
|
|
|
if (!is_dir(CONFIGS)) {
|
|
|
|
$this->err(CONFIGS .' not found');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$out = "<?php\n";
|
|
|
|
$out .= "class DATABASE_CONFIG {\n\n";
|
2007-10-15 07:48:19 +00:00
|
|
|
|
2007-09-15 20:23:30 +00:00
|
|
|
foreach ($configs as $config) {
|
2007-10-15 07:48:19 +00:00
|
|
|
$config = am($this->__defaultConfig, $config);
|
|
|
|
extract($config);
|
2007-09-15 20:23:30 +00:00
|
|
|
$out .= "\tvar \${$name} = array(\n";
|
2007-05-14 12:24:40 +00:00
|
|
|
$out .= "\t\t'driver' => '{$driver}',\n";
|
|
|
|
$out .= "\t\t'persistent' => {$persistent},\n";
|
|
|
|
$out .= "\t\t'host' => '{$host}',\n";
|
|
|
|
$out .= "\t\t'login' => '{$login}',\n";
|
|
|
|
$out .= "\t\t'password' => '{$password}',\n";
|
|
|
|
$out .= "\t\t'database' => '{$database}', \n";
|
2007-09-15 20:23:30 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if ($schema) {
|
2007-05-14 12:24:40 +00:00
|
|
|
$out .= "\t\t'schema' => '{$schema}', \n";
|
|
|
|
}
|
|
|
|
$out .= "\t\t'prefix' => '{$prefix}' \n";
|
|
|
|
$out .= "\t);\n";
|
|
|
|
}
|
2007-09-15 20:23:30 +00:00
|
|
|
$out .= "}\n";
|
|
|
|
$out .= "?>";
|
|
|
|
$filename = CONFIGS.'database.php';
|
|
|
|
return $this->createFile($filename, $out);
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|