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