Addons.php 6.9 KB

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