From 0d6672f07f77706b3ba006a7b96eef1d8b2ed64c Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sun, 12 Mar 2023 19:40:50 +0800 Subject: [PATCH] support frontend to read bookshelf config data, bookshelf.xml parse fixup, add WebLog. --- index.php | 21 ++++++++++++++ src/Data/Bookshelf/NodeBookshelf.php | 18 +++++++----- src/Utils/DOMXMLTools.php | 17 ++++++++++- src/Web/Html/Body.php | 9 ++++-- src/Web/Html/Head.php | 5 ++++ src/Web/Html/Sidebar/Sidebar.php | 5 ++++ src/Web/HtmlPage.php | 6 ++++ src/Web/WebLog.php | 43 ++++++++++++++++++++++++++++ src/Web/WebWarn.php | 33 --------------------- template/header.php | 6 ++-- 10 files changed, 117 insertions(+), 46 deletions(-) create mode 100644 src/Web/WebLog.php delete mode 100644 src/Web/WebWarn.php diff --git a/index.php b/index.php index db54cae..2051c09 100644 --- a/index.php +++ b/index.php @@ -3,15 +3,36 @@ require "./constant.php"; require "./vendor/autoload.php"; +use SukWs\Bookshelf\Data\Bookshelf\NodeBookshelf; use SukWs\Bookshelf\PageMeta; use SukWs\Bookshelf\SiteConfig\RobotsPolicy; use SukWs\Bookshelf\SiteMeta; use SukWs\Bookshelf\Utils\PageParse; use SukWs\Bookshelf\Utils\RequestNotExistException; use SukWs\Bookshelf\Web\HtmlPage; +use SukWs\Bookshelf\Web\WebLog; + +const PHB_DATA_PATH = "./data/"; +const PHB_SHELF_CONF_PATH = PHB_DATA_PATH . "bookshelf.xml"; $page = new HtmlPage(); +if (!file_exists(PHB_SHELF_CONF_PATH)) { + $page->_html_body->_sidebar->web_error->addErrorMessage("No /data/bookshelf.xml in your website!"); +} else try { + + $bookshelf = NodeBookshelf::__load_from_xml( + file_get_contents(PHB_SHELF_CONF_PATH) + ); + WebLog::info("loaded bookshelf.xml\n at path ".PHB_SHELF_CONF_PATH."\n file md5 ".md5_file(PHB_SHELF_CONF_PATH)); + + $page->_parseBookshelf($bookshelf); + +} catch (Exception $e) { + $page->_html_body->_sidebar->web_error->addErrorMessage($e->getMessage()); + WebLog::warn($e); +} + echo $page->build()->document->saveHTML(); exit(); diff --git a/src/Data/Bookshelf/NodeBookshelf.php b/src/Data/Bookshelf/NodeBookshelf.php index 002abc8..241c0a1 100644 --- a/src/Data/Bookshelf/NodeBookshelf.php +++ b/src/Data/Bookshelf/NodeBookshelf.php @@ -7,7 +7,7 @@ use DOMElement; use DOMNode; use Exception; use SukWs\Bookshelf\Utils\DOMXMLTools; -use SukWs\Bookshelf\Web\WebWarn; +use SukWs\Bookshelf\Web\WebLog; class NodeBookshelf { @@ -15,7 +15,7 @@ class NodeBookshelf { public readonly string $_site_name; - public readonly array $_configurations; + public array $_configurations; /** * @throws Exception @@ -25,16 +25,19 @@ class NodeBookshelf { /* "" version "" */ $this->_Attr_version = $node_Bookshelf->attributes->getNamedItem("version")?->nodeValue; // configure version check - if ($this->_Attr_version == null) WebWarn::output("bookshelf.xml:: file version is not declared.\n - the current ph-bookshelf uses version 2.0"); + if ($this->_Attr_version == null) WebLog::warn("bookshelf.xml:: file version is not declared.\n - the current ph-bookshelf uses version 2.0"); else if ($this->_Attr_version != "2.0") throw new Exception("-0I{@EI[AID"); // todo throw exception + WebLog::info("bookshelf.xml:: read bookshelf.xml with config version ".$this->_Attr_version); /* @var DOMNode $dom_child */ - $dom_child = $node_Bookshelf->firstChild; + /* @var DOMElement $dom_child */$dom_child = DOMXMLTools::firstChild($node_Bookshelf); + if ($dom_child == null) throw new Exception("bookshelf.xml:: is empty!"); /* == site_name == */ - if (!$dom_child->nodeName == "site_name") throw new Exception("O*R*OIArlAIWR"); // todo throw exception that site_name unavailable. + if (!$dom_child->nodeName == "site_name") throw new Exception("bookshelf.xml:: required defined first but <".$dom_child->nodeName."> found."); // todo throw exception that site_name unavailable. $this->_site_name = $dom_child->nodeValue; - $dom_child = $dom_child->nextSibling; + DOMXMLTools::next($dom_child); + WebLog::info("bookshelf.xml:: read site_name \"".$this->_site_name."\""); /* == configurations == */ if ($dom_child->nodeName == "configurations") { @@ -43,9 +46,10 @@ class NodeBookshelf { foreach ($dom_child->childNodes as $configNode) { if (DOMXMLTools::isEmpty($configNode)) continue; $my_configurations[$configNode->nodeName] = $configNode->nodeValue; + WebLog::info("bookshelf.xml:: configuration:: read [".$configNode->nodeName."] = \"".$configNode->nodeValue."\""); } $this->_configurations = $my_configurations; - $dom_child = $dom_child->nextSibling; + DOMXMLTools::next($dom_child); } // todo elements. diff --git a/src/Utils/DOMXMLTools.php b/src/Utils/DOMXMLTools.php index bc43736..f79873d 100644 --- a/src/Utils/DOMXMLTools.php +++ b/src/Utils/DOMXMLTools.php @@ -3,10 +3,12 @@ namespace SukWs\Bookshelf\Utils; use DOMElement; +use DOMNode; +use DOMText; class DOMXMLTools { - public static function isEmpty (DOMElement $element, bool $allow_empty_text = true, bool $allow_cdata = false): bool { + public static function isEmpty (DOMNode $element, bool $allow_empty_text = true, bool $allow_cdata = false): bool { if ($element->nodeName == "#comment") return true; if ($allow_empty_text && $element->nodeName == "#text" && empty(trim($element->nodeValue))) @@ -16,4 +18,17 @@ class DOMXMLTools { return false; } + public static function firstChild (DOMElement $element): ?DOMElement { + $current = $element->firstChild; + if ($current != null && self::isEmpty($current)) + self::next($current); + return $current; + } + + public static function next (DOMElement|DOMText &$current): void { + do + $current = $current->nextSibling; + while ($current != null && self::isEmpty($current)); + } + } \ No newline at end of file diff --git a/src/Web/Html/Body.php b/src/Web/Html/Body.php index 5babe0e..8113c31 100644 --- a/src/Web/Html/Body.php +++ b/src/Web/Html/Body.php @@ -4,10 +4,11 @@ namespace SukWs\Bookshelf\Web\Html; use DOMDocument; use DOMElement; +use SukWs\Bookshelf\Data\Bookshelf\NodeBookshelf; use SukWs\Bookshelf\Web\Html\Main\MainContainer; use SukWs\Bookshelf\Web\Html\Sidebar\Sidebar; use SukWs\Bookshelf\Web\HtmlPage; -use SukWs\Bookshelf\Web\WebWarn; +use SukWs\Bookshelf\Web\WebLog; class Body { @@ -28,6 +29,10 @@ class Body { } + public function _parseBookshelf (NodeBookshelf $_data_shelf): void { + $this->_sidebar->_parseBookshelf($_data_shelf); + } + public function build (): DOMElement { $this->__self->appendChild($this->_sidebar->build()); @@ -37,7 +42,7 @@ class Body { $this->__self->appendChild($script); // output the warnings message at the end. - $this->__self->appendChild(WebWarn::getWarningsAsJsLog()->build($this->root->document)); + $this->__self->appendChild(WebLog::getWarningsAsJsLog()->build($this->root->document)); return $this->__self; diff --git a/src/Web/Html/Head.php b/src/Web/Html/Head.php index 2565ce3..af1611e 100644 --- a/src/Web/Html/Head.php +++ b/src/Web/Html/Head.php @@ -3,6 +3,7 @@ namespace SukWs\Bookshelf\Web\Html; use DOMElement; +use SukWs\Bookshelf\Data\Bookshelf\NodeBookshelf; use SukWs\Bookshelf\Utils\DOMHtml; use SukWs\Bookshelf\Web\HtmlPage; @@ -32,6 +33,10 @@ class Head { } + public function _parseBookshelf (NodeBookshelf $_data_shelf): void { + // todo use shelf config + } + public function build (): DOMElement { foreach ($this->standard_headers as $meta) diff --git a/src/Web/Html/Sidebar/Sidebar.php b/src/Web/Html/Sidebar/Sidebar.php index 2c34434..b5fa15d 100644 --- a/src/Web/Html/Sidebar/Sidebar.php +++ b/src/Web/Html/Sidebar/Sidebar.php @@ -3,6 +3,7 @@ namespace SukWs\Bookshelf\Web\Html\Sidebar; use DOMElement; +use SukWs\Bookshelf\Data\Bookshelf\NodeBookshelf; use SukWs\Bookshelf\Web\Html\Body; use SukWs\Bookshelf\Web\HtmlPage; @@ -52,6 +53,10 @@ class Sidebar { } + public function _parseBookshelf (NodeBookshelf $_data_shelf): void { + $this->setSiteTitle($_data_shelf->_site_name); + } + public function toggleSidebarShow (?bool $show = null): void { if ($show === null) $show = !$this->show_sidebar; $this->show_sidebar = $show; diff --git a/src/Web/HtmlPage.php b/src/Web/HtmlPage.php index c2f596b..207c124 100644 --- a/src/Web/HtmlPage.php +++ b/src/Web/HtmlPage.php @@ -4,6 +4,7 @@ namespace SukWs\Bookshelf\Web; use DOMDocument; use DOMElement; +use SukWs\Bookshelf\Data\Bookshelf\NodeBookshelf; use SukWs\Bookshelf\Utils\DOMHtml; use SukWs\Bookshelf\Web\Html\Body; use SukWs\Bookshelf\Web\Html\Head; @@ -36,6 +37,11 @@ class HtmlPage { } + public function _parseBookshelf (NodeBookshelf $_data_shelf): void { + $this->_html_head->_parseBookshelf($_data_shelf); + $this->_html_body->_parseBookshelf($_data_shelf); + } + public function build(): self { $this->_html_html->appendChild($this->_html_head->build()); $this->_html_html->appendChild($this->_html_body->build()); diff --git a/src/Web/WebLog.php b/src/Web/WebLog.php new file mode 100644 index 0000000..700b566 --- /dev/null +++ b/src/Web/WebLog.php @@ -0,0 +1,43 @@ +> $message"; + } + + /** + * Build a new "script" DOMElement that contains a list + * of `console.log` js code to output the warning messages. + * + * @return JavascriptRaw a fully new "script" object. + */ + public static function getWarningsAsJsLog (): JavascriptRaw { + /* @var */ $js_log_code = ""; + foreach (self::$messages as $message) { + $js_log_code .= "console.log(" . json_encode($message) .");"; + } + return new JavascriptRaw($js_log_code); + } + +} \ No newline at end of file diff --git a/src/Web/WebWarn.php b/src/Web/WebWarn.php deleted file mode 100644 index 8d51dc7..0000000 --- a/src/Web/WebWarn.php +++ /dev/null @@ -1,33 +0,0 @@ - - + "> @@ -36,9 +36,9 @@ } - + appendChild($__web_js_warning_log); echo $__web_js_warning_log_document->saveHTML();