From 0c83a11a231959c9590368dcbd905a8d22e8419a Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Tue, 24 Jan 2023 20:30:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E9=A1=B9=E7=9B=AE=20composer/psr-4=20?= =?UTF-8?q?=E6=A0=87=E5=87=86=E5=8C=96=EF=BC=8C=E6=B8=85=E7=90=86=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 ++- composer.json | 7 ++++++ constant.php | 2 +- index.php | 17 ++++++------- src/Data/PageMeta.php | 7 +++--- src/Data/SiteMeta.php | 9 ++++--- src/Element/Book.php | 11 +++++--- src/Element/BookCollection.php | 14 +++++++---- src/Element/BookContent/BookContented.php | 9 ++++--- src/Element/BookContent/Chapter.php | 25 +++++++++++-------- src/Element/BookContent/Page.php | 11 +++++--- src/Element/Bookshelf.php | 15 +++++++---- src/Element/Link.php | 7 ++++-- src/Element/LinkCollection.php | 12 ++++++--- .../CommonMarkExtensions/ExtensionRef.php | 2 ++ src/Utils/Markdown/Parser.php | 2 ++ src/Utils/PageParse.php | 2 +- src/Utils/ParsedownExtend.php | 2 ++ src/Utils/RequestNotExistException.php | 4 +++ src/Utils/StringUtils.php | 16 ------------ template/footer.php | 4 +-- template/header.php | 4 +-- template/nav.php | 4 +-- template/raw-article.php | 4 +-- template/raw-book-contents.php | 2 +- 25 files changed, 114 insertions(+), 81 deletions(-) delete mode 100644 src/Utils/StringUtils.php diff --git a/README.md b/README.md index 505c39a..50da901 100644 --- a/README.md +++ b/README.md @@ -24,8 +24,9 @@ - 使用其它 Webserver,可以自行查询如何将 .htaccess 规则转换为你所使用的网站配置并写进你的网站配置当中 - PHP 版本 8.0 以上 (旧版可能可以使用,但未经完全测试) - - PHP 模块 `xml` (旧版可能叫做 `dom`) + - PHP 模块 `xml` (也可能叫做 `dom`) - PHP 模块 `mbstring` + - PHP 模块 `fileinfo` - composer 工具以安装项目依赖 - 在 php.ini 中设置 `display_errors` 以及 `display_startup_errors` 为 `Off` (或者关闭 `E_WARNING` 及以下 log) (这是由于最开始写代码极不上心导致很多地方都会有可能报出 warn,输出在屏幕上会导致很糟糕的使用体验) diff --git a/composer.json b/composer.json index 0aa877d..9645576 100644 --- a/composer.json +++ b/composer.json @@ -11,10 +11,17 @@ "email": "email@example.com" } ], + "autoload": { + "psr-4": { + "SukWs\\Bookshelf\\": "src" + } + }, "require": { "php": ">=8.0", "ext-xml": "*", + "ext-dom": "*", "ext-mbstring": "*", + "ext-fileinfo": "*", "league/commonmark": ">=2.3.8" } } \ No newline at end of file diff --git a/constant.php b/constant.php index b8983f8..d4f6824 100644 --- a/constant.php +++ b/constant.php @@ -2,6 +2,6 @@ const APP_NAME = "ph-Bookshelf"; -const VERSION = "0.3.0.15"; +const VERSION = "0.3.1"; const CHANNEL = "suk-ws"; const BRANCH = "master"; diff --git a/index.php b/index.php index c24b53f..c036fe4 100644 --- a/index.php +++ b/index.php @@ -1,13 +1,12 @@ getChilds()->getChilds()[0]; + PageMeta::$page = PageMeta::$book->getChilds()->getChildren()[0]; } } else { // 主页面 PageMeta::$book = SiteMeta::getBookshelf()->getRootBook(); - PageMeta::$page = PageMeta::$book->getChilds()->getChilds()[0]; + PageMeta::$page = PageMeta::$book->getChilds()->getChildren()[0]; PageMeta::$isMainPage = true; } diff --git a/src/Data/PageMeta.php b/src/Data/PageMeta.php index 70fdf4c..c1bf95f 100644 --- a/src/Data/PageMeta.php +++ b/src/Data/PageMeta.php @@ -1,8 +1,9 @@ parent; } diff --git a/src/Element/BookCollection.php b/src/Element/BookCollection.php index 788082e..0da485f 100644 --- a/src/Element/BookCollection.php +++ b/src/Element/BookCollection.php @@ -1,7 +1,10 @@ firstChild; $child != null; $child = $child->nextSibling) { switch ($child->nodeName) { case "Book": - array_push($node->array, Book::parse($child, $node)); + $node->array[] = Book::parse($child, $node); break; case "Collection": - array_push($node->array, BookCollection::parse($child, $node)); + $node->array[] = BookCollection::parse($child, $node); break; case "#comment": break; case "#text": if (empty(trim($child->nodeValue))) break; + throw new Exception("Unsupported element type \"$child->nodeName\" in BookCollection named \"$name\""); default: throw new Exception("Unsupported element type \"$child->nodeName\" in BookCollection named \"$name\""); } @@ -69,7 +73,7 @@ class BookCollection { /** * @return BookCollection|null */ - public function getParent (): BookCollection { + public function getParent (): ?BookCollection { return $this->parent; } diff --git a/src/Element/BookContent/BookContented.php b/src/Element/BookContent/BookContented.php index 412cbab..91da22b 100644 --- a/src/Element/BookContent/BookContented.php +++ b/src/Element/BookContent/BookContented.php @@ -1,8 +1,11 @@ name = $name; - $this->childs = $array; + $this->children = $array; $this->parent = $parent; } @@ -32,15 +36,16 @@ class Chapter { for ($child = $xmlData->firstChild;$child != null ; $child = $child->nextSibling) { switch ($child->nodeName) { case "Page": - array_push($node->childs, Page::parse($child, $node)); + $node->children[] = Page::parse($child, $node); break; case "Chapter": - array_push($node->childs, self::parse($child, $node)); + $node->children[] = self::parse($child, $node); break; case "#comment": break; case "#text": if (empty(trim($child->nodeValue))) break; + throw new Exception("Unsupported element type \"$child->nodeName\" in Chapter \"$node->name\""); default: throw new Exception("Unsupported element type \"$child->nodeName\" in Chapter \"$node->name\""); } @@ -55,14 +60,14 @@ class Chapter { /** * @return Chapter[]|Page[] */ - public function getChilds (): array { - return $this->childs; + public function getChildren (): array { + return $this->children; } /** * @return Chapter|null */ - public function getParent (): Chapter { + public function getParent (): ?Chapter { return $this->parent; } @@ -76,7 +81,7 @@ class Chapter { $this->getPage(PageMeta::$page->getId())==null?"":" active", $this->name ); - foreach ($this->childs as $node) { + foreach ($this->children as $node) { $str .= $node->getSummaryHtml(); } if ($this->parent != null) $str .= ""; @@ -85,7 +90,7 @@ class Chapter { public function getPage (string $id): ?Page { - foreach ($this->childs as $node) { + foreach ($this->children as $node) { if ($node instanceof Page && $node->getId() == $id) return $node; else if ($node instanceof Chapter) { diff --git a/src/Element/BookContent/Page.php b/src/Element/BookContent/Page.php index e59621a..a900875 100644 --- a/src/Element/BookContent/Page.php +++ b/src/Element/BookContent/Page.php @@ -1,8 +1,11 @@ attributes, $node->configurations, array("name", "id")); } else throw new Exception("Book xml data missing attributes"); - for ($child = $xmlData->firstChild;$child != null ; $child = $child->nextSibling) { + for ($child = $xmlData->firstChild; $child != null; $child = $child->nextSibling) { switch ($child->nodeName) { case "#text": default: diff --git a/src/Element/Bookshelf.php b/src/Element/Bookshelf.php index 5bf3f60..4ce01e3 100644 --- a/src/Element/Bookshelf.php +++ b/src/Element/Bookshelf.php @@ -1,8 +1,12 @@ nodeValue))) break; + throw new Exception("Unsupported element type \"$rc->nodeName\" in root child of Bookshelf"); default: throw new Exception("Unsupported element type \"$rc->nodeName\" in root child of Bookshelf"); } @@ -64,7 +69,7 @@ class Bookshelf { /** * @throws Exception */ - public static function parseConfiguration(DOMNode $dom, array &$configurations) { + public static function parseConfiguration(DOMNode $dom, array &$configurations): void { for ($rc = $dom->firstChild; $rc != null; $rc = $rc->nextSibling) { if ($rc->nodeName == "#text") { if (!empty(trim($rc->nodeValue))) @@ -87,7 +92,7 @@ class Bookshelf { } } - public static function parseConfigurationAttr (DOMNamedNodeMap $attributes, array &$configurations, array $ignores = array()) { + public static function parseConfigurationAttr (DOMNamedNodeMap $attributes, array &$configurations, array $ignores = array()): void { foreach ($attributes as $attr) { if (in_array($attr->name, $ignores)) continue; $configurations[$attr->name] = $attr->value; diff --git a/src/Element/Link.php b/src/Element/Link.php index 46aab9d..11588c5 100644 --- a/src/Element/Link.php +++ b/src/Element/Link.php @@ -1,6 +1,9 @@ parent; } diff --git a/src/Element/LinkCollection.php b/src/Element/LinkCollection.php index 589e47f..a49ce7d 100644 --- a/src/Element/LinkCollection.php +++ b/src/Element/LinkCollection.php @@ -1,6 +1,9 @@ firstChild; $child != null; $child = $child->nextSibling) { switch ($child->nodeName) { case "Link": - array_push($node->array, Link::parse($child, $node)); + $node->array[] = Link::parse($child, $node); break; case "Collection": - array_push($node->array, LinkCollection::parse($child, $node)); + $node->array[] = LinkCollection::parse($child, $node); break; case "#comment": break; case "#text": if (empty(trim($child->nodeValue))) break; + throw new Exception("Unsupported element type \"$child->nodeName\" in LinkCollection named \"$name\""); default: throw new Exception("Unsupported element type \"$child->nodeName\" in LinkCollection named \"$name\""); } @@ -68,7 +72,7 @@ class LinkCollection { /** * @return LinkCollection|null */ - public function getParent (): LinkCollection { + public function getParent (): ?LinkCollection { return $this->parent; } diff --git a/src/Utils/Markdown/CommonMarkExtensions/ExtensionRef.php b/src/Utils/Markdown/CommonMarkExtensions/ExtensionRef.php index 7ab72f6..fc1100b 100644 --- a/src/Utils/Markdown/CommonMarkExtensions/ExtensionRef.php +++ b/src/Utils/Markdown/CommonMarkExtensions/ExtensionRef.php @@ -1,5 +1,7 @@ - + + - + + "> diff --git a/template/nav.php b/template/nav.php index 8770322..94464ce 100644 --- a/template/nav.php +++ b/template/nav.php @@ -1,5 +1,5 @@ - - + +