1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
19:
20: App::uses('AppShell', 'Console/Command');
21:
22: 23: 24: 25: 26:
27: class DbConfigTask extends AppShell {
28:
29: 30: 31: 32: 33:
34: public $path = null;
35:
36: 37: 38: 39: 40:
41: protected $_defaultConfig = array(
42: 'name' => 'default',
43: 'datasource' => 'Database/Mysql',
44: 'persistent' => 'false',
45: 'host' => 'localhost',
46: 'login' => 'root',
47: 'password' => 'password',
48: 'database' => 'project_name',
49: 'schema' => null,
50: 'prefix' => null,
51: 'encoding' => null,
52: 'port' => null
53: );
54:
55: 56: 57: 58: 59: 60:
61: public $databaseClassName = 'DATABASE_CONFIG';
62:
63: 64: 65: 66: 67:
68: public function initialize() {
69: $this->path = APP . 'Config' . DS;
70: }
71:
72: 73: 74: 75: 76:
77: public function execute() {
78: if (empty($this->args)) {
79: $this->_interactive();
80: return $this->_stop();
81: }
82: }
83:
84: 85: 86: 87: 88:
89: protected function _interactive() {
90: $this->hr();
91: $this->out(__d('cake_console', 'Database Configuration:'));
92: $this->hr();
93: $done = false;
94: $dbConfigs = array();
95:
96: while (!$done) {
97: $name = '';
98:
99: while (!$name) {
100: $name = $this->in(__d('cake_console', "Name:"), null, 'default');
101: if (preg_match('/[^a-z0-9_]/i', $name)) {
102: $name = '';
103: $this->out(__d('cake_console', 'The name may only contain unaccented latin characters, numbers or underscores'));
104: } elseif (preg_match('/^[^a-z_]/i', $name)) {
105: $name = '';
106: $this->out(__d('cake_console', 'The name must start with an unaccented latin character or an underscore'));
107: }
108: }
109:
110: $datasource = $this->in(__d('cake_console', 'Datasource:'), array('Mysql', 'Postgres', 'Sqlite', 'Sqlserver'), 'Mysql');
111:
112: $persistent = $this->in(__d('cake_console', 'Persistent Connection?'), array('y', 'n'), 'n');
113: if (strtolower($persistent) === 'n') {
114: $persistent = 'false';
115: } else {
116: $persistent = 'true';
117: }
118:
119: $host = '';
120: while (!$host) {
121: $host = $this->in(__d('cake_console', 'Database Host:'), null, 'localhost');
122: }
123:
124: $port = '';
125: while (!$port) {
126: $port = $this->in(__d('cake_console', 'Port?'), null, 'n');
127: }
128:
129: if (strtolower($port) === 'n') {
130: $port = null;
131: }
132:
133: $login = '';
134: while (!$login) {
135: $login = $this->in(__d('cake_console', 'User:'), null, 'root');
136: }
137: $password = '';
138: $blankPassword = false;
139:
140: while (!$password && !$blankPassword) {
141: $password = $this->in(__d('cake_console', 'Password:'));
142:
143: if (!$password) {
144: $blank = $this->in(__d('cake_console', 'The password you supplied was empty. Use an empty password?'), array('y', 'n'), 'n');
145: if ($blank === 'y') {
146: $blankPassword = true;
147: }
148: }
149: }
150:
151: $database = '';
152: while (!$database) {
153: $database = $this->in(__d('cake_console', 'Database Name:'), null, 'cake');
154: }
155:
156: $prefix = '';
157: while (!$prefix) {
158: $prefix = $this->in(__d('cake_console', 'Table Prefix?'), null, 'n');
159: }
160: if (strtolower($prefix) === 'n') {
161: $prefix = null;
162: }
163:
164: $encoding = '';
165: while (!$encoding) {
166: $encoding = $this->in(__d('cake_console', 'Table encoding?'), null, 'n');
167: }
168: if (strtolower($encoding) === 'n') {
169: $encoding = null;
170: }
171:
172: $schema = '';
173: if ($datasource === 'postgres') {
174: while (!$schema) {
175: $schema = $this->in(__d('cake_console', 'Table schema?'), null, 'n');
176: }
177: }
178: if (strtolower($schema) === 'n') {
179: $schema = null;
180: }
181:
182: $config = compact('name', 'datasource', 'persistent', 'host', 'login', 'password', 'database', 'prefix', 'encoding', 'port', 'schema');
183:
184: while (!$this->_verify($config)) {
185: $this->_interactive();
186: }
187:
188: $dbConfigs[] = $config;
189: $doneYet = $this->in(__d('cake_console', 'Do you wish to add another database configuration?'), null, 'n');
190:
191: if (strtolower($doneYet === 'n')) {
192: $done = true;
193: }
194: }
195:
196: $this->bake($dbConfigs);
197: config('database');
198: return true;
199: }
200:
201: 202: 203: 204: 205: 206:
207: protected function _verify($config) {
208: $config = array_merge($this->_defaultConfig, $config);
209: extract($config);
210: $this->out();
211: $this->hr();
212: $this->out(__d('cake_console', 'The following database configuration will be created:'));
213: $this->hr();
214: $this->out(__d('cake_console', "Name: %s", $name));
215: $this->out(__d('cake_console', "Datasource: %s", $datasource));
216: $this->out(__d('cake_console', "Persistent: %s", $persistent));
217: $this->out(__d('cake_console', "Host: %s", $host));
218:
219: if ($port) {
220: $this->out(__d('cake_console', "Port: %s", $port));
221: }
222:
223: $this->out(__d('cake_console', "User: %s", $login));
224: $this->out(__d('cake_console', "Pass: %s", str_repeat('*', strlen($password))));
225: $this->out(__d('cake_console', "Database: %s", $database));
226:
227: if ($prefix) {
228: $this->out(__d('cake_console', "Table prefix: %s", $prefix));
229: }
230:
231: if ($schema) {
232: $this->out(__d('cake_console', "Schema: %s", $schema));
233: }
234:
235: if ($encoding) {
236: $this->out(__d('cake_console', "Encoding: %s", $encoding));
237: }
238:
239: $this->hr();
240: $looksGood = $this->in(__d('cake_console', 'Look okay?'), array('y', 'n'), 'y');
241:
242: if (strtolower($looksGood) === 'y') {
243: return $config;
244: }
245: return false;
246: }
247:
248: 249: 250: 251: 252: 253:
254: public function bake($configs) {
255: if (!is_dir($this->path)) {
256: $this->err(__d('cake_console', '%s not found', $this->path));
257: return false;
258: }
259:
260: $filename = $this->path . 'database.php';
261: $oldConfigs = array();
262:
263: if (file_exists($filename)) {
264: config('database');
265: $db = new $this->databaseClassName;
266: $temp = get_class_vars(get_class($db));
267:
268: foreach ($temp as $configName => $info) {
269: $info = array_merge($this->_defaultConfig, $info);
270:
271: if (!isset($info['schema'])) {
272: $info['schema'] = null;
273: }
274: if (!isset($info['encoding'])) {
275: $info['encoding'] = null;
276: }
277: if (!isset($info['port'])) {
278: $info['port'] = null;
279: }
280:
281: $info['persistent'] = var_export((bool)$info['persistent'], true);
282:
283: $oldConfigs[] = array(
284: 'name' => $configName,
285: 'datasource' => $info['datasource'],
286: 'persistent' => $info['persistent'],
287: 'host' => $info['host'],
288: 'port' => $info['port'],
289: 'login' => $info['login'],
290: 'password' => $info['password'],
291: 'database' => $info['database'],
292: 'prefix' => $info['prefix'],
293: 'schema' => $info['schema'],
294: 'encoding' => $info['encoding']
295: );
296: }
297: }
298:
299: foreach ($oldConfigs as $key => $oldConfig) {
300: foreach ($configs as $config) {
301: if ($oldConfig['name'] == $config['name']) {
302: unset($oldConfigs[$key]);
303: }
304: }
305: }
306:
307: $configs = array_merge($oldConfigs, $configs);
308: $out = "<?php\n";
309: $out .= "class DATABASE_CONFIG {\n\n";
310:
311: foreach ($configs as $config) {
312: $config = array_merge($this->_defaultConfig, $config);
313: extract($config);
314:
315: if (strpos($datasource, 'Database/') === false) {
316: $datasource = "Database/{$datasource}";
317: }
318: $out .= "\tpublic \${$name} = array(\n";
319: $out .= "\t\t'datasource' => '{$datasource}',\n";
320: $out .= "\t\t'persistent' => {$persistent},\n";
321: $out .= "\t\t'host' => '{$host}',\n";
322:
323: if ($port) {
324: $out .= "\t\t'port' => {$port},\n";
325: }
326:
327: $out .= "\t\t'login' => '{$login}',\n";
328: $out .= "\t\t'password' => '{$password}',\n";
329: $out .= "\t\t'database' => '{$database}',\n";
330:
331: if ($schema) {
332: $out .= "\t\t'schema' => '{$schema}',\n";
333: }
334:
335: if ($prefix) {
336: $out .= "\t\t'prefix' => '{$prefix}',\n";
337: }
338:
339: if ($encoding) {
340: $out .= "\t\t'encoding' => '{$encoding}'\n";
341: }
342:
343: $out .= "\t);\n";
344: }
345:
346: $out .= "}\n";
347: $filename = $this->path . 'database.php';
348: return $this->createFile($filename, $out);
349: }
350:
351: 352: 353: 354: 355:
356: public function getConfig() {
357: App::uses('ConnectionManager', 'Model');
358: $configs = ConnectionManager::enumConnectionObjects();
359:
360: $useDbConfig = key($configs);
361: if (!is_array($configs) || empty($configs)) {
362: return $this->execute();
363: }
364: $connections = array_keys($configs);
365:
366: if (count($connections) > 1) {
367: $useDbConfig = $this->in(__d('cake_console', 'Use Database Config') . ':', $connections, $useDbConfig);
368: }
369: return $useDbConfig;
370: }
371:
372: 373: 374: 375: 376:
377: public function getOptionParser() {
378: $parser = parent::getOptionParser();
379: return $parser->description(
380: __d('cake_console', 'Bake new database configuration settings.')
381: );
382: }
383:
384: }
385: