DbConfigTask.php 9.4 KB

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