mirror of
https://github.com/suk-ws/ph-Bookshelf.git
synced 2025-01-18 23:12:23 +08:00
support frontend to read bookshelf config data, bookshelf.xml parse fixup, add WebLog.
This commit is contained in:
parent
a7abbec4ca
commit
0d6672f07f
21
index.php
21
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();
|
||||
|
@ -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 <site_name> 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.
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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;
|
||||
|
@ -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());
|
||||
|
43
src/Web/WebLog.php
Normal file
43
src/Web/WebLog.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace SukWs\Bookshelf\Web;
|
||||
|
||||
use DOMElement;
|
||||
use SukWs\Bookshelf\Web\WebResource\JavascriptRaw;
|
||||
|
||||
class WebLog {
|
||||
|
||||
/** @var string[] $messages */
|
||||
private static array $messages = array();
|
||||
|
||||
public static function info(string $message): void {
|
||||
self::log($message, "info");
|
||||
}
|
||||
|
||||
public static function warn(string $message): void {
|
||||
self::log($message, "warn");
|
||||
}
|
||||
|
||||
public static function error(string $message): void {
|
||||
self::log($message, "error");
|
||||
}
|
||||
|
||||
public static function log (string $message, string $level): void {
|
||||
self::$messages[] = "[ph-Bookshelf][$level]>> $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);
|
||||
}
|
||||
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace SukWs\Bookshelf\Web;
|
||||
|
||||
use DOMElement;
|
||||
use SukWs\Bookshelf\Web\WebResource\JavascriptRaw;
|
||||
|
||||
class WebWarn {
|
||||
|
||||
/** @var string[] $warnings */
|
||||
private static array $warnings = array();
|
||||
|
||||
public static function output(string $message): void {
|
||||
self::$warnings[] = $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::$warnings as $message) {
|
||||
$message = str_replace("\"", "\\\"", $message);
|
||||
$message = "[ph-Bookshelf] " . $message;
|
||||
$js_log_code .= "console.log(\"$message\");\n";
|
||||
}
|
||||
return new JavascriptRaw($js_log_code);
|
||||
}
|
||||
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
<?php use SukWs\Bookshelf\SiteConfig\ConfigName; ?>
|
||||
<?php use SukWs\Bookshelf\SiteMeta; ?>
|
||||
<?php use SukWs\Bookshelf\PageMeta; ?>
|
||||
<?php use SukWs\Bookshelf\Web\WebWarn; ?>
|
||||
<?php use SukWs\Bookshelf\Web\WebLog; ?>
|
||||
<!DOCTYPE HTML>
|
||||
<html lang="<?= "" // TODO Page language ?>">
|
||||
<head>
|
||||
@ -36,9 +36,9 @@
|
||||
}
|
||||
</style>
|
||||
<style><?= SiteMeta::getCustomCssContent("custom") ?></style>
|
||||
<?php WebWarn::output("test warn"); ?>
|
||||
<?php WebLog::warn("test warn"); ?>
|
||||
<?php
|
||||
$__web_js_warning_log = WebWarn::getWarningsAsJsLog();
|
||||
$__web_js_warning_log = WebLog::getWarningsAsJsLog();
|
||||
$__web_js_warning_log_document = new DOMDocument();
|
||||
$__web_js_warning_log_document->appendChild($__web_js_warning_log);
|
||||
echo $__web_js_warning_log_document->saveHTML();
|
||||
|
Loading…
Reference in New Issue
Block a user