PageRenderTime 78ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

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