mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
Initial version of a DBConfig task
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@3735 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
c186656ae3
commit
2b008eb66a
1 changed files with 144 additions and 0 deletions
144
cake/scripts/tasks/dbconfig_task.php
Normal file
144
cake/scripts/tasks/dbconfig_task.php
Normal file
|
@ -0,0 +1,144 @@
|
|||
<?php
|
||||
/* SVN FILE: $Id$ */
|
||||
/**
|
||||
* The DbconfigTask creates the database configuration file.
|
||||
*
|
||||
* Long description for file
|
||||
*
|
||||
* PHP versions 4 and 5
|
||||
*
|
||||
* CakePHP : Rapid Development Framework <http://www.cakephp.org/>
|
||||
* Copyright (c) 2005, 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 (c) 2005, Cake Software Foundation, Inc.
|
||||
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
|
||||
* @package cake
|
||||
* @subpackage cake.cake.scripts.bake
|
||||
* @since CakePHP v 1.2
|
||||
* @version $Revision$
|
||||
* @modifiedby $LastChangedBy$
|
||||
* @lastmodified $Date$
|
||||
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
||||
*/
|
||||
class DbconfigTask extends BakeTask {
|
||||
|
||||
var $database;
|
||||
var $user;
|
||||
var $password = '';
|
||||
var $configName = 'default';
|
||||
var $databaseDriver = 'mysql';
|
||||
var $persistent = 'false';
|
||||
var $host = 'localhost';
|
||||
var $port = '';
|
||||
var $prefix = '';
|
||||
|
||||
function execute($params) {
|
||||
|
||||
$paramCount = count($params);
|
||||
|
||||
if ($paramCount >= 2) {
|
||||
$this->database = array_shift($params);
|
||||
$this->user = array_shift($params);
|
||||
|
||||
if ($paramCount > 2) {
|
||||
$this->handleParams($params);
|
||||
}
|
||||
|
||||
$this->createFile(CONFIGS.'database.php', $this->getFileContent());
|
||||
|
||||
} else {
|
||||
$this->help();
|
||||
}
|
||||
}
|
||||
|
||||
function help() {
|
||||
echo "The dbconfig task creates the database configuration file for you.\n";
|
||||
echo "Usage: bake2 dbconfig [app-alias] database user [password] [-c=configName] [-d=databaseDriver] \n";
|
||||
echo " [-persistent] [-h=hostname[:port]] [-p=prefix]\n";
|
||||
}
|
||||
|
||||
function beginsWith($str, $sub) {
|
||||
return (substr($str, 0, strlen($sub)) === $sub);
|
||||
}
|
||||
|
||||
function createFile($path, $content) {
|
||||
|
||||
if ($f = fopen($path, 'w')) {
|
||||
fwrite($f, $content);
|
||||
fclose($f);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function getFileContent() {
|
||||
$out = "<?php\n";
|
||||
$out .= "class DATABASE_CONFIG {\n\n";
|
||||
$out .= "\tvar \${$this->configName} = array(\n";
|
||||
$out .= "\t\t'driver' => '{$this->databaseDriver}',\n";
|
||||
$out .= "\t\t'host' => '{$this->host}',\n";
|
||||
|
||||
if ($this->port != '') {
|
||||
$out .= "\t\t'port' => {$this->port}, \n";
|
||||
}
|
||||
|
||||
$out .= "\t\t'login' => '{$this->user}',\n";
|
||||
$out .= "\t\t'password' => '{$this->password}',\n";
|
||||
$out .= "\t\t'database' => '{$this->database}', \n";
|
||||
$out .= "\t\t'persistent' => {$this->persistent}, \n";
|
||||
$out .= "\t\t'prefix' => '{$this->prefix}' \n";
|
||||
$out .= "\t);\n";
|
||||
$out .= "}\n";
|
||||
$out .= "?>";
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
function handleParams($params) {
|
||||
if (!$this->beginsWith($params[0], '-')) {
|
||||
$this->password = array_shift($params);
|
||||
}
|
||||
|
||||
while (count($params) > 0) {
|
||||
$param = array_shift($params);
|
||||
$firstThreeChars = substr($param, 0, 3);
|
||||
$paramValue = substr($param, 3);
|
||||
|
||||
switch ($firstThreeChars) {
|
||||
case '-c=':
|
||||
$this->configName = $paramValue;
|
||||
break;
|
||||
case '-d=':
|
||||
$this->databaseDriver = $paramValue;
|
||||
break;
|
||||
case '-pe':
|
||||
$this->persistent = 'true';
|
||||
break;
|
||||
case '-h=':
|
||||
if (strpos($paramValue, ':') === false) {
|
||||
$this->host = $paramValue;
|
||||
} else {
|
||||
$hostData = explode(':', $paramValue);
|
||||
$this->host = $hostData[0];
|
||||
$this->port = $hostData[1];
|
||||
}
|
||||
|
||||
break;
|
||||
case '-p=':
|
||||
$this->prefix = $paramValue;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
Loading…
Add table
Reference in a new issue