12
12
*/
13
13
class AppController extends Controller
14
14
{
15
- private static $ dbName ;
16
- private static $ db ;
17
15
public $ writablePaths = [
18
16
'@common/runtime ' ,
19
17
'@frontend/runtime ' ,
@@ -33,33 +31,17 @@ class AppController extends Controller
33
31
];
34
32
35
33
/**
36
- * AppController constructor.
37
- * @param string $id
38
- * @param Module $module
39
- * @param array $config
34
+ * Sets given keys to .env file
40
35
*/
41
- public function __construct ( $ id , Module $ module , array $ config = [] )
36
+ public function actionSetKeys ( )
42
37
{
43
- self ::$ db = \Yii::$ app ->db ;
44
- self ::$ dbName = $ this ->getDsnAttribute (self ::$ db ->dsn , 'dbname ' );
45
- parent ::__construct ($ id , $ module , $ config );
38
+ $ this ->setKeys ($ this ->generateKeysPaths );
46
39
}
47
40
48
41
/**
49
- * Parses DNS string to find name of database
50
- * @param $name string, string to find
51
- * @param $dsn string, DNS string
52
- * @return null|string
42
+ * @throws \yii\base\InvalidRouteException
43
+ * @throws \yii\console\Exception
53
44
*/
54
- private function getDsnAttribute ($ dsn , $ name = 'dbname ' )
55
- {
56
- if (preg_match ('/ ' . $ name . '=([^;]*)/ ' , $ dsn , $ match )) {
57
- return $ match [1 ];
58
- } else {
59
- return null ;
60
- }
61
- }
62
-
63
45
public function actionSetup ()
64
46
{
65
47
$ this ->runAction ('set-writable ' , ['interactive ' => $ this ->interactive ]);
@@ -71,45 +53,63 @@ public function actionSetup()
71
53
72
54
/**
73
55
* Truncates all tables in the database.
56
+ * @throws \yii\db\Exception
74
57
*/
75
58
public function actionTruncate ()
76
59
{
77
- if ($ this ->confirm ('This will truncate all tables of current database [ ' . self ::$ dbName . ']. ' )) {
78
- self ::$ db ->createCommand ('SET FOREIGN_KEY_CHECKS=0 ' )->execute ();
79
- $ command = self ::$ db ->createCommand ("SHOW FULL TABLES WHERE TABLE_TYPE LIKE '%TABLE' " );
60
+ $ dbName = Yii::$ app ->db ->createCommand ('SELECT DATABASE() ' )->queryScalar ();
61
+ if ($ this ->confirm ('This will truncate all tables of current database [ ' . $ dbName . ']. ' )) {
62
+ Yii::$ app ->db ->createCommand ('SET FOREIGN_KEY_CHECKS=0 ' )->execute ();
63
+ $ command = Yii::$ app ->db ->createCommand ("SHOW FULL TABLES WHERE TABLE_TYPE LIKE '%TABLE' " );
80
64
$ res = $ command ->queryAll ();
81
65
foreach ($ res as $ row ) {
82
- $ rowName = 'Tables_in_ ' . self :: $ dbName ;
66
+ $ rowName = sprintf ( 'Tables_in_%s ' , $ dbName) ;
83
67
$ this ->stdout ('Truncating table ' . $ row [$ rowName ] . PHP_EOL , Console::FG_RED );
84
- self ::$ db ->createCommand ()->truncateTable ($ row [$ rowName ])->execute ();
68
+ Yii ::$ app -> db ->createCommand ()->truncateTable ($ row [$ rowName ])->execute ();
85
69
}
86
- self ::$ db ->createCommand ('SET FOREIGN_KEY_CHECKS=1 ' )->execute ();
70
+ Yii ::$ app -> db ->createCommand ('SET FOREIGN_KEY_CHECKS=1 ' )->execute ();
87
71
}
88
72
}
89
73
90
74
/**
91
75
* Drops all tables in the database.
76
+ * @throws \yii\db\Exception
92
77
*/
93
78
public function actionDrop ()
94
79
{
95
- if ($ this ->confirm ('This will drop all tables of current database [ ' . self ::$ dbName . ']. ' )) {
96
- self ::$ db ->createCommand ("SET foreign_key_checks = 0 " )->execute ();
97
- $ tables = self ::$ db ->schema ->getTableNames ();
80
+ $ dbName = Yii::$ app ->db ->createCommand ('SELECT DATABASE() ' )->queryScalar ();
81
+ if ($ this ->confirm ('This will drop all tables of current database [ ' . $ dbName . ']. ' )) {
82
+ Yii::$ app ->db ->createCommand ("SET foreign_key_checks = 0 " )->execute ();
83
+ $ tables = Yii::$ app ->db ->schema ->getTableNames ();
98
84
foreach ($ tables as $ table ) {
99
85
$ this ->stdout ('Dropping table ' . $ table . PHP_EOL , Console::FG_RED );
100
- self ::$ db ->createCommand ()->dropTable ($ table )->execute ();
86
+ Yii ::$ app -> db ->createCommand ()->dropTable ($ table )->execute ();
101
87
}
102
- self ::$ db ->createCommand ("SET foreign_key_checks = 1 " )->execute ();
88
+ Yii ::$ app -> db ->createCommand ("SET foreign_key_checks = 1 " )->execute ();
103
89
}
104
90
}
105
91
106
92
93
+ /**
94
+ * Adds write permissions
95
+ */
107
96
public function actionSetWritable ()
108
97
{
109
98
$ this ->setWritable ($ this ->writablePaths );
110
99
}
111
100
112
- public function setWritable ($ paths )
101
+ /**
102
+ * Adds execute permissions
103
+ */
104
+ public function actionSetExecutable ()
105
+ {
106
+ $ this ->setExecutable ($ this ->executablePaths );
107
+ }
108
+
109
+ /**
110
+ * @param $paths
111
+ */
112
+ private function setWritable ($ paths )
113
113
{
114
114
foreach ($ paths as $ writable ) {
115
115
$ writable = Yii::getAlias ($ writable );
@@ -118,12 +118,10 @@ public function setWritable($paths)
118
118
}
119
119
}
120
120
121
- public function actionSetExecutable ()
122
- {
123
- $ this ->setExecutable ($ this ->executablePaths );
124
- }
125
-
126
- public function setExecutable ($ paths )
121
+ /**
122
+ * @param $paths
123
+ */
124
+ private function setExecutable ($ paths )
127
125
{
128
126
foreach ($ paths as $ executable ) {
129
127
$ executable = Yii::getAlias ($ executable );
@@ -132,12 +130,10 @@ public function setExecutable($paths)
132
130
}
133
131
}
134
132
135
- public function actionSetKeys ()
136
- {
137
- $ this ->setKeys ($ this ->generateKeysPaths );
138
- }
139
-
140
- public function setKeys ($ paths )
133
+ /**
134
+ * @param $paths
135
+ */
136
+ private function setKeys ($ paths )
141
137
{
142
138
foreach ($ paths as $ file ) {
143
139
$ file = Yii::getAlias ($ file );
0 commit comments