|
|
@ -2,6 +2,7 @@ |
|
|
|
|
|
|
|
|
|
|
|
namespace core\services; |
|
|
|
namespace core\services; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use core\components\SearchPerformance; |
|
|
|
use core\entities\ModuleRecord; |
|
|
|
use core\entities\ModuleRecord; |
|
|
|
use core\repositories\ModuleRepository; |
|
|
|
use core\repositories\ModuleRepository; |
|
|
|
use yii\helpers\FileHelper; |
|
|
|
use yii\helpers\FileHelper; |
|
|
@ -9,13 +10,13 @@ use Yii; |
|
|
|
|
|
|
|
|
|
|
|
class ModuleService |
|
|
|
class ModuleService |
|
|
|
{ |
|
|
|
{ |
|
|
|
private $modules; |
|
|
|
private $_modules; |
|
|
|
private $permission_manager; |
|
|
|
private $_permission_manager; |
|
|
|
|
|
|
|
|
|
|
|
public function __construct(ModuleRepository $modules, PermissionManager $permission_manager) |
|
|
|
public function __construct(ModuleRepository $modules, PermissionManager $permission_manager) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$this->modules = $modules; |
|
|
|
$this->_modules = $modules; |
|
|
|
$this->permission_manager = $permission_manager; |
|
|
|
$this->_permission_manager = $permission_manager; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function create($name, $class, $type = 'common', $active = ModuleRecord::STATUS_DISABLED): ModuleRecord |
|
|
|
public function create($name, $class, $type = 'common', $active = ModuleRecord::STATUS_DISABLED): ModuleRecord |
|
|
@ -25,10 +26,16 @@ class ModuleService |
|
|
|
$module->class = $class; |
|
|
|
$module->class = $class; |
|
|
|
$module->type = $type; |
|
|
|
$module->type = $type; |
|
|
|
$module->active = $active; |
|
|
|
$module->active = $active; |
|
|
|
$this->modules->save($module); |
|
|
|
$this->_modules->save($module); |
|
|
|
|
|
|
|
|
|
|
|
return $module; |
|
|
|
return $module; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @param ModuleRecord $module |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @throws \yii\base\ErrorException |
|
|
|
|
|
|
|
*/ |
|
|
|
public function delete(ModuleRecord $module) |
|
|
|
public function delete(ModuleRecord $module) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// connect manifest |
|
|
|
// connect manifest |
|
|
@ -46,9 +53,7 @@ class ModuleService |
|
|
|
if (method_exists($obj, 'safeDown')) { |
|
|
|
if (method_exists($obj, 'safeDown')) { |
|
|
|
$obj->safeDown(); |
|
|
|
$obj->safeDown(); |
|
|
|
Yii::$app->moduleManager->removeFromMigrationTable($migration); |
|
|
|
Yii::$app->moduleManager->removeFromMigrationTable($migration); |
|
|
|
|
|
|
|
} elseif (method_exists($obj, 'down')) { |
|
|
|
} |
|
|
|
|
|
|
|
elseif (method_exists($obj, 'down')) { |
|
|
|
|
|
|
|
$obj->down(); |
|
|
|
$obj->down(); |
|
|
|
Yii::$app->moduleManager->removeFromMigrationTable($migration); |
|
|
|
Yii::$app->moduleManager->removeFromMigrationTable($migration); |
|
|
|
} |
|
|
|
} |
|
|
@ -66,13 +71,15 @@ class ModuleService |
|
|
|
FileHelper::removeDirectory($modulePath); |
|
|
|
FileHelper::removeDirectory($modulePath); |
|
|
|
} |
|
|
|
} |
|
|
|
// delete module record |
|
|
|
// delete module record |
|
|
|
$this->modules->remove($module); |
|
|
|
$this->_modules->remove($module); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function disable(ModuleRecord $module) |
|
|
|
public function disable(ModuleRecord $module) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$module->active = ModuleRecord::STATUS_DISABLED; |
|
|
|
$module->active = ModuleRecord::STATUS_DISABLED; |
|
|
|
$this->modules->save($module); |
|
|
|
$this->_modules->save($module); |
|
|
|
|
|
|
|
// regenerate global control panel search |
|
|
|
|
|
|
|
SearchPerformance::init(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function enable(ModuleRecord $module) |
|
|
|
public function enable(ModuleRecord $module) |
|
|
@ -94,9 +101,7 @@ class ModuleService |
|
|
|
if (method_exists($obj, 'safeUp')) { |
|
|
|
if (method_exists($obj, 'safeUp')) { |
|
|
|
$obj->safeUp(); |
|
|
|
$obj->safeUp(); |
|
|
|
Yii::$app->moduleManager->appendToMigrationTable($migration); |
|
|
|
Yii::$app->moduleManager->appendToMigrationTable($migration); |
|
|
|
|
|
|
|
} elseif (method_exists($obj, 'up')) { |
|
|
|
} |
|
|
|
|
|
|
|
elseif (method_exists($obj, 'up')) { |
|
|
|
|
|
|
|
$obj->up(); |
|
|
|
$obj->up(); |
|
|
|
Yii::$app->moduleManager->appendToMigrationTable($migration); |
|
|
|
Yii::$app->moduleManager->appendToMigrationTable($migration); |
|
|
|
} |
|
|
|
} |
|
|
@ -108,14 +113,16 @@ class ModuleService |
|
|
|
$this->assignPermissions($manifest['permissions']); |
|
|
|
$this->assignPermissions($manifest['permissions']); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$this->modules->save($module); |
|
|
|
$this->_modules->save($module); |
|
|
|
|
|
|
|
// regenerate global control panel search |
|
|
|
|
|
|
|
SearchPerformance::init(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private function assignPermissions(array $permissions): void |
|
|
|
private function assignPermissions(array $permissions): void |
|
|
|
{ |
|
|
|
{ |
|
|
|
foreach ($permissions as $permission => $description) { |
|
|
|
foreach ($permissions as $permission => $description) { |
|
|
|
if (!$this->permission_manager->permissionExists($permission)) { |
|
|
|
if (!$this->_permission_manager->permissionExists($permission)) { |
|
|
|
$this->permission_manager->create($permission, $description); |
|
|
|
$this->_permission_manager->create($permission, $description); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -123,8 +130,8 @@ class ModuleService |
|
|
|
private function removePermissions(array $permissions) |
|
|
|
private function removePermissions(array $permissions) |
|
|
|
{ |
|
|
|
{ |
|
|
|
foreach ($permissions as $permission => $description) { |
|
|
|
foreach ($permissions as $permission => $description) { |
|
|
|
if ($this->permission_manager->permissionExists($permission)) { |
|
|
|
if ($this->_permission_manager->permissionExists($permission)) { |
|
|
|
$this->permission_manager->delete($permission); |
|
|
|
$this->_permission_manager->delete($permission); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -133,6 +140,7 @@ class ModuleService |
|
|
|
{ |
|
|
|
{ |
|
|
|
// migration if not exists |
|
|
|
// migration if not exists |
|
|
|
$migrationPath = Yii::getAlias('@common/modules/' . $module . '/migrations'); |
|
|
|
$migrationPath = Yii::getAlias('@common/modules/' . $module . '/migrations'); |
|
|
|
|
|
|
|
|
|
|
|
return file_exists($migrationPath) ? FileHelper::findFiles($migrationPath) : []; |
|
|
|
return file_exists($migrationPath) ? FileHelper::findFiles($migrationPath) : []; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -142,6 +150,7 @@ class ModuleService |
|
|
|
$connection = Yii::$app->getDb(); |
|
|
|
$connection = Yii::$app->getDb(); |
|
|
|
$command = $connection->createCommand("SELECT * FROM migration WHERE version = '$name'"); |
|
|
|
$command = $connection->createCommand("SELECT * FROM migration WHERE version = '$name'"); |
|
|
|
$result = $command->queryAll(); |
|
|
|
$result = $command->queryAll(); |
|
|
|
|
|
|
|
|
|
|
|
return $result ? true : false; |
|
|
|
return $result ? true : false; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |