ModuleStoreController.php 18.2 KB
Newer Older
S
develop  
server 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php


namespace Module\ModuleStore\Admin\Controller;


use Illuminate\Routing\Controller;
use ModStart\Admin\Auth\AdminPermission;
use ModStart\Admin\Layout\AdminConfigBuilder;
use ModStart\Core\Exception\BizException;
use ModStart\Core\Input\InputPackage;
use ModStart\Core\Input\Response;
use ModStart\Core\Util\CRUDUtil;
use ModStart\Core\Util\FileUtil;
ModStart's avatar
develop  
ModStart 已提交
15
use ModStart\Core\Util\ReUtil;
S
develop  
server 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
use ModStart\Form\Form;
use ModStart\Module\ModuleManager;
use ModStart\Repository\RepositoryUtil;
use Module\ModuleStore\Util\ModuleStoreUtil;

class ModuleStoreController extends Controller
{
    public function __construct()
    {
        AdminPermission::permitCheck('ModuleStoreManage');
    }

    public function index()
    {
        return view('module::ModuleStore.View.admin.moduleStore.index');
    }

    public function all()
    {
        return Response::generateSuccessData(ModuleStoreUtil::all());
    }

ModStart's avatar
develop  
ModStart 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    private function moduleOperateCheck($module)
    {
        BizException::throwsIf('当前环境禁止「模块管理」相关操作', config('env.MS_MODULE_STORE_DISABLE', false));
        $whitelist = config('env.MS_MODULE_WHITELIST', '');
        if (empty($whitelist)) {
            return;
        }
        $whitelist = array_map(function ($v) {
            return trim($v);
        }, explode(',', $whitelist));
        $whitelist = array_filter($whitelist);
        if (empty($whitelist)) {
            return;
        }
        $passed = false;
        foreach ($whitelist as $item) {
            if (ReUtil::isWildMatch($item, $module)) {
                $passed = true;
                break;
            }
        }
        BizException::throwsIf('只允许操作模块:' . join(',', $whitelist), !$passed);
    }

S
develop  
server 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    private function doFinish($msgs)
    {
        return Response::generateSuccessData([
            'msg' => array_map(function ($item) {
                return '<i class="iconfont icon-hr"></i> ' . $item;
            }, $msgs),
            'finish' => true,
        ]);
    }

    private function doNext($command, $step, $msgs = [], $data = [])
    {
        $input = InputPackage::buildFromInput();
        $data = array_merge($input->getJsonAsInput('data')->all(), $data);
        return Response::generateSuccessData([
            'msg' => array_map(function ($item) {
ModStart's avatar
develop  
ModStart 已提交
78 79 80
                if (!starts_with($item, '<')) {
                    $item = "<span class='ub-text-default'>$item</span>";
                }
S
develop  
server 已提交
81 82 83 84 85 86 87 88 89 90 91
                return '<i class="iconfont icon-hr"></i> ' . $item;
            }, $msgs),
            'command' => $command,
            'step' => $step,
            'data' => $data,
            'finish' => false,
        ]);
    }

    public function disable()
    {
S
develop  
server 已提交
92
        AdminPermission::demoCheck();
S
develop  
server 已提交
93 94 95 96 97 98 99
        $input = InputPackage::buildFromInput();
        $step = $input->getTrimString('step');
        $dataInput = $input->getJsonAsInput('data');
        $module = $dataInput->getTrimString('module');
        $version = $dataInput->getTrimString('version');
        BizException::throwsIfEmpty('module为空', $module);
        BizException::throwsIfEmpty('version为空', $version);
ModStart's avatar
develop  
ModStart 已提交
100
        $this->moduleOperateCheck($module);
S
develop  
server 已提交
101 102 103 104 105 106 107 108 109 110 111 112
        switch ($step) {
            default:
                $ret = ModuleManager::disable($module);
                BizException::throwsIfResponseError($ret);
                return $this->doFinish([
                    '<span class="ub-text-success">禁用成功,请 <a href="javascript:;" onclick="window.location.reload()">刷新后台</a> 查看最新系统</span>',
                ]);
        }
    }

