DbConfigTask.php 9.3 KB

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