AppLogic.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-present http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 老猫 <thinkcmf@126.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\logic;
  12. use app\admin\model\AdminMenuModel;
  13. use app\admin\model\AuthRuleModel;
  14. use app\admin\model\HookPluginModel;
  15. use cmf\model\OptionModel;
  16. use app\admin\model\PluginModel;
  17. use app\user\logic\UserActionLogic;
  18. use mindplay\annotations\Annotations;
  19. use think\facade\Cache;
  20. class AppLogic
  21. {
  22. /**
  23. * 安装应用
  24. */
  25. public static function install($appName)
  26. {
  27. $appName = strtolower($appName);
  28. $class = cmf_get_app_class($appName);
  29. if (!class_exists($class)) {
  30. return '应用不存在!';
  31. }
  32. $appPath = app_path() . $appName . DIRECTORY_SEPARATOR;
  33. $manifestFile = $appPath . 'manifest.json';
  34. if (!file_exists($manifestFile)) {
  35. return '应用描述文件缺失!';
  36. }
  37. $manifestContent = file_get_contents($manifestFile);
  38. $manifest = json_decode($manifestContent, true);
  39. if (empty($manifest)) {
  40. return '应用描述文件内容格式不正确!';
  41. }
  42. $optionName = "app_manifest_" . $appName;
  43. $findAppSetting = OptionModel::where('option_name', "app_manifest_" . $appName)->find();
  44. if (!empty($findAppSetting)) {
  45. return '应用已安装!';
  46. }
  47. $app = new $class;
  48. $installSuccess = $app->install();
  49. if (!$installSuccess) {
  50. return '应用预安装失败!';
  51. }
  52. // 导入后台菜单
  53. MenuLogic::importMenus($appName);
  54. // 导入应用钩子
  55. HookLogic::importHooks($appName);
  56. // 导入应用用户行为
  57. UserActionLogic::importUserActions($appName);
  58. $optionModel = new OptionModel();
  59. $optionModel->save([
  60. 'option_name' => $optionName,
  61. 'option_value' => $manifest
  62. ]);
  63. Cache::clear('init_hook_apps');
  64. Cache::clear('admin_menus');// 删除后台菜单缓存
  65. return true;
  66. }
  67. /**
  68. * 应用更新
  69. * @param $appName
  70. * @return bool|string
  71. * @throws \Exception
  72. */
  73. public static function update($appName)
  74. {
  75. $appName = strtolower($appName);
  76. $class = cmf_get_app_class($appName);
  77. if (!class_exists($class)) {
  78. return '应用不存在!';
  79. }
  80. $appPath = app_path() . $appName . DIRECTORY_SEPARATOR;
  81. $manifestFile = $appPath . 'manifest.json';
  82. if (!file_exists($manifestFile)) {
  83. return '应用描述文件缺失!';
  84. }
  85. $manifestContent = file_get_contents($manifestFile);
  86. $manifest = json_decode($manifestContent, true);
  87. if (empty($manifest)) {
  88. return '应用描述文件内容格式不正确!';
  89. }
  90. $findAppSetting = OptionModel::where('option_name', "app_manifest_$appName")->find();
  91. if (!empty($findAppSetting)) {
  92. cmf_set_option("app_manifest_$appName", $manifest);
  93. }
  94. $app = new $class;
  95. if (method_exists($app, 'update')) {
  96. $updateSuccess = $app->update();
  97. if (!$updateSuccess) {
  98. return '应用预升级失败!';
  99. }
  100. }
  101. // 导入后台菜单
  102. MenuLogic::importMenus($appName);
  103. // 导入应用钩子
  104. HookLogic::importHooks($appName);
  105. // 导入应用用户行为
  106. UserActionLogic::importUserActions($appName);
  107. $findAppSetting->save(['option_value' => $manifest]);
  108. Cache::clear('init_hook_apps');
  109. Cache::clear('admin_menus');// 删除后台菜单缓存
  110. return true;
  111. }
  112. public static function getList()
  113. {
  114. $dirs = array_map('basename', glob(app_path() . '*', GLOB_ONLYDIR));
  115. if ($dirs === false) {
  116. return '应用目录不可读';
  117. }
  118. $apps = [];
  119. $appManifestsIntalled = OptionModel::where('option_name', 'like', "app_manifest_%")->select();
  120. $appsIntalled = [];
  121. foreach ($appManifestsIntalled as $appManifestIntalled) {
  122. $appsIntalled[$appManifestIntalled['option_name']] = $appManifestIntalled['option_value'];
  123. }
  124. if (empty($dirs)) return $apps;
  125. foreach ($dirs as $appName) {
  126. $appPath = app_path() . $appName . DIRECTORY_SEPARATOR;
  127. $manifestFile = $appPath . 'manifest.json';
  128. $formatWrong = false;
  129. if (!file_exists($manifestFile)) {
  130. $formatWrong = true;
  131. continue;
  132. }
  133. $manifestContent = file_get_contents($manifestFile);
  134. $manifest = json_decode($manifestContent, true);
  135. if (empty($manifest)) {
  136. $formatWrong = true;
  137. }
  138. if (!$formatWrong) {
  139. $appInfo = [];
  140. if (!empty($appsIntalled["app_manifest_{$appName}"])) {
  141. $appInfo = $appsIntalled["app_manifest_{$appName}"];
  142. $appInfo['installed'] = 1;
  143. $appInfo['local_verison'] = $manifest['version'];
  144. } else {
  145. $appInfo = $manifest;
  146. $appInfo['local_verison'] = $manifest['version'];
  147. }
  148. $apps[] = $appInfo;
  149. }
  150. }
  151. return $apps;
  152. }
  153. }