DbConfigTask.php 9.2 KB

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