fake_twitter /lib/Cake/Console/Command/Task/DbConfigTask.php

Language PHP Lines 384
MD5 Hash f8bdaf73e74648b706611f7627f8b956 Estimated Cost $6,328 (why?)
Repository https://bitbucket.org/udeshika/fake_twitter View Raw File View Project SPDX
  1
  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
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?php
/**
 * The DbConfig Task handles creating and updating the database.php
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @since         CakePHP(tm) v 1.2
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

App::uses('AppShell', 'Console/Command');

/**
 * Task class for creating and updating the database configuration file.
 *
 * @package       Cake.Console.Command.Task
 */
class DbConfigTask extends AppShell {

/**
 * path to CONFIG directory
 *
 * @var string
 */
	public $path = null;

/**
 * Default configuration settings to use
 *
 * @var array
 */
	protected $_defaultConfig = array(
		'name' => 'default',
		'datasource' => 'Database/Mysql',
		'persistent' => 'false',
		'host' => 'localhost',
		'login' => 'root',
		'password' => 'password',
		'database' => 'project_name',
		'schema' => null,
		'prefix' => null,
		'encoding' => null,
		'port' => null
	);

/**
 * String name of the database config class name.
 * Used for testing.
 *
 * @var string
 */
	public $databaseClassName = 'DATABASE_CONFIG';

/**
 * initialization callback
 *
 * @return void
 */
	public function initialize() {
		$this->path = APP . 'Config' . DS;
	}

/**
 * Execution method always used for tasks
 *
 * @return void
 */
	public function execute() {
		if (empty($this->args)) {
			$this->_interactive();
			$this->_stop();
		}
	}

/**
 * Interactive interface
 *
 * @return void
 */
	protected function _interactive() {
		$this->hr();
		$this->out('Database Configuration:');
		$this->hr();
		$done = false;
		$dbConfigs = array();

		while ($done == false) {
			$name = '';

			while ($name == '') {
				$name = $this->in(__d('cake_console', "Name:"), null, 'default');
				if (preg_match('/[^a-z0-9_]/i', $name)) {
					$name = '';
					$this->out(__d('cake_console', 'The name may only contain unaccented latin characters, numbers or underscores'));
				} else if (preg_match('/^[^a-z_]/i', $name)) {
					$name = '';
					$this->out(__d('cake_console', 'The name must start with an unaccented latin character or an underscore'));
				}
			}

			$datasource = $this->in(__d('cake_console', 'Datasource:'), array('Mysql', 'Postgres', 'Sqlite', 'Sqlserver'), 'Mysql');

			$persistent = $this->in(__d('cake_console', 'Persistent Connection?'), array('y', 'n'), 'n');
			if (strtolower($persistent) == 'n') {
				$persistent = 'false';
			} else {
				$persistent = 'true';
			}

			$host = '';
			while ($host == '') {
				$host = $this->in(__d('cake_console', 'Database Host:'), null, 'localhost');
			}

			$port = '';
			while ($port == '') {
				$port = $this->in(__d('cake_console', 'Port?'), null, 'n');
			}

			if (strtolower($port) == 'n') {
				$port = null;
			}

			$login = '';
			while ($login == '') {
				$login = $this->in(__d('cake_console', 'User:'), null, 'root');
			}
			$password = '';
			$blankPassword = false;

			while ($password == '' && $blankPassword == false) {
				$password = $this->in(__d('cake_console', 'Password:'));

				if ($password == '') {
					$blank = $this->in(__d('cake_console', 'The password you supplied was empty. Use an empty password?'), array('y', 'n'), 'n');
					if ($blank == 'y') {
						$blankPassword = true;
					}
				}
			}

			$database = '';
			while ($database == '') {
				$database = $this->in(__d('cake_console', 'Database Name:'), null, 'cake');
			}

			$prefix = '';
			while ($prefix == '') {
				$prefix = $this->in(__d('cake_console', 'Table Prefix?'), null, 'n');
			}
			if (strtolower($prefix) == 'n') {
				$prefix = null;
			}

			$encoding = '';
			while ($encoding == '') {
				$encoding = $this->in(__d('cake_console', 'Table encoding?'), null, 'n');
			}
			if (strtolower($encoding) == 'n') {
				$encoding = null;
			}

			$schema = '';
			if ($datasource == 'postgres') {
				while ($schema == '') {
					$schema = $this->in(__d('cake_console', 'Table schema?'), null, 'n');
				}
			}
			if (strtolower($schema) == 'n') {
				$schema = null;
			}

			$config = compact('name', 'datasource', 'persistent', 'host', 'login', 'password', 'database', 'prefix', 'encoding', 'port', 'schema');

			while ($this->_verify($config) == false) {
				$this->_interactive();
			}

			$dbConfigs[] = $config;
			$doneYet = $this->in(__d('cake_console', 'Do you wish to add another database configuration?'), null, 'n');

			if (strtolower($doneYet == 'n')) {
				$done = true;
			}
		}

		$this->bake($dbConfigs);
		config('database');
		return true;
	}

/**
 * Output verification message and bake if it looks good
 *
 * @param array $config
 * @return boolean True if user says it looks good, false otherwise
 */
	protected function _verify($config) {
		$config = array_merge($this->_defaultConfig, $config);
		extract($config);
		$this->out();
		$this->hr();
		$this->out(__d('cake_console', 'The following database configuration will be created:'));
		$this->hr();
		$this->out(__d('cake_console', "Name:         %s", $name));
		$this->out(__d('cake_console', "Datasource:       %s", $datasource));
		$this->out(__d('cake_console', "Persistent:   %s", $persistent));
		$this->out(__d('cake_console', "Host:         %s", $host));

		if ($port) {
			$this->out(__d('cake_console', "Port:         %s", $port));
		}

		$this->out(__d('cake_console', "User:         %s", $login));
		$this->out(__d('cake_console', "Pass:         %s", str_repeat('*', strlen($password))));
		$this->out(__d('cake_console', "Database:     %s", $database));

		if ($prefix) {
			$this->out(__d('cake_console', "Table prefix: %s", $prefix));
		}

		if ($schema) {
			$this->out(__d('cake_console', "Schema:       %s", $schema));
		}

		if ($encoding) {
			$this->out(__d('cake_console', "Encoding:     %s", $encoding));
		}

		$this->hr();
		$looksGood = $this->in(__d('cake_console', 'Look okay?'), array('y', 'n'), 'y');

		if (strtolower($looksGood) == 'y') {
			return $config;
		}
		return false;
	}

/**
 * Assembles and writes database.php
 *
 * @param array $configs Configuration settings to use
 * @return boolean Success
 */
	public function bake($configs) {
		if (!is_dir($this->path)) {
			$this->err(__d('cake_console', '%s not found', $this->path));
			return false;
		}

		$filename = $this->path . 'database.php';
		$oldConfigs = array();

		if (file_exists($filename)) {
			config('database');
			$db = new $this->databaseClassName;
			$temp = get_class_vars(get_class($db));

			foreach ($temp as $configName => $info) {
				$info = array_merge($this->_defaultConfig, $info);

				if (!isset($info['schema'])) {
					$info['schema'] = null;
				}
				if (!isset($info['encoding'])) {
					$info['encoding'] = null;
				}
				if (!isset($info['port'])) {
					$info['port'] = null;
				}

				if ($info['persistent'] === false) {
					$info['persistent'] = 'false';
				} else {
					$info['persistent'] = ($info['persistent'] == true) ? 'true' : 'false';
				}

				$oldConfigs[] = array(
					'name' => $configName,
					'datasource' => $info['datasource'],
					'persistent' => $info['persistent'],
					'host' => $info['host'],
					'port' => $info['port'],
					'login' => $info['login'],
					'password' => $info['password'],
					'database' => $info['database'],
					'prefix' => $info['prefix'],
					'schema' => $info['schema'],
					'encoding' => $info['encoding']
				);
			}
		}

		foreach ($oldConfigs as $key => $oldConfig) {
			foreach ($configs as $key1 => $config) {
				if ($oldConfig['name'] == $config['name']) {
					unset($oldConfigs[$key]);
				}
			}
		}

		$configs = array_merge($oldConfigs, $configs);
		$out = "<?php\n";
		$out .= "class DATABASE_CONFIG {\n\n";

		foreach ($configs as $config) {
			$config = array_merge($this->_defaultConfig, $config);
			extract($config);

			$out .= "\tpublic \${$name} = array(\n";
			$out .= "\t\t'datasource' => 'Database/{$datasource}',\n";
			$out .= "\t\t'persistent' => {$persistent},\n";
			$out .= "\t\t'host' => '{$host}',\n";

			if ($port) {
				$out .= "\t\t'port' => {$port},\n";
			}

			$out .= "\t\t'login' => '{$login}',\n";
			$out .= "\t\t'password' => '{$password}',\n";
			$out .= "\t\t'database' => '{$database}',\n";

			if ($schema) {
				$out .= "\t\t'schema' => '{$schema}',\n";
			}

			if ($prefix) {
				$out .= "\t\t'prefix' => '{$prefix}',\n";
			}

			if ($encoding) {
				$out .= "\t\t'encoding' => '{$encoding}'\n";
			}

			$out .= "\t);\n";
		}

		$out .= "}\n";
		$filename = $this->path . 'database.php';
		return $this->createFile($filename, $out);
	}

/**
 * Get a user specified Connection name
 *
 * @return void
 */
	public function getConfig() {
		App::uses('ConnectionManager', 'Model');
		$configs = ConnectionManager::enumConnectionObjects();

		$useDbConfig = key($configs);
		if (!is_array($configs) || empty($configs)) {
			return $this->execute();
		}
		$connections = array_keys($configs);

		if (count($connections) > 1) {
			$useDbConfig = $this->in(__d('cake_console', 'Use Database Config') .':', $connections, $useDbConfig);
		}
		return $useDbConfig;
	}

/**
 * get the option parser
 *
 * @return ConsoleOptionParser
 */
	public function getOptionParser() {
		$parser = parent::getOptionParser();
		return $parser->description(
				__d('cake_console', 'Bake new database configuration settings.')
			);
	}
}
Back to Top