-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCreateDatabaseTask.php
More file actions
221 lines (196 loc) · 5.55 KB
/
CreateDatabaseTask.php
File metadata and controls
221 lines (196 loc) · 5.55 KB
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
<?php
/**
* データベースの作成
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('InstallAppTask', 'Install.Console/Command');
/**
* データベースの作成
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Install\Console\Command
*/
class CreateDatabaseTask extends InstallAppTask {
/**
* datasourceのオプションKey
*
* @var string
*/
const KEY_DATASOURCE = 'datasource';
/**
* hostのオプションKey
*
* @var string
*/
const KEY_HOST = 'host';
/**
* portのオプションKey
*
* @var string
*/
const KEY_PORT = 'port';
/**
* databaseのオプションKey
*
* @var string
*/
const KEY_DATABASE = 'database';
/**
* prefixのオプションKey
*
* @var string
*/
const KEY_PREFIX = 'prefix';
/**
* loginのオプションKey
*
* @var string
*/
const KEY_LOGIN = 'login';
/**
* passwordのオプションKey
*
* @var string
*/
const KEY_PASSWORD = 'password';
/**
* Execution method always used for tasks
*
* @return void
*/
public function execute() {
parent::execute();
//database.phpの初期化処理
$configs = $this->InstallUtil->chooseDBByEnvironment();
if (! $this->InstallUtil->saveDBConf($configs)) {
$message = __d(
'install',
'Failed to write %s. Please check permission.',
array(APP . 'Config' . DS . 'database.php')
);
return $this->error($message);
}
//引数のセット
$this->__prepare();
//データベースの設定
if ($this->InstallUtil->validatesDBConf($this->data)) {
$this->InstallUtil->saveDBConf($this->data);
} else {
$message = var_export($this->InstallUtil->validationErrors, true);
return $this->error('validation error', $message);
}
//データベース作成
if (! $this->InstallUtil->createDB($this->data)) {
return $this->error(__d('install', 'Failed to create database.'));
}
}
/**
* 引数のデータセット
*
* @return void
*/
private function __prepare() {
//引数のセット
$this->data = array();
// * datasource
if (array_key_exists(self::KEY_DATASOURCE, $this->params)) {
$this->data[self::KEY_DATASOURCE] = Hash::get($this->params, self::KEY_DATASOURCE, 'Mysql');
} else {
$this->data[self::KEY_DATASOURCE] = $this->in(
__d('install', 'Database configuration\'s datasource?'), ['Mysql'], 'Mysql'
);
}
$this->data[self::KEY_DATASOURCE] = 'Database/' . $this->data[self::KEY_DATASOURCE];
// * host
if (array_key_exists(self::KEY_HOST, $this->params)) {
$this->data[self::KEY_HOST] = Hash::get($this->params, self::KEY_HOST, 'localhost');
} else {
$this->data[self::KEY_HOST] = $this->in(
__d('install', 'Database configuration\'s host?'), '', 'localhost'
);
}
// * port
if (array_key_exists(self::KEY_PORT, $this->params)) {
$this->data[self::KEY_PORT] = Hash::get($this->params, self::KEY_PORT, '3306');
} else {
$this->data[self::KEY_PORT] = $this->in(
__d('install', 'Database configuration\'s port?'), '', '3306'
);
}
// * database
if (array_key_exists(self::KEY_DATABASE, $this->params)) {
$this->data[self::KEY_DATABASE] = Hash::get($this->params, self::KEY_DATABASE, 'nc3');
} else {
$this->data[self::KEY_DATABASE] = $this->in(
__d('install', 'Database configuration\'s database?'), '', 'nc3'
);
}
// * prefix
if (array_key_exists(self::KEY_PREFIX, $this->params)) {
$this->data[self::KEY_PREFIX] = Hash::get($this->params, self::KEY_PREFIX, '');
} else {
$this->data[self::KEY_PREFIX] = $this->in(
__d('install', 'Database configuration\'s prefix?'), '', ''
);
}
// * login
if (array_key_exists(self::KEY_LOGIN, $this->params)) {
$this->data[self::KEY_LOGIN] = Hash::get($this->params, self::KEY_LOGIN);
} else {
$this->data[self::KEY_LOGIN] = $this->in(
__d('install', 'Database configuration\'s login?')
);
}
// * password
if (array_key_exists(self::KEY_PASSWORD, $this->params)) {
$this->data[self::KEY_PASSWORD] = Hash::get($this->params, self::KEY_PASSWORD);
} else {
$this->data[self::KEY_PASSWORD] = $this->in(
__d('install', 'Database configuration\'s password?')
);
}
}
/**
* Gets the option parser instance and configures it.
*
* @return ConsoleOptionParser
*/
public function getOptionParser() {
$parser = parent::getOptionParser();
$parser->description(__d('install', 'NetCommons Install Step 3'))
->addOption(self::KEY_DATASOURCE, array(
'help' => __d('install', 'Database configuration\'s datasource. (Mysql) [Mysql]'),
'required' => false
))
->addOption(self::KEY_HOST, array(
'help' => __d('install', 'Database configuration\'s host. [localhost]'),
'required' => false
))
->addOption(self::KEY_PORT, array(
'help' => __d('install', 'Database configuration\'s port. [3306]'),
'required' => false
))
->addOption(self::KEY_DATABASE, array(
'help' => __d('install', 'Database configuration\'s database. [nc3]'),
'required' => false
))
->addOption(self::KEY_PREFIX, array(
'help' => __d('install', 'Database configuration\'s prefix. [\'\']'),
'required' => false
))
->addOption(self::KEY_LOGIN, array(
'help' => __d('install', 'Database configuration\'s login.'),
'required' => false
))
->addOption(self::KEY_PASSWORD, array(
'help' => __d('install', 'Database configuration\'s password.'),
'required' => false
));
return $parser;
}
}