    public function enable()
    {
S
develop  
server 已提交
113
        AdminPermission::demoCheck();
S
develop  
server 已提交
114 115 116 117 118 119 120
        $input = InputPackage::buildFromInput();
        $step = $input->getTrimString('step');
        $dataInput = $input->getJsonAsInput('data');
        $module = $dataInput->getTrimString('module');
        $version = $dataInput->getTrimString('version');
        BizException::throwsIfEmpty('module为空', $module);
        BizException::throwsIfEmpty('version为空', $version);
ModStart's avatar
develop  
ModStart 已提交
121
        $this->moduleOperateCheck($module);
S
develop  
server 已提交
122 123 124 125 126 127 128 129 130 131 132 133
        switch ($step) {
            default:
                $ret = ModuleManager::enable($module);
                BizException::throwsIfResponseError($ret);
                return $this->doFinish([
                    '<span class="ub-text-success">启动成功,请 <a href="javascript:;" onclick="window.location.reload()">刷新后台</a> 查看最新系统</span>',
                ]);
        }
    }

    public function uninstall()
    {
S
develop  
server 已提交
134
        AdminPermission::demoCheck();
S
develop  
server 已提交
135 136 137 138 139 140 141 142 143
        $input = InputPackage::buildFromInput();
        $step = $input->getTrimString('step');
        $dataInput = $input->getJsonAsInput('data');
        $module = $dataInput->getTrimString('module');
        $version = $dataInput->getTrimString('version');
        $isLocal = $dataInput->getBoolean('isLocal');
        BizException::throwsIfEmpty('module为空', $module);
        BizException::throwsIfEmpty('version为空', $version);
        BizException::throwsIf('系统模块不能动态配置', ModuleManager::isSystemModule($module));
ModStart's avatar
develop  
ModStart 已提交
144
        $this->moduleOperateCheck($module);
S
develop  
server 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
        if ($isLocal) {
            switch ($step) {
                default:
                    $ret = ModuleManager::uninstall($module);
                    BizException::throwsIfResponseError($ret);
                    return $this->doFinish([
                        '<span class="ub-text-success">卸载完成,请 <a href="javascript:;" onclick="window.location.reload()">刷新后台</a> 查看最新系统</span>',
                    ]);
            }
        } else {
            switch ($step) {
                case 'removePackage':
                    $ret = ModuleStoreUtil::removeModule($module, $version);
                    BizException::throwsIfResponseError($ret);
                    return $this->doFinish([
                        '<span class="ub-text-success">卸载完成,请 <a href="javascript:;" onclick="window.location.reload()">刷新后台</a> 查看最新系统</span>',
                    ]);
                default:
                    $ret = ModuleManager::uninstall($module);
                    BizException::throwsIfResponseError($ret);
                    return $this->doNext('uninstall', 'removePackage', [
                        '<span class="ub-text-success">开始卸载 ' . $module . ' V' . $version . '</span>',
                    ]);

            }
        }
    }

