1
1
<?php
2
-
2
+ /**
3
+ * FecMall file.
4
+ *
5
+ * @link http://www.fecmall.com/
6
+ * @copyright Copyright (c) 2016 FecMall Software LLC
7
+ * @license http://www.fecmall.com/license/
8
+ */
3
9
namespace fecshop \app \appinstall \modules \Database \controllers ;
4
10
5
11
use Yii ;
6
-
12
+ /**
13
+ * @author Terry Zhao <2358269014@qq.com>
14
+ * @since 1.0
15
+ */
7
16
class ConfigController extends \yii \web \Controller
8
17
{
9
18
public function init ()
@@ -13,12 +22,30 @@ public function init()
13
22
14
23
public $ _migrateLog = '' ;
15
24
16
- // 数据库管理
25
+ // 安装默认第一步页面
26
+ public function actionIndex ()
27
+ {
28
+ $ editForm = Yii::$ app ->request ->post ('editForm ' );
29
+ if ($ editForm && $ this ->checkDatabaseData ($ editForm )
30
+ && $ this ->updateDatabase ($ editForm )) {
31
+ Yii::$ app ->session ->setFlash ('database-success ' , 'mysql config set success, mysql config file path: @common/config/main-local.php ' );
32
+ // 进行跳转
33
+ $ homeUrl = Yii::$ app ->homeUrl ;
34
+ return $ this ->redirect ($ homeUrl .'/database/config/migrate ' );
35
+ }
36
+ $ errorInfo = Yii::$ app ->session ->getFlash ('database-errors ' );
37
+ $ errorInfo = $ this ->getErrorHtml ($ errorInfo );
38
+ return $ this ->render ($ this ->action ->id , [
39
+ 'errorInfo ' => $ errorInfo ,
40
+ 'editForm ' => $ editForm ,
41
+ ]);
42
+ }
43
+
44
+ // 数据库migrate页面
17
45
public function actionMigrate ()
18
46
{
19
47
$ successInfo = Yii::$ app ->session ->getFlash ('database-success ' );
20
48
$ successInfo = $ this ->getSuccessHtml ($ successInfo );
21
-
22
49
$ errorInfo = Yii::$ app ->session ->getFlash ('migrate-errors ' );
23
50
$ errorInfo = $ this ->getErrorHtml ($ errorInfo );
24
51
@@ -31,17 +58,6 @@ public function actionMigrate()
31
58
]);
32
59
}
33
60
34
- public function actionComplete ()
35
- {
36
- // 执行 chmod 644 @common/config/main-local
37
-
38
- // 删除掉install.php文件
39
- return $ this ->render ($ this ->action ->id , [
40
- ]);
41
-
42
- }
43
-
44
- // 进行测试数据sql的安装和测试图片的复制
45
61
public function actionAddtestdata ()
46
62
{
47
63
$ errorInfo = Yii::$ app ->session ->getFlash ('add-test-errors ' );
@@ -55,15 +71,14 @@ public function actionAddtestdata()
55
71
]);
56
72
57
73
}
58
-
74
+ // 进行sql migrate ,产品图片的复制
59
75
public function actionAddtestdatainit ()
60
76
{
61
77
// 1. 图片的复制
62
78
$ sourcePath = dirname (Yii::getAlias ('@common ' )) . '/environments/test_data/appimage ' ;
63
79
//$sourcePath = Yii::getAlias('@fectmelani/app/appimage');
64
80
$ targetPath = Yii::getAlias ('@appimage ' );
65
81
$ this ->copyDir ($ sourcePath , $ targetPath );
66
-
67
82
// 2. sql文件的执行
68
83
$ sqlFile = dirname (Yii::getAlias ('@common ' )) . '/environments/test_data/fecshop.sql ' ;
69
84
$ sqlStr = file_get_contents ($ sqlFile );
@@ -74,56 +89,40 @@ public function actionAddtestdatainit()
74
89
$ innerTransaction ->commit ();
75
90
echo json_encode ([
76
91
'status ' => 'success ' ,
77
- ]);exit ;
92
+ ]);
78
93
94
+ exit ;
79
95
} catch (\Exception $ e ) {
80
96
$ innerTransaction ->rollBack ();
81
97
$ message = $ e ->getMessage ();
82
98
echo json_encode ([
83
99
'status ' => 'fail ' ,
84
100
'info ' => $ message ,
85
- ]);exit ;
101
+ ]);
86
102
103
+ exit ;
87
104
}
88
-
89
105
echo json_encode ([
90
- 'status ' => 'fail ' ,
91
- 'info ' => 'error ' ,
92
- ]);
106
+ 'status ' => 'fail ' ,
107
+ 'info ' => 'error ' ,
108
+ ]);
109
+
93
110
exit ;
94
111
}
95
112
96
-
97
-
98
- // 进行数据库的migrate操作
113
+ // 进行数据库的migrate操作(ajax)
99
114
public function actionMigrateprocess ()
100
115
{
101
116
$ this ->runMigrate ();
102
117
exit ;
103
118
}
104
119
105
-
106
- // 安装默认第一步页面
107
- public function actionIndex ()
120
+ // 完成页面
121
+ public function actionComplete ()
108
122
{
109
-
110
- $ editForm = Yii::$ app ->request ->post ('editForm ' );
111
-
112
- if ($ editForm && $ this ->checkDatabaseData ($ editForm )
113
- && $ this ->updateDatabase ($ editForm )) {
114
-
115
- Yii::$ app ->session ->setFlash ('database-success ' , 'mysql config set success, mysql config file path: @common/config/main-local.php ' );
116
- // 进行跳转
117
- $ homeUrl = Yii::$ app ->homeUrl ;
118
- return $ this ->redirect ($ homeUrl .'/database/config/migrate ' );
119
- }
120
- $ errorInfo = Yii::$ app ->session ->getFlash ('database-errors ' );
121
- $ errorInfo = $ this ->getErrorHtml ($ errorInfo );
122
- return $ this ->render ($ this ->action ->id , [
123
- 'errorInfo ' => $ errorInfo ,
124
- 'editForm ' => $ editForm ,
125
- ]);
123
+ return $ this ->render ($ this ->action ->id , []);
126
124
}
125
+
127
126
// 进行数据库的信息的检查,以及将数据库信息写入文件
128
127
public function updateDatabase ($ editForm )
129
128
{
@@ -156,13 +155,15 @@ public function updateDatabase($editForm)
156
155
157
156
return false ;
158
157
}
158
+ // 得到文件的内容
159
159
$ mainLocalInfo = file_get_contents ($ mainLocalFile );
160
160
//$mainLocalInfo = require($mainLocalFile);
161
+ // 进行文件替换
161
162
$ mainLocalInfo = str_replace ('{mysql_host} ' , $ host , $ mainLocalInfo );
162
163
$ mainLocalInfo = str_replace ('{mysql_database} ' , $ database , $ mainLocalInfo );
163
164
$ mainLocalInfo = str_replace ('{mysql_user} ' , $ user , $ mainLocalInfo );
164
165
$ mainLocalInfo = str_replace ('{mysql_password} ' , $ password , $ mainLocalInfo );
165
- //var_dump($mainLocalInfo);exit;
166
+ // 写入配置文件
166
167
if (@file_put_contents ($ mainLocalFile , $ mainLocalInfo ) === false ) {
167
168
$ errors = 'Unable to write the file ' .$ mainLocalFile ;
168
169
Yii::$ app ->session ->setFlash ('database-errors ' , $ errors );
@@ -218,24 +219,27 @@ public function getSuccessHtml($successInfo){
218
219
</div>
219
220
' ;
220
221
}
222
+
221
223
return '' ;
222
224
}
223
225
224
226
public function getErrorHtml ($ errorInfo ){
225
227
if ($ errorInfo ) {
226
228
return '
227
- <div class="fecshop_message">
228
- <div class="error-msg">
229
- <div> ' . $ errorInfo .'</div>
230
- </div>
231
- </div>
229
+ <div class="fecshop_message">
230
+ <div class="error-msg">
231
+ <div> ' . $ errorInfo .'</div>
232
+ </div>
233
+ </div>
232
234
' ;
233
235
}
234
236
235
237
return '' ;
236
-
237
238
}
238
-
239
+ /**
240
+ * 在yii web环境,执行console中的命令,
241
+ * 该函数,相当于执行console命令行 `./yii migrate --interactive=0 --migrationPath=@fecshop/migrations/mysqldb`
242
+ */
239
243
public function runMigrate ()
240
244
{
241
245
$ oldApp = \Yii::$ app ;
@@ -246,10 +250,10 @@ public function runMigrate()
246
250
'db ' => $ oldApp ->db ,
247
251
],
248
252
]);
249
- $ dd = \Yii::$ app ->runAction ('migrate/up ' , ['migrationPath ' => '@fecshop/migrations/mysqldb ' , 'interactive ' => false ]);
253
+ $ runResult = \Yii::$ app ->runAction ('migrate/up ' , ['migrationPath ' => '@fecshop/migrations/mysqldb ' , 'interactive ' => false ]);
250
254
\Yii::$ app = $ oldApp ;
251
255
252
- return $ dd ;
256
+ return $ runResult ;
253
257
}
254
258
255
259
/*
@@ -275,9 +279,8 @@ public function runMigrate()
275
279
}
276
280
*/
277
281
278
-
279
-
280
- public function dir_mkdir ($ path = '' , $ mode = 0777 , $ recursive = true )
282
+ // 创建文件夹,在图片文件复制的过程中使用。
283
+ public function dirMkdir ($ path = '' , $ mode = 0777 , $ recursive = true )
281
284
{
282
285
clearstatcache ();
283
286
if (!is_dir ($ path ))
@@ -289,7 +292,7 @@ public function dir_mkdir($path = '', $mode = 0777, $recursive = true)
289
292
return true ;
290
293
}
291
294
/**
292
- * 文件夹文件拷贝
295
+ * 文件夹文件拷贝(递归)
293
296
*
294
297
* @param string $sourcePath 来源文件夹
295
298
* @param string $targetPath 目的地文件夹
@@ -302,9 +305,8 @@ public function copyDir($sourcePath, $targetPath, $isForce = true)
302
305
{
303
306
return false ;
304
307
}
305
-
306
308
$ dir = opendir ($ sourcePath );
307
- $ this ->dir_mkdir ($ targetPath );
309
+ $ this ->dirMkdir ($ targetPath );
308
310
while (false !== ($ file = readdir ($ dir )))
309
311
{
310
312
if (($ file != '. ' ) && ($ file != '.. ' )) {
0 commit comments