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