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/>
|
2008-01-01 22:18:17 +00:00
|
|
|
* Copyright 2005-2008, Cake Software Foundation, Inc.
|
2007-05-14 12:24:40 +00:00
|
|
|
* 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
|
2008-01-01 22:18:17 +00:00
|
|
|
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc.
|
2007-05-14 12:24:40 +00:00
|
|
|
* @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 {
|
2008-05-05 15:42:02 +00:00
|
|
|
/**
|
|
|
|
* path to CONFIG directory
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $path = null;
|
2007-10-22 05:52:20 +00:00
|
|
|
/**
|
|
|
|
* Default configuration settings to use
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access private
|
|
|
|
*/
|
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',
|
2007-10-20 10:14:30 +00:00
|
|
|
'schema'=> null, 'prefix'=> null, 'encoding' => null, 'port' => null);
|
2008-05-05 15:42:02 +00:00
|
|
|
/**
|
|
|
|
* initialization callback
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function initialize() {
|
|
|
|
$this->path = $this->params['working'] . DS . 'config' . DS;
|
|
|
|
}
|
2007-06-03 23:56:33 +00:00
|
|
|
/**
|
|
|
|
* Execution method always used for tasks
|
|
|
|
*
|
2007-10-22 05:52:20 +00:00
|
|
|
* @access public
|
2007-06-03 23:56:33 +00:00
|
|
|
*/
|
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-10-19 22:05:20 +00:00
|
|
|
exit();
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-03 23:56:33 +00:00
|
|
|
/**
|
|
|
|
* Interactive interface
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*/
|
2007-05-14 12:24:40 +00:00
|
|
|
function __interactive() {
|
2008-05-05 15:42:02 +00:00
|
|
|
$this->hr();
|
2007-05-14 12:24:40 +00:00
|
|
|
$this->out('Database Configuration:');
|
2008-05-05 15:42:02 +00:00
|
|
|
$this->hr();
|
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');
|
2008-03-15 05:45:43 +00:00
|
|
|
if (preg_match('/[^a-z0-9_]/i', $name)) {
|
|
|
|
$name = '';
|
|
|
|
$this->out('The name may only contain unaccented latin characters, numbers or underscores');
|
|
|
|
}
|
|
|
|
else if (preg_match('/^[^a-z_]/i', $name)) {
|
|
|
|
$name = '';
|
|
|
|
$this->out('The name must start with an unaccented latin character or an underscore');
|
|
|
|
}
|
2007-09-15 20:23:30 +00:00
|
|
|
}
|
|
|
|
$driver = '';
|
2007-05-14 12:24:40 +00:00
|
|
|
|
2007-09-15 20:23:30 +00:00
|
|
|
while ($driver == '') {
|
2007-11-09 15:50:33 +00:00
|
|
|
$driver = $this->in('Driver:', array('db2', 'firebird', 'mssql', 'mysql', 'mysqli', 'odbc', 'oracle', 'postgres', 'sqlite', 'sybase'), 'mysql');
|
2007-09-15 20:23:30 +00:00
|
|
|
}
|
|
|
|
$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');
|
|
|
|
}
|
2007-10-20 10:14:30 +00:00
|
|
|
$port = '';
|
|
|
|
|
|
|
|
while ($port == '') {
|
|
|
|
$port = $this->in('Port?', null, 'n');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (low($port) == 'n') {
|
|
|
|
$port = null;
|
|
|
|
}
|
2007-09-15 20:23:30 +00:00
|
|
|
$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;
|
|
|
|
}
|
2007-10-20 10:14:30 +00:00
|
|
|
$encoding = '';
|
|
|
|
|
|
|
|
while ($encoding == '') {
|
|
|
|
$encoding = $this->in('Table encoding?', null, 'n');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (low($encoding) == 'n') {
|
|
|
|
$encoding = null;
|
|
|
|
}
|
|
|
|
$schema = '';
|
|
|
|
|
|
|
|
while ($schema == '') {
|
|
|
|
$schema = $this->in('Table schema?', null, 'n');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (low($schema) == 'n') {
|
|
|
|
$schema = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$config = compact('name', 'driver', 'persistent', 'host', 'login', 'password', 'database', 'prefix', 'encoding', 'port', 'schema');
|
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;
|
|
|
|
}
|
|
|
|
}
|
2007-10-20 10:14:30 +00:00
|
|
|
|
2007-09-15 20:23:30 +00:00
|
|
|
$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
|
|
|
/**
|
2007-10-22 05:52:20 +00:00
|
|
|
* Output verification message and bake if it looks good
|
2007-06-03 23:56:33 +00:00
|
|
|
*
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean True if user says it looks good, false otherwise
|
2007-06-03 23:56:33 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2007-05-14 12:24:40 +00:00
|
|
|
function __verify($config) {
|
2008-03-12 21:19:31 +00:00
|
|
|
$config = array_merge($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-10-20 10:14:30 +00:00
|
|
|
$this->out("Name: $name");
|
|
|
|
$this->out("Driver: $driver");
|
|
|
|
$this->out("Persistent: $persistent");
|
|
|
|
$this->out("Host: $host");
|
|
|
|
$this->out("Port: $port");
|
|
|
|
$this->out("User: $login");
|
|
|
|
$this->out("Pass: " . str_repeat('*', strlen($password)));
|
|
|
|
$this->out("Database: $database");
|
|
|
|
$this->out("Table prefix: $prefix");
|
|
|
|
$this->out("Schema: $schema");
|
|
|
|
$this->out("Encoding: $encoding");
|
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
|
|
|
|
*
|
2007-10-22 05:52:20 +00:00
|
|
|
* @param array $configs Configuration settings to use
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean Success
|
2007-06-03 23:56:33 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2007-09-15 20:23:30 +00:00
|
|
|
function bake($configs) {
|
2008-05-05 15:42:02 +00:00
|
|
|
if (!is_dir($this->path)) {
|
|
|
|
$this->err($this->path . ' not found');
|
2007-09-15 20:23:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-10-19 22:05:20 +00:00
|
|
|
|
2008-05-05 15:42:02 +00:00
|
|
|
$filename = $this->path . 'database.php';
|
2007-10-19 22:05:20 +00:00
|
|
|
$oldConfigs = array();
|
|
|
|
|
|
|
|
if (file_exists($filename)) {
|
|
|
|
$db = new DATABASE_CONFIG;
|
|
|
|
$temp = get_class_vars(get_class($db));
|
|
|
|
|
2007-10-20 10:14:30 +00:00
|
|
|
foreach ($temp as $configName => $info) {
|
2007-10-19 22:05:20 +00:00
|
|
|
if (!isset($info['schema'])) {
|
|
|
|
$info['schema'] = null;
|
|
|
|
}
|
2007-10-20 10:14:30 +00:00
|
|
|
if (!isset($info['encoding'])) {
|
|
|
|
$info['encoding'] = null;
|
|
|
|
}
|
|
|
|
if (!isset($info['port'])) {
|
|
|
|
$info['port'] = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($info['persistent'] === false) {
|
|
|
|
$info['persistent'] = 'false';
|
|
|
|
} else {
|
|
|
|
$info['persistent'] = 'false';
|
|
|
|
}
|
|
|
|
|
2007-10-19 22:05:20 +00:00
|
|
|
$oldConfigs[] = array('name' => $configName,
|
|
|
|
'driver' => $info['driver'],
|
|
|
|
'persistent' => $info['persistent'],
|
|
|
|
'host' => $info['host'],
|
2007-10-20 10:14:30 +00:00
|
|
|
'port' => $info['port'],
|
2007-10-19 22:05:20 +00:00
|
|
|
'login' => $info['login'],
|
|
|
|
'password' => $info['password'],
|
|
|
|
'database' => $info['database'],
|
|
|
|
'prefix' => $info['prefix'],
|
2007-10-20 10:14:30 +00:00
|
|
|
'schema' => $info['schema'],
|
|
|
|
'encoding' => $info['encoding']);
|
2007-10-19 22:05:20 +00:00
|
|
|
}
|
|
|
|
}
|
2007-10-20 10:14:30 +00:00
|
|
|
|
|
|
|
foreach ($oldConfigs as $key => $oldConfig) {
|
|
|
|
foreach ($configs as $key1 => $config) {
|
2007-10-19 22:05:20 +00:00
|
|
|
if ($oldConfig['name'] == $config['name']) {
|
2007-10-20 10:14:30 +00:00
|
|
|
unset($oldConfigs[$key]);
|
2007-10-19 22:05:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-10-20 10:14:30 +00:00
|
|
|
|
2008-03-12 21:19:31 +00:00
|
|
|
$configs = array_merge($oldConfigs, $configs);
|
2007-09-15 20:23:30 +00:00
|
|
|
$out = "<?php\n";
|
|
|
|
$out .= "class DATABASE_CONFIG {\n\n";
|
2007-10-20 10:14:30 +00:00
|
|
|
|
2007-09-15 20:23:30 +00:00
|
|
|
foreach ($configs as $config) {
|
2008-03-12 21:19:31 +00:00
|
|
|
$config = array_merge($this->__defaultConfig, $config);
|
2007-10-20 10:14:30 +00:00
|
|
|
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";
|
2007-10-20 10:14:30 +00:00
|
|
|
$out .= "\t\t'persistent' => {$persistent},\n";
|
2007-05-14 12:24:40 +00:00
|
|
|
$out .= "\t\t'host' => '{$host}',\n";
|
2008-05-05 15:42:02 +00:00
|
|
|
$out .= "\t\t'port' => {$port},\n";
|
2007-05-14 12:24:40 +00:00
|
|
|
$out .= "\t\t'login' => '{$login}',\n";
|
|
|
|
$out .= "\t\t'password' => '{$password}',\n";
|
2007-10-20 10:14:30 +00:00
|
|
|
$out .= "\t\t'database' => '{$database}',\n";
|
|
|
|
$out .= "\t\t'schema' => '{$schema}',\n";
|
|
|
|
$out .= "\t\t'prefix' => '{$prefix}',\n";
|
|
|
|
$out .= "\t\t'encoding' => '{$encoding}'\n";
|
2007-05-14 12:24:40 +00:00
|
|
|
$out .= "\t);\n";
|
|
|
|
}
|
2007-10-20 10:14:30 +00:00
|
|
|
|
2007-09-15 20:23:30 +00:00
|
|
|
$out .= "}\n";
|
|
|
|
$out .= "?>";
|
2008-05-05 15:42:02 +00:00
|
|
|
$filename = $this->path.'database.php';
|
2007-09-15 20:23:30 +00:00
|
|
|
return $this->createFile($filename, $out);
|
2007-05-14 12:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
2007-10-20 10:14:30 +00:00
|
|
|
?>
|