| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* SVN FILE: $Id: db_config.php 7945 2008-12-19 02:16:01Z gwoo $ */
|
|
|
3 |
/**
|
|
|
4 |
* The DbConfig Task handles creating and updating the database.php
|
|
|
5 |
*
|
|
|
6 |
* Long description for file
|
|
|
7 |
*
|
|
|
8 |
* PHP versions 4 and 5
|
|
|
9 |
*
|
|
|
10 |
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
|
|
|
11 |
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
12 |
*
|
|
|
13 |
* Licensed under The MIT License
|
|
|
14 |
* Redistributions of files must retain the above copyright notice.
|
|
|
15 |
*
|
|
|
16 |
* @filesource
|
|
|
17 |
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
18 |
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
|
|
19 |
* @package cake
|
|
|
20 |
* @subpackage cake.cake.console.libs.tasks
|
|
|
21 |
* @since CakePHP(tm) v 1.2
|
|
|
22 |
* @version $Revision: 7945 $
|
|
|
23 |
* @modifiedby $LastChangedBy: gwoo $
|
|
|
24 |
* @lastmodified $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
|
|
|
25 |
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
26 |
*/
|
|
|
27 |
if (!class_exists('File')) {
|
|
|
28 |
uses('file');
|
|
|
29 |
}
|
|
|
30 |
/**
|
|
|
31 |
* Task class for creating and updating the database configuration file.
|
|
|
32 |
*
|
|
|
33 |
* @package cake
|
|
|
34 |
* @subpackage cake.cake.console.libs.tasks
|
|
|
35 |
*/
|
|
|
36 |
class DbConfigTask extends Shell {
|
|
|
37 |
/**
|
|
|
38 |
* path to CONFIG directory
|
|
|
39 |
*
|
|
|
40 |
* @var string
|
|
|
41 |
* @access public
|
|
|
42 |
*/
|
|
|
43 |
var $path = null;
|
|
|
44 |
/**
|
|
|
45 |
* Default configuration settings to use
|
|
|
46 |
*
|
|
|
47 |
* @var array
|
|
|
48 |
* @access private
|
|
|
49 |
*/
|
|
|
50 |
var $__defaultConfig = array(
|
|
|
51 |
'name' => 'default', 'driver'=> 'mysql', 'persistent'=> 'false', 'host'=> 'localhost',
|
|
|
52 |
'login'=> 'root', 'password'=> 'password', 'database'=> 'project_name',
|
|
|
53 |
'schema'=> null, 'prefix'=> null, 'encoding' => null, 'port' => null
|
|
|
54 |
);
|
|
|
55 |
/**
|
|
|
56 |
* initialization callback
|
|
|
57 |
*
|
|
|
58 |
* @var string
|
|
|
59 |
* @access public
|
|
|
60 |
*/
|
|
|
61 |
function initialize() {
|
|
|
62 |
$this->path = $this->params['working'] . DS . 'config' . DS;
|
|
|
63 |
}
|
|
|
64 |
/**
|
|
|
65 |
* Execution method always used for tasks
|
|
|
66 |
*
|
|
|
67 |
* @access public
|
|
|
68 |
*/
|
|
|
69 |
function execute() {
|
|
|
70 |
if (empty($this->args)) {
|
|
|
71 |
$this->__interactive();
|
|
|
72 |
$this->_stop();
|
|
|
73 |
}
|
|
|
74 |
}
|
|
|
75 |
/**
|
|
|
76 |
* Interactive interface
|
|
|
77 |
*
|
|
|
78 |
* @access private
|
|
|
79 |
*/
|
|
|
80 |
function __interactive() {
|
|
|
81 |
$this->hr();
|
|
|
82 |
$this->out('Database Configuration:');
|
|
|
83 |
$this->hr();
|
|
|
84 |
$done = false;
|
|
|
85 |
$dbConfigs = array();
|
|
|
86 |
|
|
|
87 |
while ($done == false) {
|
|
|
88 |
$name = '';
|
|
|
89 |
|
|
|
90 |
while ($name == '') {
|
|
|
91 |
$name = $this->in("Name:", null, 'default');
|
|
|
92 |
if (preg_match('/[^a-z0-9_]/i', $name)) {
|
|
|
93 |
$name = '';
|
|
|
94 |
$this->out('The name may only contain unaccented latin characters, numbers or underscores');
|
|
|
95 |
}
|
|
|
96 |
else if (preg_match('/^[^a-z_]/i', $name)) {
|
|
|
97 |
$name = '';
|
|
|
98 |
$this->out('The name must start with an unaccented latin character or an underscore');
|
|
|
99 |
}
|
|
|
100 |
}
|
|
|
101 |
$driver = '';
|
|
|
102 |
|
|
|
103 |
while ($driver == '') {
|
|
|
104 |
$driver = $this->in('Driver:', array('db2', 'firebird', 'mssql', 'mysql', 'mysqli', 'odbc', 'oracle', 'postgres', 'sqlite', 'sybase'), 'mysql');
|
|
|
105 |
}
|
|
|
106 |
$persistent = '';
|
|
|
107 |
|
|
|
108 |
while ($persistent == '') {
|
|
|
109 |
$persistent = $this->in('Persistent Connection?', array('y', 'n'), 'n');
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
if (low($persistent) == 'n') {
|
|
|
113 |
$persistent = 'false';
|
|
|
114 |
} else {
|
|
|
115 |
$persistent = 'true';
|
|
|
116 |
}
|
|
|
117 |
$host = '';
|
|
|
118 |
|
|
|
119 |
while ($host == '') {
|
|
|
120 |
$host = $this->in('Database Host:', null, 'localhost');
|
|
|
121 |
}
|
|
|
122 |
$port = '';
|
|
|
123 |
|
|
|
124 |
while ($port == '') {
|
|
|
125 |
$port = $this->in('Port?', null, 'n');
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
if (low($port) == 'n') {
|
|
|
129 |
$port = null;
|
|
|
130 |
}
|
|
|
131 |
$login = '';
|
|
|
132 |
|
|
|
133 |
while ($login == '') {
|
|
|
134 |
$login = $this->in('User:', null, 'root');
|
|
|
135 |
}
|
|
|
136 |
$password = '';
|
|
|
137 |
$blankPassword = false;
|
|
|
138 |
|
|
|
139 |
while ($password == '' && $blankPassword == false) {
|
|
|
140 |
$password = $this->in('Password:');
|
|
|
141 |
|
|
|
142 |
if ($password == '') {
|
|
|
143 |
$blank = $this->in('The password you supplied was empty. Use an empty password?', array('y', 'n'), 'n');
|
|
|
144 |
if ($blank == 'y')
|
|
|
145 |
{
|
|
|
146 |
$blankPassword = true;
|
|
|
147 |
}
|
|
|
148 |
}
|
|
|
149 |
}
|
|
|
150 |
$database = '';
|
|
|
151 |
|
|
|
152 |
while ($database == '') {
|
|
|
153 |
$database = $this->in('Database Name:', null, 'cake');
|
|
|
154 |
}
|
|
|
155 |
$prefix = '';
|
|
|
156 |
|
|
|
157 |
while ($prefix == '') {
|
|
|
158 |
$prefix = $this->in('Table Prefix?', null, 'n');
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
if (low($prefix) == 'n') {
|
|
|
162 |
$prefix = null;
|
|
|
163 |
}
|
|
|
164 |
$encoding = '';
|
|
|
165 |
|
|
|
166 |
while ($encoding == '') {
|
|
|
167 |
$encoding = $this->in('Table encoding?', null, 'n');
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
if (low($encoding) == 'n') {
|
|
|
171 |
$encoding = null;
|
|
|
172 |
}
|
|
|
173 |
$schema = '';
|
|
|
174 |
|
|
|
175 |
if ($driver == 'postgres') {
|
|
|
176 |
while ($schema == '') {
|
|
|
177 |
$schema = $this->in('Table schema?', null, 'n');
|
|
|
178 |
}
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
if (low($schema) == 'n') {
|
|
|
182 |
$schema = null;
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
$config = compact('name', 'driver', 'persistent', 'host', 'login', 'password', 'database', 'prefix', 'encoding', 'port', 'schema');
|
|
|
186 |
|
|
|
187 |
while ($this->__verify($config) == false) {
|
|
|
188 |
$this->__interactive();
|
|
|
189 |
}
|
|
|
190 |
$dbConfigs[] = $config;
|
|
|
191 |
$doneYet = $this->in('Do you wish to add another database configuration?', null, 'n');
|
|
|
192 |
|
|
|
193 |
if (low($doneYet == 'n')) {
|
|
|
194 |
$done = true;
|
|
|
195 |
}
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
$this->bake($dbConfigs);
|
|
|
199 |
config('database');
|
|
|
200 |
return true;
|
|
|
201 |
}
|
|
|
202 |
/**
|
|
|
203 |
* Output verification message and bake if it looks good
|
|
|
204 |
*
|
|
|
205 |
* @return boolean True if user says it looks good, false otherwise
|
|
|
206 |
* @access private
|
|
|
207 |
*/
|
|
|
208 |
function __verify($config) {
|
|
|
209 |
$config = array_merge($this->__defaultConfig, $config);
|
|
|
210 |
extract($config);
|
|
|
211 |
$this->out('');
|
|
|
212 |
$this->hr();
|
|
|
213 |
$this->out('The following database configuration will be created:');
|
|
|
214 |
$this->hr();
|
|
|
215 |
$this->out("Name: $name");
|
|
|
216 |
$this->out("Driver: $driver");
|
|
|
217 |
$this->out("Persistent: $persistent");
|
|
|
218 |
$this->out("Host: $host");
|
|
|
219 |
|
|
|
220 |
if ($port) {
|
|
|
221 |
$this->out("Port: $port");
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
$this->out("User: $login");
|
|
|
225 |
$this->out("Pass: " . str_repeat('*', strlen($password)));
|
|
|
226 |
$this->out("Database: $database");
|
|
|
227 |
|
|
|
228 |
if ($prefix) {
|
|
|
229 |
$this->out("Table prefix: $prefix");
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
if ($schema) {
|
|
|
233 |
$this->out("Schema: $schema");
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
if ($encoding) {
|
|
|
237 |
$this->out("Encoding: $encoding");
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
$this->hr();
|
|
|
241 |
$looksGood = $this->in('Look okay?', array('y', 'n'), 'y');
|
|
|
242 |
|
|
|
243 |
if (low($looksGood) == 'y' || low($looksGood) == 'yes') {
|
|
|
244 |
return $config;
|
|
|
245 |
}
|
|
|
246 |
return false;
|
|
|
247 |
}
|
|
|
248 |
/**
|
|
|
249 |
* Assembles and writes database.php
|
|
|
250 |
*
|
|
|
251 |
* @param array $configs Configuration settings to use
|
|
|
252 |
* @return boolean Success
|
|
|
253 |
* @access public
|
|
|
254 |
*/
|
|
|
255 |
function bake($configs) {
|
|
|
256 |
if (!is_dir($this->path)) {
|
|
|
257 |
$this->err($this->path . ' not found');
|
|
|
258 |
return false;
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
$filename = $this->path . 'database.php';
|
|
|
262 |
$oldConfigs = array();
|
|
|
263 |
|
|
|
264 |
if (file_exists($filename)) {
|
|
|
265 |
$db = new DATABASE_CONFIG;
|
|
|
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 |
if ($info['persistent'] === false) {
|
|
|
282 |
$info['persistent'] = 'false';
|
|
|
283 |
} else {
|
|
|
284 |
$info['persistent'] = 'false';
|
|
|
285 |
}
|
|
|
286 |
|
|
|
287 |
$oldConfigs[] = array(
|
|
|
288 |
'name' => $configName,
|
|
|
289 |
'driver' => $info['driver'],
|
|
|
290 |
'persistent' => $info['persistent'],
|
|
|
291 |
'host' => $info['host'],
|
|
|
292 |
'port' => $info['port'],
|
|
|
293 |
'login' => $info['login'],
|
|
|
294 |
'password' => $info['password'],
|
|
|
295 |
'database' => $info['database'],
|
|
|
296 |
'prefix' => $info['prefix'],
|
|
|
297 |
'schema' => $info['schema'],
|
|
|
298 |
'encoding' => $info['encoding']
|
|
|
299 |
);
|
|
|
300 |
}
|
|
|
301 |
}
|
|
|
302 |
|
|
|
303 |
foreach ($oldConfigs as $key => $oldConfig) {
|
|
|
304 |
foreach ($configs as $key1 => $config) {
|
|
|
305 |
if ($oldConfig['name'] == $config['name']) {
|
|
|
306 |
unset($oldConfigs[$key]);
|
|
|
307 |
}
|
|
|
308 |
}
|
|
|
309 |
}
|
|
|
310 |
|
|
|
311 |
$configs = array_merge($oldConfigs, $configs);
|
|
|
312 |
$out = "<?php\n";
|
|
|
313 |
$out .= "class DATABASE_CONFIG {\n\n";
|
|
|
314 |
|
|
|
315 |
foreach ($configs as $config) {
|
|
|
316 |
$config = array_merge($this->__defaultConfig, $config);
|
|
|
317 |
extract($config);
|
|
|
318 |
|
|
|
319 |
$out .= "\tvar \${$name} = array(\n";
|
|
|
320 |
$out .= "\t\t'driver' => '{$driver}',\n";
|
|
|
321 |
$out .= "\t\t'persistent' => {$persistent},\n";
|
|
|
322 |
$out .= "\t\t'host' => '{$host}',\n";
|
|
|
323 |
|
|
|
324 |
if ($port) {
|
|
|
325 |
$out .= "\t\t'port' => {$port},\n";
|
|
|
326 |
}
|
|
|
327 |
|
|
|
328 |
$out .= "\t\t'login' => '{$login}',\n";
|
|
|
329 |
$out .= "\t\t'password' => '{$password}',\n";
|
|
|
330 |
$out .= "\t\t'database' => '{$database}',\n";
|
|
|
331 |
|
|
|
332 |
if ($schema) {
|
|
|
333 |
$out .= "\t\t'schema' => '{$schema}',\n";
|
|
|
334 |
}
|
|
|
335 |
|
|
|
336 |
if ($prefix) {
|
|
|
337 |
$out .= "\t\t'prefix' => '{$prefix}',\n";
|
|
|
338 |
}
|
|
|
339 |
|
|
|
340 |
if ($encoding) {
|
|
|
341 |
$out .= "\t\t'encoding' => '{$encoding}'\n";
|
|
|
342 |
}
|
|
|
343 |
|
|
|
344 |
$out .= "\t);\n";
|
|
|
345 |
}
|
|
|
346 |
|
|
|
347 |
$out .= "}\n";
|
|
|
348 |
$out .= "?>";
|
|
|
349 |
$filename = $this->path.'database.php';
|
|
|
350 |
return $this->createFile($filename, $out);
|
|
|
351 |
}
|
|
|
352 |
}
|
|
|
353 |
?>
|