Addons.php 7.6 KB

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