From ee00fc567f13adbd6c59f803bcec6000d1c06b91 Mon Sep 17 00:00:00 2001 From: joyqi Date: Tue, 7 Sep 2021 15:59:09 +0800 Subject: [PATCH] fix #1152 --- var/Widget/Base.php | 18 +++++++++++++----- var/Widget/Options.php | 13 +++++-------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/var/Widget/Base.php b/var/Widget/Base.php index cbfe50ec..d843654d 100644 --- a/var/Widget/Base.php +++ b/var/Widget/Base.php @@ -21,25 +21,30 @@ if (!defined('__TYPECHO_ROOT_DIR__')) { */ abstract class Base extends Widget { + /** + * init db + */ + protected const INIT_DB = 0b0001; + /** * init user widget */ - protected const INIT_USER = 0b001; + protected const INIT_USER = 0b0010; /** * init security widget */ - protected const INIT_SECURITY = 0b010; + protected const INIT_SECURITY = 0b0100; /** * init options widget */ - protected const INIT_OPTIONS = 0b100; + protected const INIT_OPTIONS = 0b1000; /** * init all widgets */ - protected const INIT_ALL = 0b111; + protected const INIT_ALL = 0b1111; /** * init none widget @@ -79,11 +84,14 @@ abstract class Base extends Widget */ protected function init() { - $this->db = Db::get(); $components = self::INIT_ALL; $this->initComponents($components); + if ($components != self::INIT_NONE) { + $this->db = Db::get(); + } + if ($components & self::INIT_USER) { $this->user = User::alloc(); } diff --git a/var/Widget/Options.php b/var/Widget/Options.php index e5a0dd72..8b08303e 100644 --- a/var/Widget/Options.php +++ b/var/Widget/Options.php @@ -4,6 +4,7 @@ namespace Widget; use Typecho\Common; use Typecho\Config; +use Typecho\Db; use Typecho\Router; use Typecho\Router\Parser; use Typecho\Widget; @@ -111,11 +112,6 @@ class Options extends Base */ private $personalPluginConfig = []; - /** - * @var bool options loaded - */ - private $loaded = false; - /** * @param int $components */ @@ -131,7 +127,8 @@ class Options extends Base { if (!$parameter->isEmpty()) { $this->row = $this->parameter->toArray(); - $this->loaded = true; + } else { + $this->db = Db::get(); } } @@ -142,7 +139,7 @@ class Options extends Base */ public function execute() { - if (!$this->loaded) { + if (isset($this->db)) { $values = $this->db->fetchAll($this->db->select()->from('table.options') ->where('user = 0'), [$this, 'push']); @@ -195,7 +192,7 @@ class Options extends Base /** 自动初始化路由表 */ $this->routingTable = unserialize($this->routingTable); - if ($this->loaded && !isset($this->routingTable[0])) { + if (isset($this->db) && !isset($this->routingTable[0])) { /** 解析路由并缓存 */ $parser = new Parser($this->routingTable); $parsedRoutingTable = $parser->parse();