Skip to content

Commit a01db55

Browse files
committed
rework modules installation logic
1 parent d392935 commit a01db55

File tree

4 files changed

+245
-196
lines changed

4 files changed

+245
-196
lines changed

src/components/ModuleInstaller.php

Lines changed: 77 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ abstract class ModuleInstaller extends Component
2323
public $db = 'db';
2424

2525
public $runModuleMigrations = false;
26-
27-
public abstract function getModuleId();
28-
2926
public $updateConfig = true;
3027

3128
public function init()
@@ -35,25 +32,9 @@ public function init()
3532
$this->db->getSchema()->refresh();
3633
}
3734

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)
4836
{
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);
5738
}
5839

5940
public function getChange($action)
@@ -67,11 +48,6 @@ public function getChange($action)
6748
return null;
6849
}
6950

70-
public function hasChange($action)
71-
{
72-
return ($this->getChange($action) !== null);
73-
}
74-
7551
public function getChanges()
7652
{
7753
$array = [];
@@ -81,82 +57,75 @@ public function getChanges()
8157
return $array;
8258
}
8359

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+
8470
public function install()
8571
{
8672
$this->addChange('install');
8773
if ($this->updateConfig) {
8874
$this->addToConfig();
8975
$config = require(Yii::getAlias('@app/config/console.php'));
9076
$moduleId = $this->moduleId;
91-
Yii::$app->setModule($moduleId,$config['modules'][$moduleId]);
77+
Yii::$app->setModule($moduleId, $config['modules'][$moduleId]);
9278
}
9379
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]);
9581
}
9682
}
9783

98-
public function uninstall()
84+
public function addChange($action, $meta = [])
9985
{
100-
$this->addChange('uninstall');
101-
if ($this->updateConfig) {
102-
$this->removeFromConfig();
103-
}
104-
}
86+
$changes = $this->getChanges();
10587

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];
11589

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);
11991
}
12092

12193
/**
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
12496
*/
125-
public function dropTable($table)
97+
protected function writeArrayToFile($path, $var)
12698
{
127-
$this->db->createCommand()->dropTable($table)->execute();
99+
file_put_contents($path, '<?php' . PHP_EOL . 'return ' . $this->varExport($var) . ';');
128100
}
129101

130102
/**
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
135107
*/
136-
public function addColumn($table, $column, $type, $override = false)
108+
protected function varExport($var, $indent = "")
137109
{
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);
144126
}
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();
157127
}
158128

159-
160129
/**
161130
* Add module item to config
162131
*/
@@ -177,6 +146,14 @@ protected function addToConfig()
177146
$this->writeArrayToFile($this->getConfigPath(), $config);
178147
}
179148

149+
/**
150+
* @return bool|string
151+
*/
152+
protected function getConfigPath()
153+
{
154+
return \Yii::getAlias('@app/config/installed_modules.php');
155+
}
156+
180157
/**
181158
* @return array
182159
*/
@@ -185,6 +162,17 @@ protected function getConfigArray()
185162
return ['class' => str_replace('Installer', 'Module', get_called_class())];
186163
}
187164

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+
188176
/**
189177
* Remove module item from config
190178
*/
@@ -199,58 +187,13 @@ protected function removeFromConfig()
199187
$this->writeArrayToFile($this->getConfigPath(), $config);
200188
}
201189

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-
}
226190

227191
/**
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
232196
*/
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-
254197
protected function createFile($alias, $override = true)
255198
{
256199
$path = \Yii::getAlias($alias);
@@ -260,57 +203,27 @@ protected function createFile($alias, $override = true)
260203
touch($path);
261204
}
262205

206+
/**
207+
* Create directory by alias
208+
*
209+
* @param $alias
210+
* @param int $mode
211+
* @throws \yii\base\Exception
212+
*/
263213
protected function createFolder($alias, $mode = 0775)
264214
{
265215
FileHelper::createDirectory(Yii::getAlias($alias), $mode);
266216
}
267217

218+
/**
219+
* Delete file is exist
220+
* @param $alias
221+
*/
268222
protected function deleteFile($alias)
269223
{
270224
$path = \Yii::getAlias($alias);
271225
if (file_exists($path)) {
272226
@unlink($path);
273227
}
274228
}
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-
}
316229
}

src/console/ModuleController.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,6 @@ public function actionInstall($name)
2929
$this->runInstallerCommand($name, 'install', 'Module module was installed successfully.');
3030
}
3131

32-
/**
33-
* @param $name
34-
*/
35-
public function actionUpdateDb($name)
36-
{
37-
$this->runInstallerCommand($name, 'updateDb', 'Module module was updated DB successfully.');
38-
}
39-
4032
public function actionMigrate()
4133
{
4234
$changes = $this->getChanges();

0 commit comments

Comments
 (0)