Skip to content

Commit f1a3b7e

Browse files
committed
add reinstall module action
1 parent 138c4ec commit f1a3b7e

File tree

1 file changed

+50
-13
lines changed

1 file changed

+50
-13
lines changed

src/console/ModuleController.php

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,8 @@ public function actionIndex()
2121

2222
public function actionInstall($name)
2323
{
24-
$namespace = 'nullref/yii2-' . $name;
25-
$installerClassName = '\\nullref\\' . $name . '\\Installer';
26-
if (isset(\Yii::$app->extensions[$namespace])) {
27-
if (class_exists($installerClassName)) {
28-
/** @var ModuleInstaller $installer */
29-
$installer = \Yii::createObject($installerClassName, ['db' => $this->db]);
24+
if ($this->moduleExists($name)) {
25+
if (($installer = $this->getInstaller($name)) !== null) {
3026
$installer->install();
3127
echo 'Module module was installed successfully.' . PHP_EOL;
3228
} else {
@@ -38,22 +34,63 @@ public function actionInstall($name)
3834
}
3935

4036

37+
/**
38+
* @param $name
39+
*/
4140
public function actionUninstall($name)
4241
{
42+
if ($this->moduleExists($name)) {
43+
if (($installer = $this->getInstaller($name)) !== null) {
44+
$installer->uninstall();
45+
echo 'Module module was uninstalled successfully.' . PHP_EOL;
46+
} else {
47+
echo 'Module installer don\'t found.' . PHP_EOL;
48+
}
49+
} else {
50+
echo 'Module don\'t found.' . PHP_EOL;
51+
}
52+
}
4353

44-
$namespace = 'nullref/yii2-' . $name;
45-
$installerClassName = '\\nullref\\' . $name . '\\Installer';
46-
if (isset(\Yii::$app->extensions[$namespace])) {
47-
if (class_exists($installerClassName)) {
48-
/** @var ModuleInstaller $installer */
49-
$installer = \Yii::createObject($installerClassName, ['db' => $this->db]);
54+
/**
55+
* @param $name
56+
*/
57+
public function actionReinstall($name)
58+
{
59+
if ($this->moduleExists($name)) {
60+
if (($installer = $this->getInstaller($name)) !== null) {
5061
$installer->uninstall();
51-
echo 'Module module was installed successfully.' . PHP_EOL;
62+
$installer->install();
63+
echo 'Module module was reinstalled successfully.' . PHP_EOL;
5264
} else {
5365
echo 'Module installer don\'t found.' . PHP_EOL;
5466
}
5567
} else {
5668
echo 'Module don\'t found.' . PHP_EOL;
5769
}
5870
}
71+
72+
/**
73+
* @param $name
74+
* @return null|ModuleInstaller
75+
* @throws \yii\base\InvalidConfigException
76+
*/
77+
protected function getInstaller($name)
78+
{
79+
$installerClassName = '\\nullref\\' . $name . '\\Installer';
80+
if (class_exists($installerClassName)) {
81+
return \Yii::createObject($installerClassName, ['db' => $this->db]);
82+
} else {
83+
return null;
84+
}
85+
}
86+
87+
/**
88+
* @param $name
89+
* @return bool
90+
*/
91+
protected function moduleExists($name)
92+
{
93+
$namespace = 'nullref/yii2-' . $name;
94+
return isset(\Yii::$app->extensions[$namespace]);
95+
}
5996
}

0 commit comments

Comments
 (0)