123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- <?php
- namespace cmf\lib;
- use think\exception\TemplateNotFoundException;
- use think\Lang;
- use think\Loader;
- use think\Request;
- use think\View;
- use think\Config;
- use think\Db;
- abstract class Plugin
- {
-
- private $view = null;
-
- public $info = [];
- private $pluginPath = '';
- private $name = '';
- private $configFilePath = '';
- private $themeRoot = "";
- public function __construct()
- {
- $request = Request::instance();
- $engineConfig = Config::get('template');
- $this->name = $this->getName();
- $nameCStyle = Loader::parseName($this->name);
- $this->pluginPath = PLUGINS_PATH . $nameCStyle . '/';
- $this->configFilePath = $this->pluginPath . 'config.php';
- $config = $this->getConfig();
- $theme = isset($config['theme']) ? $config['theme'] : '';
- $depr = "/";
- $root = cmf_get_root();
- $themeDir = empty($theme) ? "" : '/' . $theme;
- $themePath = 'view' . $themeDir;
- $this->themeRoot = $this->pluginPath . $themePath . '/';
- $engineConfig['view_base'] = $this->themeRoot;
- $pluginRoot = $root . "plugins/{$nameCStyle}";
- $cmfAdminThemePath = config('cmf_admin_theme_path');
- $cmfAdminDefaultTheme = config('cmf_admin_default_theme');
- $adminThemePath = "{$cmfAdminThemePath}{$cmfAdminDefaultTheme}";
-
- $cdnSettings = cmf_get_option('cdn_settings');
- if (empty($cdnSettings['cdn_static_root'])) {
- $replaceConfig = [
- '__PLUGIN_TMPL__' => $pluginRoot . '/' . $themePath,
- '__PLUGIN_ROOT__' => $pluginRoot,
- '__ADMIN_TMPL__' => "{$root}/{$adminThemePath}",
- '__STATIC__' => "{$root}/static",
- '__WEB_ROOT__' => $root
- ];
- } else {
- $cdnStaticRoot = rtrim($cdnSettings['cdn_static_root'], '/');
- $replaceConfig = [
- '__PLUGIN_TMPL__' => $cdnStaticRoot . '/' . $pluginRoot . '/' . $themePath,
- '__PLUGIN_ROOT__' => $cdnStaticRoot . '/' . $pluginRoot,
- '__ADMIN_TMPL__' => "{$cdnStaticRoot}/{$adminThemePath}",
- '__STATIC__' => "{$cdnStaticRoot}/static",
- '__WEB_ROOT__' => $cdnStaticRoot
- ];
- }
- $this->view = new View($engineConfig, $replaceConfig);
-
- $langSet = $request->langset();
- $lang_file = $this->pluginPath . "lang/" . $langSet . ".php";
- Lang::load($lang_file);
- }
-
- final protected function fetch($template)
- {
- if (!is_file($template)) {
- $engineConfig = Config::get('template');
- $template = $this->themeRoot . $template . '.' . $engineConfig['view_suffix'];
- }
-
- if (!is_file($template)) {
- throw new TemplateNotFoundException('template not exists:' . $template, $template);
- }
- return $this->view->fetch($template);
- }
-
- final protected function display($content = '')
- {
- return $this->view->display($content);
- }
-
- final protected function assign($name, $value = '')
- {
- $this->view->assign($name, $value);
- }
-
- final public function getName()
- {
- if (empty($this->name)) {
- $class = get_class($this);
- $this->name = substr($class, strrpos($class, '\\') + 1, -6);
- }
- return $this->name;
- }
-
- final public function checkInfo()
- {
- $infoCheckKeys = ['name', 'title', 'description', 'status', 'author', 'version'];
- foreach ($infoCheckKeys as $value) {
- if (!array_key_exists($value, $this->info))
- return false;
- }
- return true;
- }
-
- final public function getPluginPath()
- {
- return $this->pluginPath;
- }
-
- final public function getConfigFilePath()
- {
- return $this->configFilePath;
- }
-
- final public function getThemeRoot()
- {
- return $this->themeRoot;
- }
-
- public function getView()
- {
- return $this->view;
- }
-
- final public function getConfig()
- {
- static $_config = [];
- $name = $this->getName();
- if (isset($_config[$name])) {
- return $_config[$name];
- }
- $config = Db::name('plugin')->where('name', $name)->value('config');
- if (!empty($config) && $config != "null") {
- $config = json_decode($config, true);
- } else {
- $config = $this->getDefaultConfig();
- }
- $_config[$name] = $config;
- return $config;
- }
-
- final public function getDefaultConfig()
- {
- $config = [];
- if (file_exists($this->configFilePath)) {
- $tempArr = include $this->configFilePath;
- if (!empty($tempArr)) {
- foreach ($tempArr as $key => $value) {
- if ($value['type'] == 'group') {
- foreach ($value['options'] as $gkey => $gvalue) {
- foreach ($gvalue['options'] as $ikey => $ivalue) {
- $config[$ikey] = $ivalue['value'];
- }
- }
- } else {
- $config[$key] = $tempArr[$key]['value'];
- }
- }
- }
- }
- return $config;
- }
-
- abstract public function install();
-
- abstract public function uninstall();
- }
|