|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\components; |
|
4
|
|
|
|
|
5
|
|
|
use app; |
|
6
|
|
|
use app\modules\config\models\Configurable; |
|
7
|
|
|
use app\modules\config\helpers\ConfigurationUpdater; |
|
8
|
|
|
use devgroup\TagDependencyHelper\ActiveRecordHelper; |
|
9
|
|
|
use Symfony\Component\Process\Process; |
|
10
|
|
|
use Yii; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class ExtensionModule is the base module that must use all DotPlant2 extensions |
|
14
|
|
|
* |
|
15
|
|
|
* @package app\components |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class ExtensionModule extends \yii\base\Module |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var string Path to migrations folder inside extension root source directory |
|
21
|
|
|
*/ |
|
22
|
|
|
public $migrationsFolder = 'migrations'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* ID of module for application config. |
|
26
|
|
|
* You must override it in your extension. |
|
27
|
|
|
*/ |
|
28
|
|
|
public static $moduleId = 'temporary-module'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Installs module |
|
32
|
|
|
* @param bool $applyAppMigrations |
|
33
|
|
|
* @param bool $updateComposer |
|
34
|
|
|
* @return mixed |
|
|
|
|
|
|
35
|
|
|
*/ |
|
36
|
|
|
public static function installModule($applyAppMigrations = true, $updateComposer = true) |
|
37
|
|
|
{ |
|
38
|
|
|
Yii::$app->setModule( |
|
39
|
|
|
static::$moduleId, |
|
40
|
|
|
static::className() |
|
|
|
|
|
|
41
|
|
|
); |
|
42
|
|
|
/** @var ExtensionModule $module */ |
|
43
|
|
|
$module = Yii::$app->getModule(static::$moduleId); |
|
44
|
|
|
return $module->updateModule($applyAppMigrations, $updateComposer, true); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Updates/installs module |
|
49
|
|
|
* |
|
50
|
|
|
* @param bool $applyAppMigrations True if we need to apply app migrations |
|
51
|
|
|
* @param bool $updateComposer True if we need to update composer |
|
52
|
|
|
*/ |
|
53
|
|
|
public function updateModule($applyAppMigrations = true, $updateComposer = true, $updateConfig = true) |
|
54
|
|
|
{ |
|
55
|
|
|
// apply migrations |
|
56
|
|
|
$migrationPath = $this->getMigrationsPath(); |
|
57
|
|
|
|
|
58
|
|
|
/** @var Process $process */ |
|
59
|
|
|
$process = Yii::$app->updateHelper |
|
60
|
|
|
->applyMigrations( |
|
61
|
|
|
$migrationPath, |
|
62
|
|
|
$this->getMigrationTable(), |
|
63
|
|
|
$applyAppMigrations, |
|
64
|
|
|
$updateComposer |
|
65
|
|
|
); |
|
66
|
|
|
$process->mustRun(); |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
if ($updateConfig === true) { |
|
70
|
|
|
$this->updateConfig(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $process->getExitCode() === 0; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Uninstalls module |
|
78
|
|
|
* @return bool True if ok |
|
79
|
|
|
*/ |
|
80
|
|
|
public function uninstallModule() |
|
81
|
|
|
{ |
|
82
|
|
|
|
|
83
|
|
|
$this->removeConfig(); |
|
84
|
|
|
|
|
85
|
|
|
// apply migrations |
|
86
|
|
|
$migrationPath = $this->getMigrationsPath(); |
|
87
|
|
|
|
|
88
|
|
|
/** @var Process $process */ |
|
89
|
|
|
$process = Yii::$app->updateHelper |
|
90
|
|
|
->applyMigrations( |
|
91
|
|
|
$migrationPath, |
|
92
|
|
|
$this->getMigrationTable(), |
|
93
|
|
|
false, |
|
94
|
|
|
false, |
|
95
|
|
|
true |
|
96
|
|
|
); |
|
97
|
|
|
$process->mustRun(); |
|
98
|
|
|
|
|
99
|
|
|
return $process->getExitCode() === 0; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return string Full real path to module migrations |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getMigrationsPath() |
|
106
|
|
|
{ |
|
107
|
|
|
$reflectionClass = new \ReflectionClass($this); |
|
108
|
|
|
$directory = dirname($reflectionClass->getFileName()); |
|
109
|
|
|
|
|
110
|
|
|
return realpath( |
|
111
|
|
|
"$directory/{$this->migrationsFolder}/" |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return string Returns name of migration table |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getMigrationTable() |
|
119
|
|
|
{ |
|
120
|
|
|
return 'migrations_' . md5($this->className()); |
|
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Update configurable config if it exists. |
|
125
|
|
|
* Unexistent values will be filled with default values. |
|
126
|
|
|
*/ |
|
127
|
|
|
public function updateConfig() |
|
128
|
|
|
{ |
|
129
|
|
|
if ($this->getBehavior('configurableModule') !== null) { |
|
130
|
|
|
// This extension has config behavior |
|
131
|
|
|
|
|
132
|
|
|
// Find corresponding configurable |
|
133
|
|
|
$configurable = Configurable::find() |
|
134
|
|
|
->where(['module' => $this->id]) |
|
135
|
|
|
->one(); |
|
136
|
|
|
|
|
137
|
|
|
if ($configurable !== null) { |
|
138
|
|
|
// We should save default values of it |
|
139
|
|
|
$array = [$configurable]; |
|
140
|
|
|
ConfigurationUpdater::updateConfiguration( |
|
141
|
|
|
$array, |
|
|
|
|
|
|
142
|
|
|
false |
|
143
|
|
|
); |
|
144
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
\yii\caching\TagDependency::invalidate( |
|
150
|
|
|
Yii::$app->cache, |
|
151
|
|
|
[ |
|
152
|
|
|
ActiveRecordHelper::getCommonTag(Configurable::className()), |
|
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
] |
|
155
|
|
|
); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Remove module configuration from config files. |
|
160
|
|
|
*/ |
|
161
|
|
|
public function removeConfig() |
|
162
|
|
|
{ |
|
163
|
|
|
if ($this->getBehavior('configurableModule') !== null) { |
|
164
|
|
|
// This extension has config behavior |
|
165
|
|
|
|
|
166
|
|
|
// Find corresponding configurable |
|
167
|
|
|
$configurables = Configurable::find() |
|
168
|
|
|
->where('module!=:module', [':module' => $this->id]) |
|
169
|
|
|
->all(); |
|
170
|
|
|
|
|
171
|
|
|
if ($configurables !== null) { |
|
172
|
|
|
// The trick is to use previous saved values from other configurables |
|
173
|
|
|
// and exclude current module from configuration |
|
174
|
|
|
// that is done by setting not to load existing configuration |
|
175
|
|
|
|
|
176
|
|
|
ConfigurationUpdater::updateConfiguration( |
|
177
|
|
|
$configurables, |
|
178
|
|
|
false, |
|
179
|
|
|
false |
|
180
|
|
|
); |
|
181
|
|
|
|
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
} |
|
186
|
|
|
\yii\caching\TagDependency::invalidate( |
|
187
|
|
|
Yii::$app->cache, |
|
188
|
|
|
[ |
|
189
|
|
|
ActiveRecordHelper::getCommonTag(Configurable::className()), |
|
|
|
|
|
|
190
|
|
|
|
|
191
|
|
|
] |
|
192
|
|
|
); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
|
|
196
|
|
|
} |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.