Addons.php 7.1 KB

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