    public function upgrade()
    {
S
develop  
server 已提交
175
        AdminPermission::demoCheck();
S
develop  
server 已提交
176 177 178 179 180 181 182 183 184
        $input = InputPackage::buildFromInput();
        $token = $input->getTrimString('token');
        $step = $input->getTrimString('step');
        BizException::throwsIfEmpty('请先登录ModStartCMS账号', $token);
        $dataInput = $input->getJsonAsInput('data');
        $module = $dataInput->getTrimString('module');
        $version = $dataInput->getTrimString('version');
        BizException::throwsIfEmpty('module为空', $module);
        BizException::throwsIfEmpty('version为空', $version);
ModStart's avatar
develop  
ModStart 已提交
185
        $this->moduleOperateCheck($module);
S
develop  
server 已提交
186 187

        switch ($step) {
S
develop  
server 已提交
188
            case 'installModule':
S
develop  
server 已提交
189
                $ret = ModuleManager::install($module, true);
S
develop  
server 已提交
190 191 192 193 194 195 196 197 198 199 200
                BizException::throwsIfResponseError($ret);
                $ret = ModuleManager::enable($module);
                BizException::throwsIfResponseError($ret);
                return $this->doFinish([
                    '<span class="ub-text-success">升级安装完成,请 <a href="javascript:;" onclick="window.location.reload()">刷新后台</a> 查看最新系统</span>',
                ]);
            case 'unpackPackage':
                $package = $dataInput->getTrimString('package');
                BizException::throwsIfEmpty('package为空', $package);
                $licenseKey = $dataInput->getTrimString('licenseKey');
                BizException::throwsIfEmpty('licenseKey为空', $licenseKey);
ModStart's avatar
develop  
ModStart 已提交
201 202 203 204 205 206
                try {
                    $ret = ModuleStoreUtil::unpackModule($module, $package, $licenseKey);
                } catch (\Exception $e) {
                    ModuleStoreUtil::cleanDownloadedPackage($package);
                    throw $e;
                }
S
develop  
server 已提交
207
                BizException::throwsIfResponseError($ret);
ModStart's avatar
develop  
ModStart 已提交
208
                return $this->doNext('upgrade', 'installModule', array_merge([
S
develop  
server 已提交
209 210
                    '<span class="ub-text-success">模块解压完成</span>',
                    '<span class="ub-text-default">开始安装...</span>',
ModStart's avatar
develop  
ModStart 已提交
211
                ], $ret['data']));
S
develop  
server 已提交
212 213 214
            case 'downloadPackage':
                $ret = ModuleStoreUtil::downloadPackage($token, $module, $version);
                BizException::throwsIfResponseError($ret);
S
develop  
server 已提交
215
                return $this->doNext('upgrade', 'unpackPackage', [
S
develop  
server 已提交
216 217 218 219 220 221
                    '<span class="ub-text-success">获取安装包完成,大小 ' . FileUtil::formatByte($ret['data']['packageSize']) . '</span>',
                    '<span class="ub-text-default">开始解压安装包...</span>'
                ], [
                    'package' => $ret['data']['package'],
                    'licenseKey' => $ret['data']['licenseKey'],
                ]);
S
develop  
server 已提交
222 223 224
            case 'checkPackage':
                $ret = ModuleStoreUtil::checkPackage($token, $module, $version);
                BizException::throwsIfResponseError($ret);
ModStart's avatar
develop  
ModStart 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
                $msgs = [];
                foreach ($ret['data']['requires'] as $require) {
                    $msgs[] = '<span>&nbsp;&nbsp;</span>'
                        . ($require['success']
                            ? '<span class="ub-text-success"><i class="iconfont icon-check"></i> 成功</span>'
                            : '<span class="ub-text-danger"><i class="iconfont icon-warning"></i> 失败</span>')
                        . " <span>$require[name]</span> " . ($require['resolve'] ? " <span>解决:$require[resolve]</span>" : "");
                }
                if ($ret['data']['errorCount'] > 0) {
                    return $this->doFinish(array_merge($msgs, [
                        '<span class="ub-text-danger">预检失败,' . $ret['data']['errorCount'] . '个依赖不满足要求</span>',
                    ]));
                }
                $msgs[] = '<span class="ub-text-default">开始下载安装包...</span>';
                return $this->doNext('upgrade', 'downloadPackage', array_merge([
                    '<span class="ub-text-success">预检成功,' . count($ret['data']['requires']) . '个依赖满足要求,安装包大小 ' . FileUtil::formatByte($ret['data']['packageSize']) . '</span>',
                ], $msgs));
S
develop  
server 已提交
242 243
            default:
                return $this->doNext('upgrade', 'checkPackage', [
S
develop  
server 已提交
244
                    '<span class="ub-text-success">开始升级到远程模块 ' . $module . ' V' . $version . '</span>',
S
develop  
server 已提交
245
                    '<span class="ub-text-default">开始模块安装预检...</span>'
S
develop  
server 已提交
246 247 248 249 250 251
                ]);
        }
    }

