From 2b008eb66ab4701a3e50f5dda1ecd90b06962bcb Mon Sep 17 00:00:00 2001 From: dho Date: Sun, 22 Oct 2006 16:04:30 +0000 Subject: [PATCH] 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 --- cake/scripts/tasks/dbconfig_task.php | 144 +++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 cake/scripts/tasks/dbconfig_task.php diff --git a/cake/scripts/tasks/dbconfig_task.php b/cake/scripts/tasks/dbconfig_task.php new file mode 100644 index 000000000..07670000a --- /dev/null +++ b/cake/scripts/tasks/dbconfig_task.php @@ -0,0 +1,144 @@ + + * 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 = "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; + } + } + } +} +?> \ No newline at end of file