diff --git a/common/modules/pages/migrations/m180827_195748_set_pages_unicode_collate.php b/common/modules/pages/migrations/m180827_195748_set_pages_unicode_collate.php index 8bdcc6f..544cb95 100644 --- a/common/modules/pages/migrations/m180827_195748_set_pages_unicode_collate.php +++ b/common/modules/pages/migrations/m180827_195748_set_pages_unicode_collate.php @@ -28,6 +28,6 @@ class m180827_195748_set_pages_unicode_collate extends Migration */ public function safeDown() { - true; + return true; } } diff --git a/console/controllers/ModuleController.php b/console/controllers/ModuleController.php index a8ff1dd..06f7599 100644 --- a/console/controllers/ModuleController.php +++ b/console/controllers/ModuleController.php @@ -6,8 +6,10 @@ namespace console\controllers; +use core\components\SearchPerformance; use core\entities\ModuleRecord; use core\services\ModuleService; +use core\services\PermissionManager; use yii\console\Controller; /** @@ -22,10 +24,16 @@ class ModuleController extends Controller */ private $_service; - public function __construct(string $id, $module, ModuleService $service, array $config = []) + /** + * @var PermissionManager Manage permissions + */ + private $_permission_manager; + + public function __construct(string $id, $module, ModuleService $service, PermissionManager $permission_manager, array $config = []) { parent::__construct($id, $module, $config); $this->_service = $service; + $this->_permission_manager = $permission_manager; } /** @@ -48,4 +56,48 @@ class ModuleController extends Controller $this->_service->enable($module); } } + + public function actionInstall($name) + { + $path = \Yii::getAlias('@common/modules/' . $name); + + // check exists + if (file_exists($path)) { + echo 'Module ' . $name . ' already exists' . PHP_EOL; + return; + } + // create folder + mkdir($path); + // git clone + chdir($path); + shell_exec('git clone https://gitlab.com/zertex/zxcms-' . $name . '.git .'); + // get module manifest + $manifest = require \Yii::getAlias('@common/modules/' . $name . '/manifest.php'); + // migration + if (file_exists($path . '/migrations')) { + shell_exec('php ' . __DIR__ . '/yii migrate --migrationPath=' . $path . '/migrations --interactive=0'); + } + // add module record with active flag + $db_module = ModuleRecord::find()->andWhere(['name' => $name])->one(); + if (!$db_module) { + $this->_service->create($name, 'common\\modules\\' . $name . '\\' . $manifest['vjlule'], 'common', 1); + } + // init permissions + if (isset($manifest['permissions']) && is_array($manifest['permissions'])) { + $this->assignPermissions($manifest['permissions']); + } + // regenerate cp global search + SearchPerformance::init(); + + echo 'Module ' . $name . ' successfully installed' . PHP_EOL; + } + + private function assignPermissions(array $permissions): void + { + foreach ($permissions as $permission => $description) { + if (!$this->_permission_manager->permissionExists($permission)) { + $this->_permission_manager->create($permission, $description); + } + } + } } diff --git a/console/migrations/m180827_194913_set_core_tables_unicode_oollate.php b/console/migrations/m180827_194913_set_core_tables_unicode_oollate.php index 52e74a4..9cefcdc 100644 --- a/console/migrations/m180827_194913_set_core_tables_unicode_oollate.php +++ b/console/migrations/m180827_194913_set_core_tables_unicode_oollate.php @@ -28,6 +28,6 @@ class m180827_194913_set_core_tables_unicode_oollate extends Migration */ public function safeDown() { - true; + return true; } }