    public function install()
    {
S
develop  
server 已提交
252
        AdminPermission::demoCheck();
S
develop  
server 已提交
253 254 255 256 257 258 259 260 261 262 263
        $input = InputPackage::buildFromInput();
        $token = $input->getTrimString('token');
        $step = $input->getTrimString('step');
        BizException::throwsIfEmpty('请先登录ModStartCMS账号', $token);
        $dataInput = $input->getJsonAsInput('data');
        $module = $dataInput->getTrimString('module');
        $version = $dataInput->getTrimString('version');
        $isLocal = $dataInput->getBoolean('isLocal');
        BizException::throwsIfEmpty('module为空', $module);
        BizException::throwsIfEmpty('version为空', $version);
        BizException::throwsIf('系统模块不能动态配置', ModuleManager::isSystemModule($module));
ModStart's avatar
develop  
ModStart 已提交
264
        $this->moduleOperateCheck($module);
S
develop  
server 已提交
265

S
develop  
server 已提交
266 267 268
        if ($isLocal) {
            switch ($step) {
                case 'installModule':
S
develop  
server 已提交
269
                    $ret = ModuleManager::install($module, true);
S
develop  
server 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
                    BizException::throwsIfResponseError($ret);
                    $ret = ModuleManager::enable($module);
                    BizException::throwsIfResponseError($ret);
                    return $this->doFinish([
                        '<span class="ub-text-success">安装完成,请 <a href="javascript:;" onclick="window.location.reload()">刷新后台</a> 查看最新系统</span>',
                    ]);
                default:
                    return $this->doNext('install', 'installModule', [
                        '<span class="ub-text-success">开始安装本地模块 ' . $module . ' V' . $version . '</span>',
                        '<span class="ub-text-default">开始安装..</span>'
                    ]);
            }
        } else {
            switch ($step) {
                case 'installModule':
S
develop  
server 已提交
285 286 287 288 289
                    $ret = ModuleManager::install($module, true);
                    if (Response::isError($ret)) {
                        ModuleManager::clean($module);
                        BizException::throws($ret['msg']);
                    }
S
develop  
server 已提交
290 291 292 293 294 295 296 297 298 299
                    $ret = ModuleManager::enable($module);
                    BizException::throwsIfResponseError($ret);
                    return $this->doFinish([
                        '<span class="ub-text-success">安装完成,请 <a href="javascript:;" onclick="window.location.reload()">刷新后台</a> 查看最新系统</span>',
                    ]);
                case 'unpackPackage':
                    $package = $dataInput->getTrimString('package');
                    BizException::throwsIfEmpty('package为空', $package);
                    $licenseKey = $dataInput->getTrimString('licenseKey');
                    BizException::throwsIfEmpty('licenseKey为空', $licenseKey);
ModStart's avatar
develop  
ModStart 已提交
300 301 302 303 304 305
                    try {
                        $ret = ModuleStoreUtil::unpackModule($module, $package, $licenseKey);
                    } catch (\Exception $e) {
                        ModuleStoreUtil::cleanDownloadedPackage($package);
                        throw $e;
                    }
S
develop  
server 已提交
306
                    BizException::throwsIfResponseError($ret);
ModStart's avatar
develop  
ModStart 已提交
307
                    return $this->doNext('install', 'installModule', array_merge([
S
develop  
server 已提交
308 309
                        '<span class="ub-text-success">模块解压完成</span>',
                        '<span class="ub-text-default">开始安装...</span>',
ModStart's avatar
develop  
ModStart 已提交
310
                    ], $ret['data']));
S
develop  
server 已提交
311 312 313 314 315 316 317 318 319 320
                case 'downloadPackage':
                    $ret = ModuleStoreUtil::downloadPackage($token, $module, $version);
                    BizException::throwsIfResponseError($ret);
                    return $this->doNext('install', 'unpackPackage', [
                        '<span class="ub-text-success">获取安装包完成,大小 ' . FileUtil::formatByte($ret['data']['packageSize']) . '</span>',
                        '<span class="ub-text-default">开始解压安装包...</span>'
                    ], [
                        'package' => $ret['data']['package'],
                        'licenseKey' => $ret['data']['licenseKey'],
                    ]);
S
develop  
server 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
                case 'checkPackage':
                    $ret = ModuleStoreUtil::checkPackage($token, $module, $version);
                    BizException::throwsIfResponseError($ret);
                    $msgs = [];
                    foreach ($ret['data']['requires'] as $require) {
                        $msgs[] = '<span>&nbsp;&nbsp;</span>'
                            . ($require['success']
                                ? '<span class="ub-text-success"><i class="iconfont icon-check"></i> 成功</span>'
                                : '<span class="ub-text-danger"><i class="iconfont icon-warning"></i> 失败</span>')
                            . " <span>$require[name]</span> " . ($require['resolve'] ? " <span>解决:$require[resolve]</span>" : "");
                    }
                    if ($ret['data']['errorCount'] > 0) {
                        return $this->doFinish(array_merge($msgs, [
                            '<span class="ub-text-danger">预检失败,' . $ret['data']['errorCount'] . '个依赖不满足要求</span>',
                        ]));
                    }
                    $msgs[] = '<span class="ub-text-default">开始下载安装包...</span>';
                    return $this->doNext('install', 'downloadPackage', array_merge([
                        '<span class="ub-text-success">预检成功,' . count($ret['data']['requires']) . '个依赖满足要求,安装包大小 ' . FileUtil::formatByte($ret['data']['packageSize']) . '</span>',
                    ], $msgs));
                    break;
S
develop  
server 已提交
342
                default:
S
develop  
server 已提交
343
                    return $this->doNext('install', 'checkPackage', [
S
develop  
server 已提交
344
                        '<span class="ub-text-success">开始安装远程模块 ' . $module . ' V' . $version . '</span>',
S
develop  
server 已提交
345
                        '<span class="ub-text-default">开始模块安装预检...</span>'
S
develop  
server 已提交
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
                    ]);
            }
        }
    }

