Addons.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. namespace think;
  3. use think\Config;
  4. use think\View;
  5. /**
  6. * 插件基类
  7. * Class Addons
  8. * @author Byron Sampson <xiaobo.sun@qq.com>
  9. * @package think\addons
  10. */
  11. abstract class Addons
  12. {
  13. // 视图实例对象
  14. protected $view = null;
  15. // 当前错误信息
  16. protected $error;
  17. // 插件目录
  18. public $addons_path = '';
  19. // 插件配置作用域
  20. protected $configRange = 'addonconfig';
  21. // 插件信息作用域
  22. protected $infoRange = 'addoninfo';
  23. /**
  24. * 架构函数
  25. * @access public
  26. */
  27. public function __construct()
  28. {
  29. $name = $this->getName();
  30. // 获取当前插件目录
  31. $this->addons_path = ADDON_PATH . $name . DS;
  32. // 初始化视图模型
  33. $config = ['view_path' => $this->addons_path];
  34. $config = array_merge(Config::get('template'), $config);
  35. $this->view = new View($config, Config::get('view_replace_str'));
  36. // 控制器初始化
  37. if (method_exists($this, '_initialize')) {
  38. $this->_initialize();
  39. }
  40. }
  41. /**
  42. * 读取基础配置信息
  43. * @param string $name
  44. * @return array
  45. */
  46. final public function getInfo($name = '')
  47. {
  48. if (empty($name)) {
  49. $name = $this->getName();
  50. }
  51. if (Config::has($name, $this->infoRange)) {
  52. return Config::get($name, $this->infoRange);
  53. }
  54. $info_file = $this->addons_path . 'info.ini';
  55. if (is_file($info_file)) {
  56. $info = Config::parse($info_file, '', $name, $this->infoRange);
  57. $info['url'] = addon_url($name);
  58. }
  59. Config::set($name, $info, $this->infoRange);
  60. return $info;
  61. }
  62. /**
  63. * 获取插件的配置数组
  64. * @param string $name 可选模块名
  65. * @return array
  66. */
  67. final public function getConfig($name = '')
  68. {
  69. if (empty($name)) {
  70. $name = $this->getName();
  71. }
  72. if (Config::has($name, $this->configRange)) {
  73. return Config::get($name, $this->configRange);
  74. }
  75. $config = [];
  76. $config_file = $this->addons_path . 'config.php';
  77. if (is_file($config_file)) {
  78. $temp_arr = include $config_file;
  79. foreach ($temp_arr as $key => $value) {
  80. $config[$value['name']] = $value['value'];
  81. }
  82. unset($temp_arr);
  83. }
  84. Config::set($name, $config, $this->configRange);
  85. return $config;
  86. }
  87. /**
  88. * 设置配置数据
  89. * @param $name
  90. * @param array $value
  91. * @return array
  92. */
  93. final public function setConfig($name = '', $value = [])
  94. {
  95. if (empty($name)) {
  96. $name = $this->getName();
  97. }
  98. $config = $this->getConfig($name);
  99. $config = array_merge($config, $value);
  100. Config::set($name, $config, $this->configRange);
  101. return $config;
  102. }
  103. /**
  104. * 设置插件信息数据
  105. * @param $name
  106. * @param array $value
  107. * @return array
  108. */
  109. final public function setInfo($name = '', $value = [])
  110. {
  111. if (empty($name)) {
  112. $name = $this->getName();
  113. }
  114. $info = $this->getInfo($name);
  115. $info = array_merge($info, $value);
  116. Config::set($name, $info, $this->infoRange);
  117. return $info;
  118. }
  119. /**
  120. * 获取完整配置列表
  121. * @param string $name
  122. * @return array
  123. */
  124. final public function getFullConfig($name = '')
  125. {
  126. $fullConfigArr = [];
  127. if (empty($name)) {
  128. $name = $this->getName();
  129. }
  130. $config_file = $this->addons_path . 'config.php';
  131. if (is_file($config_file)) {
  132. $fullConfigArr = include $config_file;
  133. }
  134. return $fullConfigArr;
  135. }
  136. /**
  137. * 获取当前模块名
  138. * @return string
  139. */
  140. final public function getName()
  141. {
  142. $data = explode('\\', get_class($this));
  143. return strtolower(array_pop($data));
  144. }
  145. /**
  146. * 检查基础配置信息是否完整
  147. * @return bool
  148. */
  149. final public function checkInfo()
  150. {
  151. $info = $this->getInfo();
  152. $info_check_keys = ['name', 'title', 'intro', 'author', 'version', 'state'];
  153. foreach ($info_check_keys as $value) {
  154. if (!array_key_exists($value, $info)) {
  155. return false;
  156. }
  157. }
  158. return true;
  159. }
  160. /**
  161. * 加载模板和页面输出 可以返回输出内容
  162. * @access public
  163. * @param string $template 模板文件名或者内容
  164. * @param array $vars 模板输出变量
  165. * @param array $replace 替换内容
  166. * @param array $config 模板参数
  167. * @return mixed
  168. * @throws \Exception
  169. */
  170. public function fetch($template = '', $vars = [], $replace = [], $config = [])
  171. {
  172. if (!is_file($template)) {
  173. $template = '/' . $template;
  174. }
  175. // 关闭模板布局
  176. $this->view->engine->layout(false);
  177. echo $this->view->fetch($template, $vars, $replace, $config);
  178. }
  179. /**
  180. * 渲染内容输出
  181. * @access public
  182. * @param string $content 内容
  183. * @param array $vars 模板输出变量
  184. * @param array $replace 替换内容
  185. * @param array $config 模板参数
  186. * @return mixed
  187. */
  188. public function display($content, $vars = [], $replace = [], $config = [])
  189. {
  190. // 关闭模板布局
  191. $this->view->engine->layout(false);
  192. echo $this->view->display($content, $vars, $replace, $config);
  193. }
  194. /**
  195. * 渲染内容输出
  196. * @access public
  197. * @param string $content 内容
  198. * @param array $vars 模板输出变量
  199. * @return mixed
  200. */
  201. public function show($content, $vars = [])
  202. {
  203. // 关闭模板布局
  204. $this->view->engine->layout(false);
  205. echo $this->view->fetch($content, $vars, [], [], true);
  206. }
  207. /**
  208. * 模板变量赋值
  209. * @access protected
  210. * @param mixed $name 要显示的模板变量
  211. * @param mixed $value 变量的值
  212. * @return void
  213. */
  214. public function assign($name, $value = '')
  215. {
  216. $this->view->assign($name, $value);
  217. }
  218. /**
  219. * 获取当前错误信息
  220. * @return mixed
  221. */
  222. public function getError()
  223. {
  224. return $this->error;
  225. }
  226. //必须实现安装
  227. abstract public function install();
  228. //必须卸载插件方法
  229. abstract public function uninstall();
  230. }