Plugin.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 海豚PHP框架 [ DolphinPHP ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://dolphinphp.com
  8. // +----------------------------------------------------------------------
  9. namespace app\common\controller;
  10. use think\Container;
  11. use think\Exception;
  12. /**
  13. * 插件类
  14. * @package app\common\controller
  15. * @author 蔡伟明 <314013107@qq.com>
  16. */
  17. abstract class Plugin
  18. {
  19. /**
  20. * @var null 视图实例对象
  21. */
  22. protected $view = null;
  23. /**
  24. * @var string 插件配置文件
  25. */
  26. public $config_file = '';
  27. /**
  28. * @var string 插件路径
  29. */
  30. public $plugin_path = '';
  31. /**
  32. * @var string 错误信息
  33. */
  34. protected $error = '';
  35. /**
  36. * 构造方法
  37. */
  38. public function __construct()
  39. {
  40. $this->view = Container::get('view');
  41. $this->plugin_path = config('plugin_path').$this->getName().'/';
  42. if (is_file($this->plugin_path.'config.php')) {
  43. $this->config_file = $this->plugin_path.'config.php';
  44. }
  45. if (is_file($this->plugin_path.'common.php')) {
  46. include $this->plugin_path.'common.php';
  47. }
  48. }
  49. /**
  50. * 获取插件名称
  51. * @author 蔡伟明 <314013107@qq.com>
  52. * @return string
  53. */
  54. final public function getName()
  55. {
  56. $class = get_class($this);
  57. return substr($class, strrpos($class, '\\') + 1);
  58. }
  59. /**
  60. * 显示方法
  61. * @param string $template 模板或直接解析内容
  62. * @param array $vars 模板输出变量
  63. * @param array $config 模板参数
  64. * @param bool $renderContent 是否渲染内容
  65. * @throws \Exception
  66. * @author 蔡伟明 <314013107@qq.com>
  67. */
  68. final protected function fetch($template = '', $vars = [], $config = [], $renderContent = false)
  69. {
  70. if ($template != '') {
  71. if (!is_file($template)) {
  72. $template = $this->plugin_path. 'view/'. $template . '.' . config('template.view_suffix');
  73. if (!is_file($template)) {
  74. throw new Exception('模板不存在:'.$template, 5001);
  75. }
  76. }
  77. echo $this->view->fetch($template, $vars, $config, $renderContent);
  78. }
  79. }
  80. /**
  81. * 模板变量赋值
  82. * @param string $name 要显示的模板变量
  83. * @param string $value 变量的值
  84. * @author 蔡伟明 <314013107@qq.com>
  85. * @return $this
  86. */
  87. final protected function assign($name = '', $value='')
  88. {
  89. $this->view->assign($name, $value);
  90. return $this;
  91. }
  92. /**
  93. * 获取插件配置值,先从数据库获取,如果没有则从插件配置文件获取
  94. * @param string $name 插件名称
  95. * @author 蔡伟明 <314013107@qq.com>
  96. * @return array|mixed
  97. */
  98. final public function getConfigValue($name='')
  99. {
  100. static $_config = array();
  101. if(empty($name)){
  102. $name = $this->getName();
  103. }
  104. if(isset($_config[$name])){
  105. return $_config[$name];
  106. }
  107. $config = plugin_config($name);
  108. if (!$config) {
  109. if ($this->config_file != '') {
  110. $file_config = include $this->config_file;
  111. }
  112. if (isset($file_config) && $file_config != '') {
  113. $config = parse_config($file_config);
  114. $_config[$name] = $config;
  115. }
  116. }
  117. return $config;
  118. }
  119. /**
  120. * 获取错误信息
  121. * @author 蔡伟明 <314013107@qq.com>
  122. * @return string
  123. */
  124. final public function getError()
  125. {
  126. return $this->error;
  127. }
  128. /**
  129. * 必须实现安装方法
  130. * @author 蔡伟明 <314013107@qq.com>
  131. * @return mixed
  132. */
  133. abstract public function install();
  134. /**
  135. * 必须实现卸载方法
  136. * @author 蔡伟明 <314013107@qq.com>
  137. * @return mixed
  138. */
  139. abstract public function uninstall();
  140. }