Browse Source

Backend ModuleController remove install action

ModuleService remove migration check from from enable
master
Egorka 6 years ago
parent
commit
78fd2976eb
  1. 78
      backend/controllers/ModuleController.php
  2. 51
      core/services/ModuleService.php

78
backend/controllers/ModuleController.php

@ -39,7 +39,7 @@ class ModuleController extends Controller
'class' => AccessControl::class, 'class' => AccessControl::class,
'rules' => [ 'rules' => [
[ [
'actions' => ['list', 'disable', 'enable', 'delete', 'search', 'install'], 'actions' => ['list', 'disable', 'enable', 'delete', 'search'],
'allow' => true, 'allow' => true,
'roles' => ['ModuleManagement'], 'roles' => ['ModuleManagement'],
], ],
@ -98,84 +98,8 @@ class ModuleController extends Controller
return $this->redirect(['module/list']); return $this->redirect(['module/list']);
} }
public function actionInstall($name)
{
$needed_manifest = [];
// check installed extension
if (!extension_loaded('zip')) {
\Yii::$app->session->setFlash('danger', \Yii::t('main', 'PHP extension "zip" not installed, Need ZipArchive class'));
return $this->redirect(['search']);
}
try {
$list = file_get_contents('http://zertex.ru/zxcms/modules.txt');
$links = array_filter(explode('|', $list));
$modules = [];
foreach ($links as $link) {
$manifest = file_get_contents($link);
$manifest = eval(str_replace('<?php', '', $manifest));
if ($manifest['name'] == $name) {
$needed_manifest = $manifest;
}
}
} catch (\Exception $e) {
\Yii::$app->session->setFlash('danger', \Yii::t('main', 'Service not available'));
return $this->redirect(['search']);
}
if (empty($needed_manifest) && !isset($needed_manifest['git_path']) && !isset($needed_manifest['git_nameh'])) {
\Yii::$app->session->setFlash('danger', \Yii::t('main', 'Module cannot be installed'));
return $this->redirect(['search']);
}
// get git link, download
$download_link = $needed_manifest['git_path'] . '/-/archive/master/' . $needed_manifest['git_name'] . '-master.zip';
if (FileHelper::downloadFile($download_link, \Yii::getAlias('@runtime/module.zip'))) {
// unzip
$zip = new \ZipArchive;
$res = $zip->open(\Yii::getAlias('@runtime/module.zip'));
if ($res === true) {
mkdir(\Yii::getAlias('@runtime/_module'));
$zip->extractTo(\Yii::getAlias('@runtime/_module/'));
$zip->close();
$module_path = \Yii::getAlias('@common/modules/') . $needed_manifest['name'];
if (!file_exists($module_path)) {
mkdir($module_path);
}
FileHelper::copyDirectory(\Yii::getAlias('@runtime/_module/' . $needed_manifest['git_name'] . '-master'), $module_path);
} else {
\Yii::$app->session->setFlash('danger', \Yii::t('main', 'Error module archive'));
return $this->redirect(['search']);
}
} else {
\Yii::$app->session->setFlash('danger', \Yii::t('main', 'Error while install module'));
return $this->redirect(['search']);
}
// remove zip
$this->cleanInstallationFiles();
\Yii::$app->session->setFlash('success', \Yii::t('main', 'Module installed successful'));
return $this->redirect(['search']);
}
private function cleanInstallationFiles()
{
if (file_exists(\Yii::getAlias('@runtime/_module'))) {
FileHelper::removeDirectory(\Yii::getAlias('@runtime/_module'));
}
if (file_exists(\Yii::getAlias('@runtime/module.zip'))) {
unlink(\Yii::getAlias('@runtime/module.zip'));
}
}
public function actionSearch() public function actionSearch()
{ {
//print_r(file_get_contents('https://gitlab.com/zertex/zxcms-blog.git')); die;
//https://gitlab.com/zertex/zxcms-blog/-/archive/master/zxcms-blog-master.zip
$message = null; $message = null;
try { try {
$list = file_get_contents('http://zertex.ru/zxcms/modules.txt'); $list = file_get_contents('http://zertex.ru/zxcms/modules.txt');

51
core/services/ModuleService.php

@ -84,63 +84,12 @@ class ModuleService
public function enable(ModuleRecord $module) public function enable(ModuleRecord $module)
{ {
// connect manifest
$manifest = require Yii::getAlias('@common/modules/' . $module->name . '/manifest.php');
$module->active = ModuleRecord::STATUS_ENABLED; $module->active = ModuleRecord::STATUS_ENABLED;
// migration if not exists
$migrations = $this->getMigrationFiles($module->name);
foreach ($migrations as $migrationPath) {
$migrationFile = basename($migrationPath);
$migration = str_replace('.php', '', $migrationFile);
if (!$this->migrationExists($migration)) {
$addMigrationRecord = false;
// run migration
require $migrationPath;
$obj = new $migration;
if (method_exists($obj, 'safeUp')) {
$obj->safeUp();
$addMigrationRecord = true;
}
if (method_exists($obj, 'up')) {
$obj->up();
$addMigrationRecord = true;
}
if ($addMigrationRecord) {
Yii::$app->moduleManager->appendToMigrationTable($migration);
}
}
}
// add not exists permissions
if (isset($manifest['permissions']) && is_array($manifest['permissions'])) {
$this->assignPermissions($manifest['permissions']);
}
$this->_modules->save($module); $this->_modules->save($module);
// regenerate global control panel search // regenerate global control panel search
SearchPerformance::init(); 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);
}
}
}
private function removePermissions(array $permissions)
{
foreach ($permissions as $permission => $description) {
if ($this->_permission_manager->permissionExists($permission)) {
$this->_permission_manager->delete($permission);
}
}
}
private function getMigrationFiles($module) private function getMigrationFiles($module)
{ {
// migration if not exists // migration if not exists

Loading…
Cancel
Save