| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2013-present http://www.thinkcmf.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: 老猫 <thinkcmf@126.com>
- // +----------------------------------------------------------------------
- namespace app\admin\logic;
- use app\admin\model\AdminMenuModel;
- use app\admin\model\AuthRuleModel;
- use app\admin\model\HookPluginModel;
- use cmf\model\OptionModel;
- use app\admin\model\PluginModel;
- use app\user\logic\UserActionLogic;
- use mindplay\annotations\Annotations;
- use think\facade\Cache;
- class AppLogic
- {
- /**
- * 安装应用
- */
- public static function install($appName)
- {
- $appName = strtolower($appName);
- $class = cmf_get_app_class($appName);
- if (!class_exists($class)) {
- return '应用不存在!';
- }
- $appPath = app_path() . $appName . DIRECTORY_SEPARATOR;
- $manifestFile = $appPath . 'manifest.json';
- if (!file_exists($manifestFile)) {
- return '应用描述文件缺失!';
- }
- $manifestContent = file_get_contents($manifestFile);
- $manifest = json_decode($manifestContent, true);
- if (empty($manifest)) {
- return '应用描述文件内容格式不正确!';
- }
- $optionName = "app_manifest_" . $appName;
- $findAppSetting = OptionModel::where('option_name', "app_manifest_" . $appName)->find();
- if (!empty($findAppSetting)) {
- return '应用已安装!';
- }
- $app = new $class;
- $installSuccess = $app->install();
- if (!$installSuccess) {
- return '应用预安装失败!';
- }
- // 导入后台菜单
- MenuLogic::importMenus($appName);
- // 导入应用钩子
- HookLogic::importHooks($appName);
- // 导入应用用户行为
- UserActionLogic::importUserActions($appName);
- $optionModel = new OptionModel();
- $optionModel->save([
- 'option_name' => $optionName,
- 'option_value' => $manifest
- ]);
- Cache::clear('init_hook_apps');
- Cache::clear('admin_menus');// 删除后台菜单缓存
-
- return true;
- }
- /**
- * 应用更新
- * @param $appName
- * @return bool|string
- * @throws \Exception
- */
- public static function update($appName)
- {
- $appName = strtolower($appName);
- $class = cmf_get_app_class($appName);
- if (!class_exists($class)) {
- return '应用不存在!';
- }
- $appPath = app_path() . $appName . DIRECTORY_SEPARATOR;
- $manifestFile = $appPath . 'manifest.json';
- if (!file_exists($manifestFile)) {
- return '应用描述文件缺失!';
- }
- $manifestContent = file_get_contents($manifestFile);
- $manifest = json_decode($manifestContent, true);
- if (empty($manifest)) {
- return '应用描述文件内容格式不正确!';
- }
- $findAppSetting = OptionModel::where('option_name', "app_manifest_$appName")->find();
- if (!empty($findAppSetting)) {
- cmf_set_option("app_manifest_$appName", $manifest);
- }
- $app = new $class;
- if (method_exists($app, 'update')) {
- $updateSuccess = $app->update();
- if (!$updateSuccess) {
- return '应用预升级失败!';
- }
- }
- // 导入后台菜单
- MenuLogic::importMenus($appName);
- // 导入应用钩子
- HookLogic::importHooks($appName);
- // 导入应用用户行为
- UserActionLogic::importUserActions($appName);
- $findAppSetting->save(['option_value' => $manifest]);
- Cache::clear('init_hook_apps');
- Cache::clear('admin_menus');// 删除后台菜单缓存
- return true;
- }
- public static function getList()
- {
- $dirs = array_map('basename', glob(app_path() . '*', GLOB_ONLYDIR));
- if ($dirs === false) {
- return '应用目录不可读';
- }
- $apps = [];
- $appManifestsIntalled = OptionModel::where('option_name', 'like', "app_manifest_%")->select();
- $appsIntalled = [];
- foreach ($appManifestsIntalled as $appManifestIntalled) {
- $appsIntalled[$appManifestIntalled['option_name']] = $appManifestIntalled['option_value'];
- }
- if (empty($dirs)) return $apps;
- foreach ($dirs as $appName) {
- $appPath = app_path() . $appName . DIRECTORY_SEPARATOR;
- $manifestFile = $appPath . 'manifest.json';
- $formatWrong = false;
- if (!file_exists($manifestFile)) {
- $formatWrong = true;
- continue;
- }
- $manifestContent = file_get_contents($manifestFile);
- $manifest = json_decode($manifestContent, true);
- if (empty($manifest)) {
- $formatWrong = true;
- }
- if (!$formatWrong) {
- $appInfo = [];
- if (!empty($appsIntalled["app_manifest_{$appName}"])) {
- $appInfo = $appsIntalled["app_manifest_{$appName}"];
- $appInfo['installed'] = 1;
- $appInfo['local_verison'] = $manifest['version'];
- } else {
- $appInfo = $manifest;
- $appInfo['local_verison'] = $manifest['version'];
- }
- $apps[] = $appInfo;
- }
- }
- return $apps;
- }
- }
|