cakephp2-php8/cake/console/libs/tasks/db_config.php

168 lines
4.7 KiB
PHP
Raw Normal View History

<?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
*/
if(!class_exists('File')) {
uses('file');
}
/**
* Task class for creating and updating the database configuration file.
*
* @package cake
* @subpackage cake.cake.console.libs.tasks
*/
class DbConfigTask extends Shell {
function execute() {
if(empty($this->args)) {
$this->__interactive();
}
}
function __interactive() {
$this->hr();
$this->out('Database Configuration:');
$this->hr();
$driver = '';
while ($driver == '') {
$driver = $this->in('What database driver would you like to use?', array('mysql','mysqli','mssql','sqlite','postgres', 'odbc', 'oracle', 'db2'), 'mysql');
}
$persistent = '';
while ($persistent == '') {
$persistent = $this->in('Use persistent connection', array('y', 'n'), 'n');
}
if(low($persistent) == 'n') {
$persistent = 'false';
} else {
$persistent = 'true';
}
$host = '';
while ($host == '') {
$host = $this->in('What is the hostname for the database server?', null, 'localhost');
}
$login = '';
while ($login == '') {
$login = $this->in('What is the database username?', null, 'root');
}
$password = '';
$blankPassword = false;
while ($password == '' && $blankPassword == false) {
$password = $this->in('What is the database 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;
}
}
}
$database = '';
while ($database == '') {
$database = $this->in('What is the name of the database you will be using?', null, 'cake');
}
$prefix = '';
while ($prefix == '') {
$prefix = $this->in('Enter a table prefix?', null, 'n');
}
if(low($prefix) == 'n') {
$prefix = null;
}
$config = compact('driver', 'persistent', 'host', 'login', 'password', 'database', 'prefix');
while($this->__verify($config) == false) {
$this->__interactive();
}
}
function __verify($config) {
$defaults = array('driver'=> 'mysql', 'persistent'=> 'false', 'host'=> 'localhost',
'login'=> 'root', 'password'=> 'password', 'database'=> 'project_name',
'schema'=> null,'prefix'=> null);
$config = am($defaults, $config);
extract($config);
$this->out('');
$this->hr();
$this->out('The following database configuration will be created:');
$this->hr();
$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");
$this->hr();
$looksGood = $this->in('Look okay?', array('y', 'n'), 'y');
if (low($looksGood) == 'y' || low($looksGood) == 'yes') {
return $this->__bake($config);
} else {
return false;
}
}
function __bake($config) {
$defaults = array('driver'=> 'mysql', 'persistent'=> 'false', 'host'=> 'localhost',
'login'=> 'root', 'password'=> 'password', 'database'=> 'project_name',
'schema'=> null,'prefix'=> null);
$config = am($defaults, $config);
extract($config);
if(is_dir(CONFIGS)) {
$out = "<?php\n";
$out .= "class DATABASE_CONFIG {\n\n";
$out .= "\tvar \$default = array(\n";
$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";
if($schema) {
$out .= "\t\t'schema' => '{$schema}', \n";
}
$out .= "\t\t'prefix' => '{$prefix}' \n";
$out .= "\t);\n";
$out .= "}\n";
$out .= "?>";
$filename = CONFIGS.'database.php';
return $this->createFile($filename, $out);
} else {
$this->err(CONFIGS .' not found');
}
return false;
}
}
?>