Addons.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 插件基类 插件需要继承此类
  13. // +----------------------------------------------------------------------
  14. namespace addons;
  15. use think\facade\Config;
  16. use think\facade\View;
  17. abstract class Addons
  18. {
  19. // 当前插件标识
  20. protected $name;
  21. // 插件路径
  22. public $addon_path = '';
  23. // 插件配置作用域
  24. protected $configRange = 'addonconfig';
  25. // 插件信息作用域
  26. protected $infoRange = 'addoninfo';
  27. public function __construct()
  28. {
  29. $this->name = $this->getName();
  30. // 获取当前插件目录
  31. $this->addon_path = ADDON_PATH . $this->name . DIRECTORY_SEPARATOR;
  32. // 初始化视图模型
  33. $config = ['view_path' => $this->addon_path, 'cache_path' => app()->getRuntimePath() . 'temp' . DIRECTORY_SEPARATOR];
  34. $config = array_merge(Config::get('view'), $config);
  35. $this->view = clone View::engine('Think');
  36. $this->view->config($config);
  37. // 控制器初始化
  38. if (method_exists($this, '_initialize')) {
  39. $this->_initialize();
  40. }
  41. }
  42. /**
  43. * @title 获取当前模块名
  44. * @return string
  45. */
  46. final public function getName()
  47. {
  48. $data = explode('\\', get_class($this));
  49. return strtolower(array_pop($data));
  50. }
  51. /**
  52. * 读取基础配置信息.
  53. *
  54. * @param string $name
  55. *
  56. * @return array
  57. */
  58. final public function getInfo($name = '')
  59. {
  60. if (empty($name)) {
  61. $name = $this->name;
  62. }
  63. $info = Config::get($name, $this->infoRange);
  64. if ($info) {
  65. return $info;
  66. }
  67. $info_file = $this->addon_path . 'info.ini';
  68. if (is_file($info_file)) {
  69. $info = parse_ini_file($info_file, true, INI_SCANNER_TYPED) ?: [];
  70. $info['url'] = addon_url($name);
  71. }
  72. Config::set([$name => $info], $this->infoRange);
  73. return $info ? $info : [];
  74. }
  75. /**
  76. * 获取插件的配置数组.
  77. *
  78. * @param string $name 可选模块名
  79. *
  80. * @return array
  81. */
  82. final public function getConfig($name = '')
  83. {
  84. if (empty($name)) {
  85. $name = $this->name;
  86. }
  87. $config = Config::get($name, $this->configRange);
  88. if ($config) {
  89. return $config;
  90. }
  91. $config_file = $this->addon_path . 'config.php';
  92. if (is_file($config_file)) {
  93. $temp_arr = include $config_file;
  94. foreach ($temp_arr as $key => $value) {
  95. $config[$value['name']] = $value['value'];
  96. }
  97. unset($temp_arr);
  98. }
  99. Config::set([$name => $config], $this->configRange);
  100. return $config;
  101. }
  102. /**
  103. * 获取完整配置列表.
  104. *
  105. * @param string $name
  106. *
  107. * @return array
  108. */
  109. final public function getFullConfig($name = '')
  110. {
  111. $fullConfigArr = [];
  112. if (empty($name)) {
  113. $name = $this->name;
  114. }
  115. $config_file = $this->addon_path . 'config.php';
  116. if (is_file($config_file)) {
  117. $fullConfigArr = include $config_file;
  118. }
  119. return $fullConfigArr;
  120. }
  121. /**
  122. * 设置配置数据.
  123. *
  124. * @param $name
  125. * @param array $value
  126. *
  127. * @return array
  128. */
  129. final public function setConfig($name = '', $value = [])
  130. {
  131. if (empty($name)) {
  132. $name = $this->name;
  133. }
  134. $config = $this->getConfig($name);
  135. $config = array_merge($config, $value);
  136. Config::set([$name => $config], $this->configRange);
  137. return $config;
  138. }
  139. /**
  140. * 设置插件信息数据.
  141. *
  142. * @param $name
  143. * @param array $value
  144. *
  145. * @return array
  146. */
  147. final public function setInfo($name = '', $value = [])
  148. {
  149. if (empty($name)) {
  150. $name = $this->name;
  151. }
  152. $info = $this->getInfo($name);
  153. $info = array_merge($info, $value);
  154. Config::set([$name => $info], $this->infoRange);
  155. return $info;
  156. }
  157. /**
  158. * 检查基础配置信息是否完整.
  159. *
  160. * @return bool
  161. */
  162. final public function checkInfo()
  163. {
  164. $info = $this->getInfo();
  165. $info_check_keys = ['name', 'title', 'description', 'author', 'version', 'status'];
  166. foreach ($info_check_keys as $value) {
  167. if (!array_key_exists($value, $info)) {
  168. return false;
  169. }
  170. }
  171. return true;
  172. }
  173. /**
  174. * 获取模板引擎
  175. * @access public
  176. * @param string $type 模板引擎类型
  177. * @return $this
  178. */
  179. protected function engine($engine)
  180. {
  181. $this->view->engine($engine);
  182. return $this;
  183. }
  184. /**
  185. * 模板变量赋值
  186. * @param string|array $name 模板变量
  187. * @param mixed $value 变量值
  188. * @return $this
  189. */
  190. protected function assign($name, $value = '')
  191. {
  192. $this->view->assign([$name => $value]);
  193. return $this;
  194. }
  195. /**
  196. * 解析和获取模板内容 用于输出
  197. * @param string $template 模板文件名或者内容
  198. * @param array $vars 模板变量
  199. * @return string
  200. * @throws \Exception
  201. */
  202. protected function fetch($template = '', $vars = [])
  203. {
  204. return $this->view->fetch($template, $vars);
  205. }
  206. /**
  207. * 渲染内容输出
  208. * @param string $content 内容
  209. * @param array $vars 模板变量
  210. * @return string
  211. */
  212. protected function display($content, $vars = [])
  213. {
  214. return $this->view->display($content, $vars);
  215. }
  216. //必须实现安装
  217. abstract public function install();
  218. //必须卸载插件方法
  219. abstract public function uninstall();
  220. }