5
5
use yii \base \Component ;
6
6
use yii \db \Connection ;
7
7
use yii \di \Instance ;
8
- use Composer \Installer \PackageEvent ;
9
8
10
9
/**
11
- *
10
+ * @property string $moduleId
12
11
*/
13
- class ModuleInstaller extends Component
12
+ abstract class ModuleInstaller extends Component
14
13
{
15
14
/**
16
15
* @var Connection|array|string
17
16
*/
18
17
public $ db = 'db ' ;
19
18
19
+ public abstract function getModuleId ();
20
+
21
+ public $ updateConfig = true ;
22
+
20
23
public function init ()
21
24
{
22
25
parent ::init ();
@@ -26,12 +29,16 @@ public function init()
26
29
27
30
public function install ()
28
31
{
29
- //do some stuff
32
+ if ($ this ->updateConfig ) {
33
+ $ this ->addToConfig ();
34
+ }
30
35
}
31
36
32
37
public function uninstall ()
33
38
{
34
- //do some stuff
39
+ if ($ this ->updateConfig ) {
40
+ $ this ->removeFromConfig ();
41
+ }
35
42
}
36
43
37
44
/**
@@ -63,6 +70,86 @@ public function createTable($table, $columns, $options = null)
63
70
{
64
71
$ this ->db ->createCommand ()->createTable ($ table , $ columns , $ options )->execute ();
65
72
}
73
+
74
+
75
+ /**
76
+ * Add module item to config
77
+ */
78
+ protected function addToConfig ()
79
+ {
80
+ $ path = $ this ->getConfigPath ();
81
+ $ config = require ($ path );
82
+
83
+ $ config [$ this ->moduleId ] = $ this ->getConfigArray ();
84
+
85
+ $ this ->writeConfigFile ($ config );
86
+ }
87
+
88
+ /**
89
+ * @return array
90
+ */
91
+ protected function getConfigArray ()
92
+ {
93
+ return ['class ' => str_replace ('Installer ' , 'Module ' , static ::class)];
94
+ }
95
+
96
+ /**
97
+ * Remove module item from config
98
+ */
99
+ protected function removeFromConfig ()
100
+ {
101
+ $ path = $ this ->getConfigPath ();
102
+ $ config = require ($ path );
103
+
104
+ if (isset ($ config [$ this ->moduleId ])) {
105
+ unset($ config [$ this ->moduleId ]);
106
+ }
107
+ $ this ->writeConfigFile ($ config );
108
+ }
109
+
110
+ /**
111
+ * Write config file
112
+ * @param $var
113
+ */
114
+ protected function writeConfigFile ($ var )
115
+ {
116
+ file_put_contents ($ this ->getConfigPath (), '<?php ' . PHP_EOL . 'return ' . $ this ->varExport ($ var ) . '; ' );
117
+ }
118
+
119
+ /**
120
+ * @return bool|string
121
+ */
122
+ protected function getConfigPath ()
123
+ {
124
+ return \Yii::getAlias ('@app/config/modules.php ' );
125
+ }
126
+
127
+ /**
128
+ * var_export as php 5.4
129
+ * @param $var
130
+ * @param string $indent
131
+ * @return mixed|string
132
+ */
133
+ protected function varExport ($ var , $ indent = "" )
134
+ {
135
+ switch (gettype ($ var )) {
136
+ case "string " :
137
+ return '" ' . addcslashes ($ var , "\\\$\"\r\n\t\v\f" ) . '" ' ;
138
+ case "array " :
139
+ $ indexed = array_keys ($ var ) === range (0 , count ($ var ) - 1 );
140
+ $ r = [];
141
+ foreach ($ var as $ key => $ value ) {
142
+ $ r [] = "$ indent "
143
+ . ($ indexed ? "" : $ this ->varExport ($ key ) . " => " )
144
+ . $ this ->varExport ($ value , "$ indent " );
145
+ }
146
+ return "[ \n" . implode (", \n" , $ r ) . $ indent . "\n ] " ;
147
+ case "boolean " :
148
+ return $ var ? "true " : "false " ;
149
+ default :
150
+ return var_export ($ var , true );
151
+ }
152
+ }
66
153
}
67
154
68
155
0 commit comments