PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/Application/Common/Controller/Addon.class.php

https://gitlab.com/xuebutayan/yshop
PHP | 149 lines | 98 code | 10 blank | 41 comment | 5 complexity | 3b4377e601df8c7aa5ab72b9f650a4c5 MD5 | raw file
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | OneThink [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: yangweijie <yangweijiester@gmail.com> <code-tech.diandian.com>
  8. // +----------------------------------------------------------------------
  9. namespace Common\Controller;
  10. /**
  11. * 插件类
  12. * @author yangweijie <yangweijiester@gmail.com>
  13. */
  14. abstract class Addon{
  15. /**
  16. * 视图实例对象
  17. * @var view
  18. * @access protected
  19. */
  20. protected $view = null;
  21. /**
  22. * $info = array(
  23. * 'name'=>'Editor',
  24. * 'title'=>'编辑器',
  25. * 'description'=>'用于增强整站长文本的输入和显示',
  26. * 'status'=>1,
  27. * 'author'=>'thinkphp',
  28. * 'version'=>'0.1'
  29. * )
  30. */
  31. public $info = array();
  32. public $addon_path = '';
  33. public $config_file = '';
  34. public $custom_config = '';
  35. public $admin_list = array();
  36. public $custom_adminlist = '';
  37. public $access_url = array();
  38. public function __construct(){
  39. $this->view = \Think\Think::instance('Think\View');
  40. $this->addon_path = ONETHINK_ADDON_PATH.$this->getName().'/';
  41. $TMPL_PARSE_STRING = C('TMPL_PARSE_STRING');
  42. $TMPL_PARSE_STRING['__ADDONROOT__'] = __ROOT__ . '/Addons/'.$this->getName();
  43. C('TMPL_PARSE_STRING', $TMPL_PARSE_STRING);
  44. if(is_file($this->addon_path.'config.php')){
  45. $this->config_file = $this->addon_path.'config.php';
  46. }
  47. }
  48. /**
  49. * 模板主题设置
  50. * @access protected
  51. * @param string $theme 模版主题
  52. * @return Action
  53. */
  54. final protected function theme($theme){
  55. $this->view->theme($theme);
  56. return $this;
  57. }
  58. //显示方法
  59. final protected function display($template=''){
  60. if($template == '')
  61. $template = CONTROLLER_NAME;
  62. echo ($this->fetch($template));
  63. }
  64. /**
  65. * 模板变量赋值
  66. * @access protected
  67. * @param mixed $name 要显示的模板变量
  68. * @param mixed $value 变量的值
  69. * @return Action
  70. */
  71. final protected function assign($name,$value='') {
  72. $this->view->assign($name,$value);
  73. return $this;
  74. }
  75. //用于显示模板的方法
  76. final protected function fetch($templateFile = CONTROLLER_NAME){
  77. if(!is_file($templateFile)){
  78. $templateFile = $this->addon_path.$templateFile.C('TMPL_TEMPLATE_SUFFIX');
  79. if(!is_file($templateFile)){
  80. throw new \Exception("模板不存在:$templateFile");
  81. }
  82. }
  83. return $this->view->fetch($templateFile);
  84. }
  85. final public function getName(){
  86. $class = get_class($this);
  87. return substr($class,strrpos($class, '\\')+1, -5);
  88. }
  89. final public function checkInfo(){
  90. $info_check_keys = array('name','title','description','status','author','version');
  91. foreach ($info_check_keys as $value) {
  92. if(!array_key_exists($value, $this->info))
  93. return FALSE;
  94. }
  95. return TRUE;
  96. }
  97. /**
  98. * 获取插件的配置数组
  99. */
  100. final public function getConfig($name=''){
  101. static $_config = array();
  102. if(empty($name)){
  103. $name = $this->getName();
  104. }
  105. if(isset($_config[$name])){
  106. return $_config[$name];
  107. }
  108. $config = array();
  109. $map['name'] = $name;
  110. $map['status'] = 1;
  111. $config = M('Addons')->where($map)->getField('config');
  112. if($config){
  113. $config = json_decode($config, true);
  114. }else{
  115. $temp_arr = include $this->config_file;
  116. foreach ($temp_arr as $key => $value) {
  117. if($value['type'] == 'group'){
  118. foreach ($value['options'] as $gkey => $gvalue) {
  119. foreach ($gvalue['options'] as $ikey => $ivalue) {
  120. $config[$ikey] = $ivalue['value'];
  121. }
  122. }
  123. }else{
  124. $config[$key] = $temp_arr[$key]['value'];
  125. }
  126. }
  127. }
  128. $_config[$name] = $config;
  129. return $config;
  130. }
  131. //必须实现安装
  132. abstract public function install();
  133. //必须卸载插件方法
  134. abstract public function uninstall();
  135. }