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