2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* The DbConfig Task handles creating and updating the database.php
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2011-05-31 02:46:14 +00:00
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2011-05-31 02:46:14 +00:00
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.console.libs.tasks
|
|
|
|
* @since CakePHP(tm) v 1.2
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Task class for creating and updating the database configuration file.
|
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.console.libs.tasks
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class DbConfigTask extends Shell {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* path to CONFIG directory
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $path = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Default configuration settings to use
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $__defaultConfig = array(
|
|
|
|
'name' => 'default', 'driver'=> 'mysql', 'persistent'=> 'false', 'host'=> 'localhost',
|
|
|
|
'login'=> 'root', 'password'=> 'password', 'database'=> 'project_name',
|
|
|
|
'schema'=> null, 'prefix'=> null, 'encoding' => null, 'port' => null
|
|
|
|
);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-21 03:15:31 +00:00
|
|
|
/**
|
|
|
|
* String name of the database config class name.
|
|
|
|
* Used for testing.
|
|
|
|
*
|
|
|
|
* @var string
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-05-21 03:15:31 +00:00
|
|
|
var $databaseClassName = 'DATABASE_CONFIG';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* initialization callback
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function initialize() {
|
|
|
|
$this->path = $this->params['working'] . DS . 'config' . DS;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Execution method always used for tasks
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function execute() {
|
|
|
|
if (empty($this->args)) {
|
|
|
|
$this->__interactive();
|
2008-06-04 19:04:58 +00:00
|
|
|
$this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Interactive interface
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function __interactive() {
|
|
|
|
$this->hr();
|
|
|
|
$this->out('Database Configuration:');
|
|
|
|
$this->hr();
|
|
|
|
$done = false;
|
|
|
|
$dbConfigs = array();
|
|
|
|
|
|
|
|
while ($done == false) {
|
|
|
|
$name = '';
|
|
|
|
|
|
|
|
while ($name == '') {
|
|
|
|
$name = $this->in("Name:", null, 'default');
|
|
|
|
if (preg_match('/[^a-z0-9_]/i', $name)) {
|
|
|
|
$name = '';
|
|
|
|
$this->out('The name may only contain unaccented latin characters, numbers or underscores');
|
2009-05-21 03:15:31 +00:00
|
|
|
} else if (preg_match('/^[^a-z_]/i', $name)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$name = '';
|
|
|
|
$this->out('The name must start with an unaccented latin character or an underscore');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-01 11:01:26 +00:00
|
|
|
$driver = $this->in('Driver:', array('mssql', 'mysql', 'mysqli', 'oracle', 'postgres', 'sqlite'), 'mysql');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-05-21 03:21:36 +00:00
|
|
|
$persistent = $this->in('Persistent Connection?', array('y', 'n'), 'n');
|
2009-08-28 02:46:00 +00:00
|
|
|
if (strtolower($persistent) == 'n') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$persistent = 'false';
|
|
|
|
} else {
|
|
|
|
$persistent = 'true';
|
|
|
|
}
|
|
|
|
|
2009-05-21 03:21:36 +00:00
|
|
|
$host = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
while ($host == '') {
|
|
|
|
$host = $this->in('Database Host:', null, 'localhost');
|
|
|
|
}
|
|
|
|
|
2009-05-21 03:21:36 +00:00
|
|
|
$port = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
while ($port == '') {
|
|
|
|
$port = $this->in('Port?', null, 'n');
|
|
|
|
}
|
|
|
|
|
2009-08-28 02:46:00 +00:00
|
|
|
if (strtolower($port) == 'n') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$port = null;
|
|
|
|
}
|
|
|
|
|
2009-05-21 03:21:36 +00:00
|
|
|
$login = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
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');
|
2009-05-21 03:21:36 +00:00
|
|
|
if ($blank == 'y') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$blankPassword = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-21 03:21:36 +00:00
|
|
|
$database = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
while ($database == '') {
|
|
|
|
$database = $this->in('Database Name:', null, 'cake');
|
|
|
|
}
|
|
|
|
|
2009-05-21 03:21:36 +00:00
|
|
|
$prefix = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
while ($prefix == '') {
|
|
|
|
$prefix = $this->in('Table Prefix?', null, 'n');
|
|
|
|
}
|
2009-08-28 02:46:00 +00:00
|
|
|
if (strtolower($prefix) == 'n') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$prefix = null;
|
|
|
|
}
|
|
|
|
|
2009-05-21 03:21:36 +00:00
|
|
|
$encoding = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
while ($encoding == '') {
|
|
|
|
$encoding = $this->in('Table encoding?', null, 'n');
|
|
|
|
}
|
2009-08-28 02:46:00 +00:00
|
|
|
if (strtolower($encoding) == 'n') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$encoding = null;
|
|
|
|
}
|
|
|
|
|
2009-05-21 03:21:36 +00:00
|
|
|
$schema = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($driver == 'postgres') {
|
|
|
|
while ($schema == '') {
|
|
|
|
$schema = $this->in('Table schema?', null, 'n');
|
|
|
|
}
|
|
|
|
}
|
2009-08-28 02:46:00 +00:00
|
|
|
if (strtolower($schema) == 'n') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$schema = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$config = compact('name', 'driver', 'persistent', 'host', 'login', 'password', 'database', 'prefix', 'encoding', 'port', 'schema');
|
|
|
|
|
|
|
|
while ($this->__verify($config) == false) {
|
|
|
|
$this->__interactive();
|
|
|
|
}
|
|
|
|
$dbConfigs[] = $config;
|
|
|
|
$doneYet = $this->in('Do you wish to add another database configuration?', null, 'n');
|
|
|
|
|
2009-08-28 02:46:00 +00:00
|
|
|
if (strtolower($doneYet == 'n')) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->bake($dbConfigs);
|
|
|
|
config('database');
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Output verification message and bake if it looks good
|
|
|
|
*
|
|
|
|
* @return boolean True if user says it looks good, false otherwise
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function __verify($config) {
|
|
|
|
$config = array_merge($this->__defaultConfig, $config);
|
|
|
|
extract($config);
|
2009-09-26 21:08:37 +00:00
|
|
|
$this->out();
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->hr();
|
|
|
|
$this->out('The following database configuration will be created:');
|
|
|
|
$this->hr();
|
|
|
|
$this->out("Name: $name");
|
|
|
|
$this->out("Driver: $driver");
|
|
|
|
$this->out("Persistent: $persistent");
|
|
|
|
$this->out("Host: $host");
|
|
|
|
|
|
|
|
if ($port) {
|
|
|
|
$this->out("Port: $port");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->out("User: $login");
|
|
|
|
$this->out("Pass: " . str_repeat('*', strlen($password)));
|
|
|
|
$this->out("Database: $database");
|
|
|
|
|
|
|
|
if ($prefix) {
|
|
|
|
$this->out("Table prefix: $prefix");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($schema) {
|
|
|
|
$this->out("Schema: $schema");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($encoding) {
|
|
|
|
$this->out("Encoding: $encoding");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->hr();
|
|
|
|
$looksGood = $this->in('Look okay?', array('y', 'n'), 'y');
|
|
|
|
|
2009-05-21 03:15:31 +00:00
|
|
|
if (strtolower($looksGood) == 'y') {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Assembles and writes database.php
|
|
|
|
*
|
|
|
|
* @param array $configs Configuration settings to use
|
|
|
|
* @return boolean Success
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function bake($configs) {
|
|
|
|
if (!is_dir($this->path)) {
|
|
|
|
$this->err($this->path . ' not found');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$filename = $this->path . 'database.php';
|
|
|
|
$oldConfigs = array();
|
|
|
|
|
|
|
|
if (file_exists($filename)) {
|
2009-11-09 17:30:09 +00:00
|
|
|
config('database');
|
2009-05-21 03:15:31 +00:00
|
|
|
$db = new $this->databaseClassName;
|
2008-05-30 11:40:08 +00:00
|
|
|
$temp = get_class_vars(get_class($db));
|
|
|
|
|
|
|
|
foreach ($temp as $configName => $info) {
|
|
|
|
$info = array_merge($this->__defaultConfig, $info);
|
|
|
|
|
|
|
|
if (!isset($info['schema'])) {
|
|
|
|
$info['schema'] = null;
|
|
|
|
}
|
|
|
|
if (!isset($info['encoding'])) {
|
|
|
|
$info['encoding'] = null;
|
|
|
|
}
|
|
|
|
if (!isset($info['port'])) {
|
|
|
|
$info['port'] = null;
|
|
|
|
}
|
|
|
|
|
2008-10-23 00:10:44 +00:00
|
|
|
if ($info['persistent'] === false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$info['persistent'] = 'false';
|
|
|
|
} else {
|
2009-05-09 14:24:44 +00:00
|
|
|
$info['persistent'] = ($info['persistent'] == true) ? 'true' : 'false';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$oldConfigs[] = array(
|
|
|
|
'name' => $configName,
|
|
|
|
'driver' => $info['driver'],
|
|
|
|
'persistent' => $info['persistent'],
|
|
|
|
'host' => $info['host'],
|
|
|
|
'port' => $info['port'],
|
|
|
|
'login' => $info['login'],
|
|
|
|
'password' => $info['password'],
|
|
|
|
'database' => $info['database'],
|
|
|
|
'prefix' => $info['prefix'],
|
|
|
|
'schema' => $info['schema'],
|
|
|
|
'encoding' => $info['encoding']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($oldConfigs as $key => $oldConfig) {
|
|
|
|
foreach ($configs as $key1 => $config) {
|
|
|
|
if ($oldConfig['name'] == $config['name']) {
|
|
|
|
unset($oldConfigs[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$configs = array_merge($oldConfigs, $configs);
|
|
|
|
$out = "<?php\n";
|
|
|
|
$out .= "class DATABASE_CONFIG {\n\n";
|
|
|
|
|
|
|
|
foreach ($configs as $config) {
|
|
|
|
$config = array_merge($this->__defaultConfig, $config);
|
|
|
|
extract($config);
|
|
|
|
|
|
|
|
$out .= "\tvar \${$name} = array(\n";
|
|
|
|
$out .= "\t\t'driver' => '{$driver}',\n";
|
|
|
|
$out .= "\t\t'persistent' => {$persistent},\n";
|
|
|
|
$out .= "\t\t'host' => '{$host}',\n";
|
|
|
|
|
|
|
|
if ($port) {
|
|
|
|
$out .= "\t\t'port' => {$port},\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$out .= "\t\t'login' => '{$login}',\n";
|
|
|
|
$out .= "\t\t'password' => '{$password}',\n";
|
|
|
|
$out .= "\t\t'database' => '{$database}',\n";
|
|
|
|
|
|
|
|
if ($schema) {
|
|
|
|
$out .= "\t\t'schema' => '{$schema}',\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($prefix) {
|
|
|
|
$out .= "\t\t'prefix' => '{$prefix}',\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($encoding) {
|
|
|
|
$out .= "\t\t'encoding' => '{$encoding}'\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$out .= "\t);\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$out .= "}\n";
|
|
|
|
$out .= "?>";
|
2009-05-21 03:15:31 +00:00
|
|
|
$filename = $this->path . 'database.php';
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->createFile($filename, $out);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-04-29 00:44:23 +00:00
|
|
|
/**
|
|
|
|
* Get a user specified Connection name
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-04-29 00:44:23 +00:00
|
|
|
function getConfig() {
|
2009-11-04 18:21:05 +00:00
|
|
|
App::import('Model', 'ConnectionManager', false);
|
|
|
|
|
2009-04-29 00:44:23 +00:00
|
|
|
$useDbConfig = 'default';
|
2009-05-21 03:15:31 +00:00
|
|
|
$configs = get_class_vars($this->databaseClassName);
|
2009-04-29 00:44:23 +00:00
|
|
|
if (!is_array($configs)) {
|
|
|
|
return $this->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
$connections = array_keys($configs);
|
|
|
|
if (count($connections) > 1) {
|
|
|
|
$useDbConfig = $this->in(__('Use Database Config', true) .':', $connections, 'default');
|
|
|
|
}
|
|
|
|
return $useDbConfig;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|