db_config.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: db__config_8php-source.html 580 2008-07-01 14:45:49Z gwoo $ */
00003 /**
00004  * The DbConfig Task handles creating and updating the database.php
00005  *
00006  * Long description for file
00007  *
00008  * PHP versions 4 and 5
00009  *
00010  * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
00011  * Copyright 2005-2008, Cake Software Foundation, Inc.
00012  *                              1785 E. Sahara Avenue, Suite 490-204
00013  *                              Las Vegas, Nevada 89104
00014  *
00015  * Licensed under The MIT License
00016  * Redistributions of files must retain the above copyright notice.
00017  *
00018  * @filesource
00019  * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc.
00020  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00021  * @package         cake
00022  * @subpackage      cake.cake.console.libs.tasks
00023  * @since           CakePHP(tm) v 1.2
00024  * @version         $Revision: 580 $
00025  * @modifiedby      $LastChangedBy: gwoo $
00026  * @lastmodified    $Date: 2008-07-01 09:45:49 -0500 (Tue, 01 Jul 2008) $
00027  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00028  */
00029 if (!class_exists('File')) {
00030     uses('file');
00031 }
00032 /**
00033  * Task class for creating and updating the database configuration file.
00034  *
00035  * @package     cake
00036  * @subpackage  cake.cake.console.libs.tasks
00037  */
00038 class DbConfigTask extends Shell {
00039 /**
00040  * path to CONFIG directory
00041  *
00042  * @var string
00043  * @access public
00044  */
00045     var $path = null;
00046 /**
00047  * Default configuration settings to use
00048  *
00049  * @var array
00050  * @access private
00051  */
00052     var $__defaultConfig = array(
00053         'name' => 'default', 'driver'=> 'mysql', 'persistent'=> 'false', 'host'=> 'localhost',
00054         'login'=> 'root', 'password'=> 'password', 'database'=> 'project_name',
00055         'schema'=> null, 'prefix'=> null, 'encoding' => null, 'port' => null
00056     );
00057 /**
00058  * initialization callback
00059  *
00060  * @var string
00061  * @access public
00062  */
00063     function initialize() {
00064         $this->path = $this->params['working'] . DS . 'config' . DS;
00065     }
00066 /**
00067  * Execution method always used for tasks
00068  *
00069  * @access public
00070  */
00071     function execute() {
00072         if (empty($this->args)) {
00073             $this->__interactive();
00074             $this->_stop();
00075         }
00076     }
00077 /**
00078  * Interactive interface
00079  *
00080  * @access private
00081  */
00082     function __interactive() {
00083         $this->hr();
00084         $this->out('Database Configuration:');
00085         $this->hr();
00086         $done = false;
00087         $dbConfigs = array();
00088 
00089         while ($done == false) {
00090             $name = '';
00091 
00092             while ($name == '') {
00093                 $name = $this->in("Name:", null, 'default');
00094                 if (preg_match('/[^a-z0-9_]/i', $name)) {
00095                     $name = '';
00096                     $this->out('The name may only contain unaccented latin characters, numbers or underscores');
00097                 }
00098                 else if (preg_match('/^[^a-z_]/i', $name)) {
00099                     $name = '';
00100                     $this->out('The name must start with an unaccented latin character or an underscore');
00101                 }
00102             }
00103             $driver = '';
00104 
00105             while ($driver == '') {
00106                 $driver = $this->in('Driver:', array('db2', 'firebird', 'mssql', 'mysql', 'mysqli', 'odbc', 'oracle', 'postgres', 'sqlite', 'sybase'), 'mysql');
00107             }
00108             $persistent = '';
00109 
00110             while ($persistent == '') {
00111                 $persistent = $this->in('Persistent Connection?', array('y', 'n'), 'n');
00112             }
00113 
00114             if (low($persistent) == 'n') {
00115                 $persistent = 'false';
00116             } else {
00117                 $persistent = 'true';
00118             }
00119             $host = '';
00120 
00121             while ($host == '') {
00122                 $host = $this->in('Database Host:', null, 'localhost');
00123             }
00124             $port = '';
00125 
00126             while ($port == '') {
00127                 $port = $this->in('Port?', null, 'n');
00128             }
00129 
00130             if (low($port) == 'n') {
00131                 $port = null;
00132             }
00133             $login = '';
00134 
00135             while ($login == '') {
00136                 $login = $this->in('User:', null, 'root');
00137             }
00138             $password = '';
00139             $blankPassword = false;
00140 
00141             while ($password == '' && $blankPassword == false) {
00142                 $password = $this->in('Password:');
00143 
00144                 if ($password == '') {
00145                     $blank = $this->in('The password you supplied was empty. Use an empty password?', array('y', 'n'), 'n');
00146                     if ($blank == 'y')
00147                     {
00148                         $blankPassword = true;
00149                     }
00150                 }
00151             }
00152             $database = '';
00153 
00154             while ($database == '') {
00155                 $database = $this->in('Database Name:', null, 'cake');
00156             }
00157             $prefix = '';
00158 
00159             while ($prefix == '') {
00160                 $prefix = $this->in('Table Prefix?', null, 'n');
00161             }
00162 
00163             if (low($prefix) == 'n') {
00164                 $prefix = null;
00165             }
00166             $encoding = '';
00167 
00168             while ($encoding == '') {
00169                 $encoding = $this->in('Table encoding?', null, 'n');
00170             }
00171 
00172             if (low($encoding) == 'n') {
00173                 $encoding = null;
00174             }
00175             $schema = '';
00176 
00177             if ($driver == 'postgres') {
00178                 while ($schema == '') {
00179                     $schema = $this->in('Table schema?', null, 'n');
00180                 }
00181             }
00182 
00183             if (low($schema) == 'n') {
00184                 $schema = null;
00185             }
00186 
00187             $config = compact('name', 'driver', 'persistent', 'host', 'login', 'password', 'database', 'prefix', 'encoding', 'port', 'schema');
00188 
00189             while ($this->__verify($config) == false) {
00190                 $this->__interactive();
00191             }
00192             $dbConfigs[] = $config;
00193             $doneYet = $this->in('Do you wish to add another database configuration?', null, 'n');
00194 
00195             if (low($doneYet == 'n')) {
00196                 $done = true;
00197             }
00198         }
00199 
00200         $this->bake($dbConfigs);
00201         config('database');
00202         return true;
00203     }
00204 /**
00205  * Output verification message and bake if it looks good
00206  *
00207  * @return boolean True if user says it looks good, false otherwise
00208  * @access private
00209  */
00210     function __verify($config) {
00211         $config = array_merge($this->__defaultConfig, $config);
00212         extract($config);
00213         $this->out('');
00214         $this->hr();
00215         $this->out('The following database configuration will be created:');
00216         $this->hr();
00217         $this->out("Name:         $name");
00218         $this->out("Driver:       $driver");
00219         $this->out("Persistent:   $persistent");
00220         $this->out("Host:         $host");
00221 
00222         if ($port) {
00223             $this->out("Port:         $port");
00224         }
00225 
00226         $this->out("User:         $login");
00227         $this->out("Pass:         " . str_repeat('*', strlen($password)));
00228         $this->out("Database:     $database");
00229 
00230         if ($prefix) {
00231             $this->out("Table prefix: $prefix");
00232         }
00233 
00234         if ($schema) {
00235             $this->out("Schema:       $schema");
00236         }
00237 
00238         if ($encoding) {
00239             $this->out("Encoding:     $encoding");
00240         }
00241 
00242         $this->hr();
00243         $looksGood = $this->in('Look okay?', array('y', 'n'), 'y');
00244 
00245         if (low($looksGood) == 'y' || low($looksGood) == 'yes') {
00246             return $config;
00247         }
00248         return false;
00249     }
00250 /**
00251  * Assembles and writes database.php
00252  *
00253  * @param array $configs Configuration settings to use
00254  * @return boolean Success
00255  * @access public
00256  */
00257     function bake($configs) {
00258         if (!is_dir($this->path)) {
00259             $this->err($this->path . ' not found');
00260             return false;
00261         }
00262 
00263         $filename = $this->path . 'database.php';
00264         $oldConfigs = array();
00265 
00266         if (file_exists($filename)) {
00267             $db = new DATABASE_CONFIG;
00268             $temp = get_class_vars(get_class($db));
00269 
00270             foreach ($temp as $configName => $info) {
00271                 $info = array_merge($this->__defaultConfig, $info);
00272 
00273                 if (!isset($info['schema'])) {
00274                     $info['schema'] = null;
00275                 }
00276                 if (!isset($info['encoding'])) {
00277                     $info['encoding'] = null;
00278                 }
00279                 if (!isset($info['port'])) {
00280                     $info['port'] = null;
00281                 }
00282 
00283                 if($info['persistent'] === false) {
00284                     $info['persistent'] = 'false';
00285                 } else {
00286                     $info['persistent'] = 'false';
00287                 }
00288 
00289                 $oldConfigs[] = array(
00290                     'name' => $configName,
00291                     'driver' => $info['driver'],
00292                     'persistent' => $info['persistent'],
00293                     'host' => $info['host'],
00294                     'port' => $info['port'],
00295                     'login' => $info['login'],
00296                     'password' => $info['password'],
00297                     'database' => $info['database'],
00298                     'prefix' => $info['prefix'],
00299                     'schema' => $info['schema'],
00300                     'encoding' => $info['encoding']
00301                 );
00302             }
00303         }
00304 
00305         foreach ($oldConfigs as $key => $oldConfig) {
00306             foreach ($configs as $key1 => $config) {
00307                 if ($oldConfig['name'] == $config['name']) {
00308                     unset($oldConfigs[$key]);
00309                 }
00310             }
00311         }
00312 
00313         $configs = array_merge($oldConfigs, $configs);
00314         $out = "<?php\n";
00315         $out .= "class DATABASE_CONFIG {\n\n";
00316 
00317         foreach ($configs as $config) {
00318             $config = array_merge($this->__defaultConfig, $config);
00319             extract($config);
00320 
00321             $out .= "\tvar \${$name} = array(\n";
00322             $out .= "\t\t'driver' => '{$driver}',\n";
00323             $out .= "\t\t'persistent' => {$persistent},\n";
00324             $out .= "\t\t'host' => '{$host}',\n";
00325 
00326             if ($port) {
00327                 $out .= "\t\t'port' => {$port},\n";
00328             }
00329 
00330             $out .= "\t\t'login' => '{$login}',\n";
00331             $out .= "\t\t'password' => '{$password}',\n";
00332             $out .= "\t\t'database' => '{$database}',\n";
00333 
00334             if ($schema) {
00335                 $out .= "\t\t'schema' => '{$schema}',\n";
00336             }
00337 
00338             if ($prefix) {
00339                 $out .= "\t\t'prefix' => '{$prefix}',\n";
00340             }
00341 
00342             if ($encoding) {
00343                 $out .= "\t\t'encoding' => '{$encoding}'\n";
00344             }
00345 
00346             $out .= "\t);\n";
00347         }
00348 
00349         $out .= "}\n";
00350         $out .= "?>";
00351         $filename = $this->path.'database.php';
00352         return $this->createFile($filename, $out);
00353     }
00354 }
00355 ?>