PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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