You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
3.0 KiB
103 lines
3.0 KiB
<?php |
|
/** |
|
* Created by Error202 |
|
* Date: 04.09.2018 |
|
*/ |
|
|
|
namespace console\controllers; |
|
|
|
use core\components\SearchPerformance; |
|
use core\entities\ModuleRecord; |
|
use core\services\ModuleService; |
|
use core\services\PermissionManager; |
|
use yii\console\Controller; |
|
|
|
/** |
|
* Modules management from console |
|
* Class ModuleController |
|
* @package console\controllers |
|
*/ |
|
class ModuleController extends Controller |
|
{ |
|
/** |
|
* @var ModuleService Modules management service |
|
*/ |
|
private $_service; |
|
|
|
/** |
|
* @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; |
|
} |
|
|
|
/** |
|
* First modules initialization |
|
*/ |
|
public function actionInit() : void |
|
{ |
|
\Yii::$app->moduleManager->getModules(); |
|
} |
|
|
|
/** |
|
* Activate module and apply it migration if needed |
|
* @param $name |
|
*/ |
|
public function actionActivate($name) |
|
{ |
|
$module = ModuleRecord::find()->andWhere(['name' => $name])->one(); |
|
|
|
if ($module || $module->isDisabled()) { |
|
$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); |
|
} |
|
} |
|
} |
|
}
|
|
|