/cake/console/libs/tasks/db_config.php

https://github.com/hardsshah/bookmarks · PHP · 353 lines · 235 code · 46 blank · 72 comment · 73 complexity · f22b3ec5838fe026fd97239c4fd67900 MD5 · raw file

  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * The DbConfig Task handles creating and updating the database.php
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
  11. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The MIT License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  19. * @package cake
  20. * @subpackage cake.cake.console.libs.tasks
  21. * @since CakePHP(tm) v 1.2
  22. * @version $Revision$
  23. * @modifiedby $LastChangedBy$
  24. * @lastmodified $Date$
  25. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  26. */
  27. if (!class_exists('File')) {
  28. uses('file');
  29. }
  30. /**
  31. * Task class for creating and updating the database configuration file.
  32. *
  33. * @package cake
  34. * @subpackage cake.cake.console.libs.tasks
  35. */
  36. class DbConfigTask extends Shell {
  37. /**
  38. * path to CONFIG directory
  39. *
  40. * @var string
  41. * @access public
  42. */
  43. var $path = null;
  44. /**
  45. * Default configuration settings to use
  46. *
  47. * @var array
  48. * @access private
  49. */
  50. var $__defaultConfig = array(
  51. 'name' => 'default', 'driver'=> 'mysql', 'persistent'=> 'false', 'host'=> 'localhost',
  52. 'login'=> 'root', 'password'=> 'password', 'database'=> 'project_name',
  53. 'schema'=> null, 'prefix'=> null, 'encoding' => null, 'port' => null
  54. );
  55. /**
  56. * initialization callback
  57. *
  58. * @var string
  59. * @access public
  60. */
  61. function initialize() {
  62. $this->path = $this->params['working'] . DS . 'config' . DS;
  63. }
  64. /**
  65. * Execution method always used for tasks
  66. *
  67. * @access public
  68. */
  69. function execute() {
  70. if (empty($this->args)) {
  71. $this->__interactive();
  72. $this->_stop();
  73. }
  74. }
  75. /**
  76. * Interactive interface
  77. *
  78. * @access private
  79. */
  80. function __interactive() {
  81. $this->hr();
  82. $this->out('Database Configuration:');
  83. $this->hr();
  84. $done = false;
  85. $dbConfigs = array();
  86. while ($done == false) {
  87. $name = '';
  88. while ($name == '') {
  89. $name = $this->in("Name:", null, 'default');
  90. if (preg_match('/[^a-z0-9_]/i', $name)) {
  91. $name = '';
  92. $this->out('The name may only contain unaccented latin characters, numbers or underscores');
  93. }
  94. else if (preg_match('/^[^a-z_]/i', $name)) {
  95. $name = '';
  96. $this->out('The name must start with an unaccented latin character or an underscore');
  97. }
  98. }
  99. $driver = '';
  100. while ($driver == '') {
  101. $driver = $this->in('Driver:', array('db2', 'firebird', 'mssql', 'mysql', 'mysqli', 'odbc', 'oracle', 'postgres', 'sqlite', 'sybase'), 'mysql');
  102. }
  103. $persistent = '';
  104. while ($persistent == '') {
  105. $persistent = $this->in('Persistent Connection?', array('y', 'n'), 'n');
  106. }
  107. if (low($persistent) == 'n') {
  108. $persistent = 'false';
  109. } else {
  110. $persistent = 'true';
  111. }
  112. $host = '';
  113. while ($host == '') {
  114. $host = $this->in('Database Host:', null, 'localhost');
  115. }
  116. $port = '';
  117. while ($port == '') {
  118. $port = $this->in('Port?', null, 'n');
  119. }
  120. if (low($port) == 'n') {
  121. $port = null;
  122. }
  123. $login = '';
  124. while ($login == '') {
  125. $login = $this->in('User:', null, 'root');
  126. }
  127. $password = '';
  128. $blankPassword = false;
  129. while ($password == '' && $blankPassword == false) {
  130. $password = $this->in('Password:');
  131. if ($password == '') {
  132. $blank = $this->in('The password you supplied was empty. Use an empty password?', array('y', 'n'), 'n');
  133. if ($blank == 'y')
  134. {
  135. $blankPassword = true;
  136. }
  137. }
  138. }
  139. $database = '';
  140. while ($database == '') {
  141. $database = $this->in('Database Name:', null, 'cake');
  142. }
  143. $prefix = '';
  144. while ($prefix == '') {
  145. $prefix = $this->in('Table Prefix?', null, 'n');
  146. }
  147. if (low($prefix) == 'n') {
  148. $prefix = null;
  149. }
  150. $encoding = '';
  151. while ($encoding == '') {
  152. $encoding = $this->in('Table encoding?', null, 'n');
  153. }
  154. if (low($encoding) == 'n') {
  155. $encoding = null;
  156. }
  157. $schema = '';
  158. if ($driver == 'postgres') {
  159. while ($schema == '') {
  160. $schema = $this->in('Table schema?', null, 'n');
  161. }
  162. }
  163. if (low($schema) == 'n') {
  164. $schema = null;
  165. }
  166. $config = compact('name', 'driver', 'persistent', 'host', 'login', 'password', 'database', 'prefix', 'encoding', 'port', 'schema');
  167. while ($this->__verify($config) == false) {
  168. $this->__interactive();
  169. }
  170. $dbConfigs[] = $config;
  171. $doneYet = $this->in('Do you wish to add another database configuration?', null, 'n');
  172. if (low($doneYet == 'n')) {
  173. $done = true;
  174. }
  175. }
  176. $this->bake($dbConfigs);
  177. config('database');
  178. return true;
  179. }
  180. /**
  181. * Output verification message and bake if it looks good
  182. *
  183. * @return boolean True if user says it looks good, false otherwise
  184. * @access private
  185. */
  186. function __verify($config) {
  187. $config = array_merge($this->__defaultConfig, $config);
  188. extract($config);
  189. $this->out('');
  190. $this->hr();
  191. $this->out('The following database configuration will be created:');
  192. $this->hr();
  193. $this->out("Name: $name");
  194. $this->out("Driver: $driver");
  195. $this->out("Persistent: $persistent");
  196. $this->out("Host: $host");
  197. if ($port) {
  198. $this->out("Port: $port");
  199. }
  200. $this->out("User: $login");
  201. $this->out("Pass: " . str_repeat('*', strlen($password)));
  202. $this->out("Database: $database");
  203. if ($prefix) {
  204. $this->out("Table prefix: $prefix");
  205. }
  206. if ($schema) {
  207. $this->out("Schema: $schema");
  208. }
  209. if ($encoding) {
  210. $this->out("Encoding: $encoding");
  211. }
  212. $this->hr();
  213. $looksGood = $this->in('Look okay?', array('y', 'n'), 'y');
  214. if (low($looksGood) == 'y' || low($looksGood) == 'yes') {
  215. return $config;
  216. }
  217. return false;
  218. }
  219. /**
  220. * Assembles and writes database.php
  221. *
  222. * @param array $configs Configuration settings to use
  223. * @return boolean Success
  224. * @access public
  225. */
  226. function bake($configs) {
  227. if (!is_dir($this->path)) {
  228. $this->err($this->path . ' not found');
  229. return false;
  230. }
  231. $filename = $this->path . 'database.php';
  232. $oldConfigs = array();
  233. if (file_exists($filename)) {
  234. $db = new DATABASE_CONFIG;
  235. $temp = get_class_vars(get_class($db));
  236. foreach ($temp as $configName => $info) {
  237. $info = array_merge($this->__defaultConfig, $info);
  238. if (!isset($info['schema'])) {
  239. $info['schema'] = null;
  240. }
  241. if (!isset($info['encoding'])) {
  242. $info['encoding'] = null;
  243. }
  244. if (!isset($info['port'])) {
  245. $info['port'] = null;
  246. }
  247. if ($info['persistent'] === false) {
  248. $info['persistent'] = 'false';
  249. } else {
  250. $info['persistent'] = 'false';
  251. }
  252. $oldConfigs[] = array(
  253. 'name' => $configName,
  254. 'driver' => $info['driver'],
  255. 'persistent' => $info['persistent'],
  256. 'host' => $info['host'],
  257. 'port' => $info['port'],
  258. 'login' => $info['login'],
  259. 'password' => $info['password'],
  260. 'database' => $info['database'],
  261. 'prefix' => $info['prefix'],
  262. 'schema' => $info['schema'],
  263. 'encoding' => $info['encoding']
  264. );
  265. }
  266. }
  267. foreach ($oldConfigs as $key => $oldConfig) {
  268. foreach ($configs as $key1 => $config) {
  269. if ($oldConfig['name'] == $config['name']) {
  270. unset($oldConfigs[$key]);
  271. }
  272. }
  273. }
  274. $configs = array_merge($oldConfigs, $configs);
  275. $out = "<?php\n";
  276. $out .= "class DATABASE_CONFIG {\n\n";
  277. foreach ($configs as $config) {
  278. $config = array_merge($this->__defaultConfig, $config);
  279. extract($config);
  280. $out .= "\tvar \${$name} = array(\n";
  281. $out .= "\t\t'driver' => '{$driver}',\n";
  282. $out .= "\t\t'persistent' => {$persistent},\n";
  283. $out .= "\t\t'host' => '{$host}',\n";
  284. if ($port) {
  285. $out .= "\t\t'port' => {$port},\n";
  286. }
  287. $out .= "\t\t'login' => '{$login}',\n";
  288. $out .= "\t\t'password' => '{$password}',\n";
  289. $out .= "\t\t'database' => '{$database}',\n";
  290. if ($schema) {
  291. $out .= "\t\t'schema' => '{$schema}',\n";
  292. }
  293. if ($prefix) {
  294. $out .= "\t\t'prefix' => '{$prefix}',\n";
  295. }
  296. if ($encoding) {
  297. $out .= "\t\t'encoding' => '{$encoding}'\n";
  298. }
  299. $out .= "\t);\n";
  300. }
  301. $out .= "}\n";
  302. $out .= "?>";
  303. $filename = $this->path.'database.php';
  304. return $this->createFile($filename, $out);
  305. }
  306. }
  307. ?>