@@ -23,9 +23,6 @@ abstract class ModuleInstaller extends Component
23
23
public $ db = 'db ' ;
24
24
25
25
public $ runModuleMigrations = false ;
26
-
27
- public abstract function getModuleId ();
28
-
29
26
public $ updateConfig = true ;
30
27
31
28
public function init ()
@@ -35,25 +32,9 @@ public function init()
35
32
$ this ->db ->getSchema ()->refresh ();
36
33
}
37
34
38
- public function addChange ($ action , $ meta = [])
39
- {
40
- $ changes = $ this ->getChanges ();
41
-
42
- $ changes [] = ['module ' => $ this ->getModuleId (), 'action ' => $ action , 'meta ' => $ meta ];
43
-
44
- $ this ->writeArrayToFile ($ this ->getChangesPath (), $ changes );
45
- }
46
-
47
- public function runModuleMigrations ($ id = null , $ path = null )
35
+ public function hasChange ($ action )
48
36
{
49
- if ($ path ==null ) {
50
- if ($ id === null ){
51
- $ id = $ this ->getModuleId ();
52
- }
53
- $ module = Yii::$ app ->getModule ($ id );
54
- $ path = $ module ->getBasePath () . DIRECTORY_SEPARATOR . 'migrations ' ;
55
- }
56
- \Yii::$ app ->runAction ('migrate/up ' , ['migrationPath ' => $ path , 'interactive ' => false ]);
37
+ return ($ this ->getChange ($ action ) !== null );
57
38
}
58
39
59
40
public function getChange ($ action )
@@ -67,11 +48,6 @@ public function getChange($action)
67
48
return null ;
68
49
}
69
50
70
- public function hasChange ($ action )
71
- {
72
- return ($ this ->getChange ($ action ) !== null );
73
- }
74
-
75
51
public function getChanges ()
76
52
{
77
53
$ array = [];
@@ -81,82 +57,75 @@ public function getChanges()
81
57
return $ array ;
82
58
}
83
59
60
+ /**
61
+ * @return bool|string
62
+ */
63
+ protected function getChangesPath ()
64
+ {
65
+ return \Yii::getAlias ('@app/config/changes.php ' );
66
+ }
67
+
68
+ public abstract function getModuleId ();
69
+
84
70
public function install ()
85
71
{
86
72
$ this ->addChange ('install ' );
87
73
if ($ this ->updateConfig ) {
88
74
$ this ->addToConfig ();
89
75
$ config = require (Yii::getAlias ('@app/config/console.php ' ));
90
76
$ moduleId = $ this ->moduleId ;
91
- Yii::$ app ->setModule ($ moduleId ,$ config ['modules ' ][$ moduleId ]);
77
+ Yii::$ app ->setModule ($ moduleId , $ config ['modules ' ][$ moduleId ]);
92
78
}
93
79
if ($ this ->runModuleMigrations || \Yii::$ app ->controller ->confirm ('Run migrations ' )) {
94
- $ this ->runModuleMigrations ( );
80
+ \Yii:: $ app -> runAction ( ' modules-migrate/up ' , [ ' all ' , ' moduleId ' => $ this ->getModuleId (), ' interactive ' => false ] );
95
81
}
96
82
}
97
83
98
- public function uninstall ( )
84
+ public function addChange ( $ action , $ meta = [] )
99
85
{
100
- $ this ->addChange ('uninstall ' );
101
- if ($ this ->updateConfig ) {
102
- $ this ->removeFromConfig ();
103
- }
104
- }
86
+ $ changes = $ this ->getChanges ();
105
87
106
- /**
107
- * Check if table exist
108
- * @param $tableName
109
- * @return bool
110
- */
111
- public function tableExist ($ tableName )
112
- {
113
- return $ this ->db ->schema ->getTableSchema ($ tableName , true ) !== null ;
114
- }
88
+ $ changes [] = ['module ' => $ this ->getModuleId (), 'action ' => $ action , 'meta ' => $ meta ];
115
89
116
- public function hasColumn ($ tableName , $ columnName )
117
- {
118
- return ($ this ->tableExist ($ tableName ) && isset ($ this ->db ->schema ->getTableSchema ($ tableName , true )->columns [$ columnName ]));
90
+ $ this ->writeArrayToFile ($ this ->getChangesPath (), $ changes );
119
91
}
120
92
121
93
/**
122
- * Builds and executes a SQL statement for dropping a DB table.
123
- * @param string $table the table to be dropped. The name will be properly quoted by the method.
94
+ * Write config file
95
+ * @param $var
124
96
*/
125
- public function dropTable ( $ table )
97
+ protected function writeArrayToFile ( $ path , $ var )
126
98
{
127
- $ this ->db -> createCommand ()-> dropTable ( $ table )-> execute ( );
99
+ file_put_contents ( $ path , ' <?php ' . PHP_EOL . ' return ' . $ this ->varExport ( $ var ) . ' ; ' );
128
100
}
129
101
130
102
/**
131
- * @param $table
132
- * @param $column
133
- * @param $type
134
- * @param bool $override
103
+ * var_export as php 5.4
104
+ * @param $var
105
+ * @param string $indent
106
+ * @return mixed|string
135
107
*/
136
- public function addColumn ( $ table , $ column , $ type , $ override = false )
108
+ protected function varExport ( $ var , $ indent = "" )
137
109
{
138
- if ($ this ->hasColumn ($ table , $ column )) {
139
- if ($ override ) {
140
- $ this ->db ->createCommand ()->dropColumn ($ table , $ column )->execute ();
141
- } else {
142
- return ;
143
- }
110
+ switch (gettype ($ var )) {
111
+ case "string " :
112
+ return '" ' . addcslashes ($ var , "\\\$\"\r\n\t\v\f" ) . '" ' ;
113
+ case "array " :
114
+ $ indexed = array_keys ($ var ) === range (0 , count ($ var ) - 1 );
115
+ $ r = [];
116
+ foreach ($ var as $ key => $ value ) {
117
+ $ r [] = "$ indent "
118
+ . ($ indexed ? "" : $ this ->varExport ($ key ) . " => " )
119
+ . $ this ->varExport ($ value , "$ indent " );
120
+ }
121
+ return "[ \n" . implode (", \n" , $ r ) . $ indent . "\n ] " ;
122
+ case "boolean " :
123
+ return $ var ? "true " : "false " ;
124
+ default :
125
+ return var_export ($ var , true );
144
126
}
145
- $ this ->db ->createCommand ()->addColumn ($ table , $ column , $ type )->execute ();
146
- }
147
-
148
- /**
149
- * Create table by name, columns and options
150
- * @param $table
151
- * @param $columns
152
- * @param null $options
153
- */
154
- public function createTable ($ table , $ columns , $ options = null )
155
- {
156
- $ this ->db ->createCommand ()->createTable ($ table , $ columns , $ options )->execute ();
157
127
}
158
128
159
-
160
129
/**
161
130
* Add module item to config
162
131
*/
@@ -177,6 +146,14 @@ protected function addToConfig()
177
146
$ this ->writeArrayToFile ($ this ->getConfigPath (), $ config );
178
147
}
179
148
149
+ /**
150
+ * @return bool|string
151
+ */
152
+ protected function getConfigPath ()
153
+ {
154
+ return \Yii::getAlias ('@app/config/installed_modules.php ' );
155
+ }
156
+
180
157
/**
181
158
* @return array
182
159
*/
@@ -185,6 +162,17 @@ protected function getConfigArray()
185
162
return ['class ' => str_replace ('Installer ' , 'Module ' , get_called_class ())];
186
163
}
187
164
165
+ public function uninstall ()
166
+ {
167
+ $ this ->addChange ('uninstall ' );
168
+ if ($ this ->updateConfig ) {
169
+ $ this ->removeFromConfig ();
170
+ }
171
+ if ($ this ->runModuleMigrations || \Yii::$ app ->controller ->confirm ('Down module migrations? ' )) {
172
+ \Yii::$ app ->runAction ('modules-migrate/down ' , ['all ' , 'moduleId ' => $ this ->getModuleId (), 'interactive ' => false ]);
173
+ }
174
+ }
175
+
188
176
/**
189
177
* Remove module item from config
190
178
*/
@@ -199,58 +187,13 @@ protected function removeFromConfig()
199
187
$ this ->writeArrayToFile ($ this ->getConfigPath (), $ config );
200
188
}
201
189
202
- /**
203
- * Write config file
204
- * @param $var
205
- */
206
- protected function writeArrayToFile ($ path , $ var )
207
- {
208
- file_put_contents ($ path , '<?php ' . PHP_EOL . 'return ' . $ this ->varExport ($ var ) . '; ' );
209
- }
210
-
211
- /**
212
- * @return bool|string
213
- */
214
- protected function getConfigPath ()
215
- {
216
- return \Yii::getAlias ('@app/config/installed_modules.php ' );
217
- }
218
-
219
- /**
220
- * @return bool|string
221
- */
222
- protected function getChangesPath ()
223
- {
224
- return \Yii::getAlias ('@app/config/changes.php ' );
225
- }
226
190
227
191
/**
228
- * var_export as php 5.4
229
- * @param $var
230
- * @param string $indent
231
- * @return mixed|string
192
+ * Create file by alias
193
+ *
194
+ * @param $alias
195
+ * @param bool $override
232
196
*/
233
- protected function varExport ($ var , $ indent = "" )
234
- {
235
- switch (gettype ($ var )) {
236
- case "string " :
237
- return '" ' . addcslashes ($ var , "\\\$\"\r\n\t\v\f" ) . '" ' ;
238
- case "array " :
239
- $ indexed = array_keys ($ var ) === range (0 , count ($ var ) - 1 );
240
- $ r = [];
241
- foreach ($ var as $ key => $ value ) {
242
- $ r [] = "$ indent "
243
- . ($ indexed ? "" : $ this ->varExport ($ key ) . " => " )
244
- . $ this ->varExport ($ value , "$ indent " );
245
- }
246
- return "[ \n" . implode (", \n" , $ r ) . $ indent . "\n ] " ;
247
- case "boolean " :
248
- return $ var ? "true " : "false " ;
249
- default :
250
- return var_export ($ var , true );
251
- }
252
- }
253
-
254
197
protected function createFile ($ alias , $ override = true )
255
198
{
256
199
$ path = \Yii::getAlias ($ alias );
@@ -260,57 +203,27 @@ protected function createFile($alias, $override = true)
260
203
touch ($ path );
261
204
}
262
205
206
+ /**
207
+ * Create directory by alias
208
+ *
209
+ * @param $alias
210
+ * @param int $mode
211
+ * @throws \yii\base\Exception
212
+ */
263
213
protected function createFolder ($ alias , $ mode = 0775 )
264
214
{
265
215
FileHelper::createDirectory (Yii::getAlias ($ alias ), $ mode );
266
216
}
267
217
218
+ /**
219
+ * Delete file is exist
220
+ * @param $alias
221
+ */
268
222
protected function deleteFile ($ alias )
269
223
{
270
224
$ path = \Yii::getAlias ($ alias );
271
225
if (file_exists ($ path )) {
272
226
@unlink ($ path );
273
227
}
274
228
}
275
-
276
- /**
277
- * Add column for category relation if entity has it
278
- */
279
- public function updateDb ()
280
- {
281
- $ this ->addChange ('updateDb ' );
282
- $ module = Yii::$ app ->getModule ($ this ->moduleId );
283
- /** @var Module $module */
284
- foreach ($ module ->getComponents () as $ id => $ component ) {
285
- if (is_array ($ component )) {
286
- $ component = $ module ->get ($ id );
287
- }
288
- if ($ component instanceof EntityManager) {
289
- $ model = $ component ->createModel ();
290
- foreach ($ model ->behaviors as $ behavior ) {
291
- if ($ behavior instanceof HasOneRelation) {
292
- $ this ->addColumn ($ model ->tableName (), $ behavior ->getAttributeName (), Schema::TYPE_INTEGER );
293
- }
294
- }
295
- foreach ($ model ->behaviors as $ behavior ) {
296
- if ($ behavior instanceof HasManyRelation) {
297
- if (!$ this ->tableExist ($ behavior ->getTableName ())) {
298
- $ this ->createTable ($ behavior ->getTableName (), [
299
- $ behavior ->getToFieldName () => Schema::TYPE_INTEGER ,
300
- $ behavior ->getFromFieldName () => Schema::TYPE_INTEGER ,
301
- ]);
302
- $ this ->db ->createCommand ()
303
- ->addPrimaryKey ($ behavior ->getFromFieldName () . $ behavior ->getToFieldName (),
304
- $ behavior ->getTableName (), [
305
- $ behavior ->getToFieldName (),
306
- $ behavior ->getFromFieldName ()
307
- ])
308
- ->execute ();
309
- }
310
- }
311
- }
312
- }
313
-
314
- }
315
- }
316
229
}
0 commit comments