    public function config(AdminConfigBuilder $builder, $module)
    {
        $basic = ModuleManager::getModuleBasic($module);
        AdminPermission::demoPostCheck();
        $builder->useDialog();
        $builder->pageTitle($basic['title'] . ' ' . L('Module Config'));
        $moduleInfo = ModuleManager::getInstalledModuleInfo($module);
        BizException::throwsIfEmpty('Module config error', $basic['config']);
        foreach ($basic['config'] as $key => $callers) {
            $field = null;
            if (!isset($moduleInfo['config'][$key])) {
                $moduleInfo['config'][$key] = null;
            }
            foreach ($callers as $caller) {
                $name = array_shift($caller);
                if (null === $field) {
                    array_unshift($caller, $key);
                    $field = call_user_func([$builder, $name], ...$caller);
                } else {
                    call_user_func([$field, $name], ...$caller);
                }
            }
        }
ModStart's avatar
develop  
ModStart 已提交
374
        return $builder->perform(RepositoryUtil::itemFromArray($moduleInfo['config']), function (Form $form) use ($module, $moduleInfo) {
ModStart's avatar
develop  
ModStart 已提交
375
            AdminPermission::demoCheck();
ModStart's avatar
develop  
ModStart 已提交
376 377 378 379 380
            if ($moduleInfo['isSystem']) {
                ModuleManager::saveSystemOverwriteModuleConfig($module, $form->dataForming());
            } else {
                ModuleManager::saveUserInstalledModuleConfig($module, $form->dataForming());
            }
S
develop  
server 已提交
381 382 383 384 385
            return Response::generate(0, '保存成功', null, CRUDUtil::jsDialogClose());
        });
    }

}