1
0
mirror of https://github.com/suk-ws/ph-Bookshelf.git synced 2024-12-05 09:26:52 +08:00

使项目 composer/psr-4 标准化,清理代码

This commit is contained in:
A.C.Sukazyo Eyre 2023-01-24 20:30:27 +08:00
parent e8fe17b673
commit 0c83a11a23
Signed by: Eyre_S
GPG Key ID: C17CE40291207874
25 changed files with 114 additions and 81 deletions

View File

@ -24,8 +24,9 @@
- 使用其它 Webserver可以自行查询如何将 .htaccess 规则转换为你所使用的网站配置并写进你的网站配置当中 - 使用其它 Webserver可以自行查询如何将 .htaccess 规则转换为你所使用的网站配置并写进你的网站配置当中
- PHP 版本 8.0 以上 - PHP 版本 8.0 以上
(旧版可能可以使用,但未经完全测试) (旧版可能可以使用,但未经完全测试)
- PHP 模块 `xml` (旧版可能叫做 `dom`) - PHP 模块 `xml` (可能叫做 `dom`)
- PHP 模块 `mbstring` - PHP 模块 `mbstring`
- PHP 模块 `fileinfo`
- composer 工具以安装项目依赖 - composer 工具以安装项目依赖
- 在 php.ini 中设置 `display_errors` 以及 `display_startup_errors``Off` (或者关闭 `E_WARNING` 及以下 log) <small>(这是由于最开始写代码极不上心导致很多地方都会有可能报出 warn输出在屏幕上会导致很糟糕的使用体验)</small> - 在 php.ini 中设置 `display_errors` 以及 `display_startup_errors``Off` (或者关闭 `E_WARNING` 及以下 log) <small>(这是由于最开始写代码极不上心导致很多地方都会有可能报出 warn输出在屏幕上会导致很糟糕的使用体验)</small>

View File

@ -11,10 +11,17 @@
"email": "email@example.com" "email": "email@example.com"
} }
], ],
"autoload": {
"psr-4": {
"SukWs\\Bookshelf\\": "src"
}
},
"require": { "require": {
"php": ">=8.0", "php": ">=8.0",
"ext-xml": "*", "ext-xml": "*",
"ext-dom": "*",
"ext-mbstring": "*", "ext-mbstring": "*",
"ext-fileinfo": "*",
"league/commonmark": ">=2.3.8" "league/commonmark": ">=2.3.8"
} }
} }

View File

@ -2,6 +2,6 @@
const APP_NAME = "ph-Bookshelf"; const APP_NAME = "ph-Bookshelf";
const VERSION = "0.3.0.15"; const VERSION = "0.3.1";
const CHANNEL = "suk-ws"; const CHANNEL = "suk-ws";
const BRANCH = "master"; const BRANCH = "master";

View File

@ -1,13 +1,12 @@
<?php <?php
require_once "./vendor/autoload.php"; require "./constant.php";
require "./vendor/autoload.php";
require_once "./src/Data/SiteMeta.php";
require_once "./src/Data/PageMeta.php";
require_once "./src/Utils/PageParse.php";
require_once "./src/Utils/RequestNotExistException.php";
use SukWs\Bookshelf\Data\PageMeta;
use SukWs\Bookshelf\Data\SiteMeta;
use SukWs\Bookshelf\Utils\PageParse;
use SukWs\Bookshelf\Utils\RequestNotExistException;
try { try {
@ -40,12 +39,12 @@ try {
if ($tmp == null) throw new RequestNotExistException("Page required \"$uri[1]\" not found on book \"$uri[0]\"!"); if ($tmp == null) throw new RequestNotExistException("Page required \"$uri[1]\" not found on book \"$uri[0]\"!");
PageMeta::$page = $tmp; PageMeta::$page = $tmp;
} else { } else {
PageMeta::$page = PageMeta::$book->getChilds()->getChilds()[0]; PageMeta::$page = PageMeta::$book->getChilds()->getChildren()[0];
} }
} else { } else {
// 主页面 // 主页面
PageMeta::$book = SiteMeta::getBookshelf()->getRootBook(); PageMeta::$book = SiteMeta::getBookshelf()->getRootBook();
PageMeta::$page = PageMeta::$book->getChilds()->getChilds()[0]; PageMeta::$page = PageMeta::$book->getChilds()->getChildren()[0];
PageMeta::$isMainPage = true; PageMeta::$isMainPage = true;
} }

View File

@ -1,8 +1,9 @@
<?php <?php
require_once "./src/Data/SiteMeta.php"; namespace SukWs\Bookshelf\Data;
require_once "./src/Element/BookContent/BookContented.php";
require_once "./src/Element/BookContent/Page.php"; use SukWs\Bookshelf\Element\BookContent\BookContented;
use SukWs\Bookshelf\Element\BookContent\Page;
class PageMeta { class PageMeta {

View File

@ -1,8 +1,9 @@
<?php <?php
require_once "./src/Data/PageMeta.php"; namespace SukWs\Bookshelf\Data;
require_once "./src/Element/Bookshelf.php";
require_once "./constant.php"; use Exception;
use SukWs\Bookshelf\Element\Bookshelf;
class SiteMeta { class SiteMeta {
@ -19,7 +20,7 @@ class SiteMeta {
/** /**
* @throws Exception * @throws Exception
*/ */
public static function load () { public static function load (): void {
self::$BOOKSHELF = Bookshelf::parseString(file_get_contents("./data/bookshelf.xml")); self::$BOOKSHELF = Bookshelf::parseString(file_get_contents("./data/bookshelf.xml"));
} }

View File

@ -1,8 +1,11 @@
<?php <?php
require_once "./src/Data/PageMeta.php"; namespace SukWs\Bookshelf\Element;
require_once "./src/Element/BookCollection.php";
require_once "./src/Element/BookContent/BookContented.php"; use SukWs\Bookshelf\Data\PageMeta;
use DOMNode;
use SukWs\Bookshelf\Element\BookContent\BookContented;
use Exception;
class Book { class Book {
@ -50,7 +53,7 @@ class Book {
/** /**
* @return BookCollection|null * @return BookCollection|null
*/ */
public function getParent (): BookCollection { public function getParent (): ?BookCollection {
return $this->parent; return $this->parent;
} }

View File

@ -1,7 +1,10 @@
<?php <?php
require_once "./src/Data/PageMeta.php"; namespace SukWs\Bookshelf\Element;
require_once "./src/Element/Book.php";
use SukWs\Bookshelf\Data\PageMeta;
use DOMNode;
use Exception;
class BookCollection { class BookCollection {
@ -39,15 +42,16 @@ class BookCollection {
for ($child = $root->firstChild; $child != null; $child = $child->nextSibling) { for ($child = $root->firstChild; $child != null; $child = $child->nextSibling) {
switch ($child->nodeName) { switch ($child->nodeName) {
case "Book": case "Book":
array_push($node->array, Book::parse($child, $node)); $node->array[] = Book::parse($child, $node);
break; break;
case "Collection": case "Collection":
array_push($node->array, BookCollection::parse($child, $node)); $node->array[] = BookCollection::parse($child, $node);
break; break;
case "#comment": case "#comment":
break; break;
case "#text": case "#text":
if (empty(trim($child->nodeValue))) break; if (empty(trim($child->nodeValue))) break;
throw new Exception("Unsupported element type \"$child->nodeName\" in BookCollection named \"$name\"");
default: default:
throw new Exception("Unsupported element type \"$child->nodeName\" in BookCollection named \"$name\""); throw new Exception("Unsupported element type \"$child->nodeName\" in BookCollection named \"$name\"");
} }
@ -69,7 +73,7 @@ class BookCollection {
/** /**
* @return BookCollection|null * @return BookCollection|null
*/ */
public function getParent (): BookCollection { public function getParent (): ?BookCollection {
return $this->parent; return $this->parent;
} }

View File

@ -1,8 +1,11 @@
<?php <?php
require_once "./src/Element/Book.php"; namespace SukWs\Bookshelf\Element\BookContent;
require_once "./src/Element/Bookshelf.php";
require_once "./src/Element/BookContent/Chapter.php"; use DOMDocument;
use DOMNode;
use SukWs\Bookshelf\Element\Bookshelf;
use Exception;
class BookContented { class BookContented {

View File

@ -1,19 +1,23 @@
<?php <?php
require_once "./src/Element/BookContent/Page.php"; namespace SukWs\Bookshelf\Element\BookContent;
use SukWs\Bookshelf\Data\PageMeta;
use DOMNode;
use Exception;
class Chapter { class Chapter {
private string $name; private string $name;
/** @var Chapter[]|Page[] */ /** @var Chapter[]|Page[] */
private array $childs; private array $children;
private ?Chapter $parent; private ?Chapter $parent;
private function __construct (string $name, array $array, ?Chapter $parent) { private function __construct (string $name, array $array, ?Chapter $parent) {
$this->name = $name; $this->name = $name;
$this->childs = $array; $this->children = $array;
$this->parent = $parent; $this->parent = $parent;
} }
@ -32,15 +36,16 @@ class Chapter {
for ($child = $xmlData->firstChild;$child != null ; $child = $child->nextSibling) { for ($child = $xmlData->firstChild;$child != null ; $child = $child->nextSibling) {
switch ($child->nodeName) { switch ($child->nodeName) {
case "Page": case "Page":
array_push($node->childs, Page::parse($child, $node)); $node->children[] = Page::parse($child, $node);
break; break;
case "Chapter": case "Chapter":
array_push($node->childs, self::parse($child, $node)); $node->children[] = self::parse($child, $node);
break; break;
case "#comment": case "#comment":
break; break;
case "#text": case "#text":
if (empty(trim($child->nodeValue))) break; if (empty(trim($child->nodeValue))) break;
throw new Exception("Unsupported element type \"$child->nodeName\" in Chapter \"$node->name\"");
default: default:
throw new Exception("Unsupported element type \"$child->nodeName\" in Chapter \"$node->name\""); throw new Exception("Unsupported element type \"$child->nodeName\" in Chapter \"$node->name\"");
} }
@ -55,14 +60,14 @@ class Chapter {
/** /**
* @return Chapter[]|Page[] * @return Chapter[]|Page[]
*/ */
public function getChilds (): array { public function getChildren (): array {
return $this->childs; return $this->children;
} }
/** /**
* @return Chapter|null * @return Chapter|null
*/ */
public function getParent (): Chapter { public function getParent (): ?Chapter {
return $this->parent; return $this->parent;
} }
@ -76,7 +81,7 @@ class Chapter {
$this->getPage(PageMeta::$page->getId())==null?"":" active", $this->getPage(PageMeta::$page->getId())==null?"":" active",
$this->name $this->name
); );
foreach ($this->childs as $node) { foreach ($this->children as $node) {
$str .= $node->getSummaryHtml(); $str .= $node->getSummaryHtml();
} }
if ($this->parent != null) $str .= "</div></div>"; if ($this->parent != null) $str .= "</div></div>";
@ -85,7 +90,7 @@ class Chapter {
public function getPage (string $id): ?Page { public function getPage (string $id): ?Page {
foreach ($this->childs as $node) { foreach ($this->children as $node) {
if ($node instanceof Page && $node->getId() == $id) if ($node instanceof Page && $node->getId() == $id)
return $node; return $node;
else if ($node instanceof Chapter) { else if ($node instanceof Chapter) {

View File

@ -1,8 +1,11 @@
<?php <?php
require_once "./src/Data/PageMeta.php"; namespace SukWs\Bookshelf\Element\BookContent;
require_once "./src/Element/Bookshelf.php";
require_once "./src/Element/BookContent/Chapter.php"; use SukWs\Bookshelf\Data\PageMeta;
use DOMNode;
use SukWs\Bookshelf\Element\Bookshelf;
use Exception;
class Page { class Page {
@ -39,7 +42,7 @@ class Page {
Bookshelf::parseConfigurationAttr($xmlData->attributes, $node->configurations, array("name", "id")); Bookshelf::parseConfigurationAttr($xmlData->attributes, $node->configurations, array("name", "id"));
} else } else
throw new Exception("Book xml data missing attributes"); 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) { switch ($child->nodeName) {
case "#text": case "#text":
default: default:

View File

@ -1,8 +1,12 @@
<?php <?php
require_once "./src/Element/BookCollection.php"; namespace SukWs\Bookshelf\Element;
require_once "./src/Element/LinkCollection.php";
require_once "./src/Element/BookContent/BookContented.php"; use DOMDocument;
use DOMNamedNodeMap;
use DOMNode;
use SukWs\Bookshelf\Element\BookContent\BookContented;
use Exception;
class Bookshelf { class Bookshelf {
@ -51,6 +55,7 @@ class Bookshelf {
break; break;
case "#text": case "#text":
if (empty(trim($rc->nodeValue))) break; if (empty(trim($rc->nodeValue))) break;
throw new Exception("Unsupported element type \"$rc->nodeName\" in root child of Bookshelf");
default: default:
throw new Exception("Unsupported element type \"$rc->nodeName\" in root child of Bookshelf"); throw new Exception("Unsupported element type \"$rc->nodeName\" in root child of Bookshelf");
} }
@ -64,7 +69,7 @@ class Bookshelf {
/** /**
* @throws Exception * @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) { for ($rc = $dom->firstChild; $rc != null; $rc = $rc->nextSibling) {
if ($rc->nodeName == "#text") { if ($rc->nodeName == "#text") {
if (!empty(trim($rc->nodeValue))) 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) { foreach ($attributes as $attr) {
if (in_array($attr->name, $ignores)) continue; if (in_array($attr->name, $ignores)) continue;
$configurations[$attr->name] = $attr->value; $configurations[$attr->name] = $attr->value;

View File

@ -1,6 +1,9 @@
<?php <?php
require_once "./src/Element/LinkCollection.php"; namespace SukWs\Bookshelf\Element;
use DOMNode;
use Exception;
class Link { class Link {
@ -48,7 +51,7 @@ class Link {
/** /**
* @return LinkCollection|null * @return LinkCollection|null
*/ */
public function getParent (): LinkCollection { public function getParent (): ?LinkCollection {
return $this->parent; return $this->parent;
} }

View File

@ -1,6 +1,9 @@
<?php <?php
require_once "./src/Element/Link.php"; namespace SukWs\Bookshelf\Element;
use DOMNode;
use Exception;
class LinkCollection { class LinkCollection {
@ -38,15 +41,16 @@ class LinkCollection {
for ($child = $root->firstChild; $child != null; $child = $child->nextSibling) { for ($child = $root->firstChild; $child != null; $child = $child->nextSibling) {
switch ($child->nodeName) { switch ($child->nodeName) {
case "Link": case "Link":
array_push($node->array, Link::parse($child, $node)); $node->array[] = Link::parse($child, $node);
break; break;
case "Collection": case "Collection":
array_push($node->array, LinkCollection::parse($child, $node)); $node->array[] = LinkCollection::parse($child, $node);
break; break;
case "#comment": case "#comment":
break; break;
case "#text": case "#text":
if (empty(trim($child->nodeValue))) break; if (empty(trim($child->nodeValue))) break;
throw new Exception("Unsupported element type \"$child->nodeName\" in LinkCollection named \"$name\"");
default: default:
throw new Exception("Unsupported element type \"$child->nodeName\" in LinkCollection named \"$name\""); throw new Exception("Unsupported element type \"$child->nodeName\" in LinkCollection named \"$name\"");
} }
@ -68,7 +72,7 @@ class LinkCollection {
/** /**
* @return LinkCollection|null * @return LinkCollection|null
*/ */
public function getParent (): LinkCollection { public function getParent (): ?LinkCollection {
return $this->parent; return $this->parent;
} }

View File

@ -1,5 +1,7 @@
<?php <?php
namespace SukWs\Bookshelf\Utils\Markdown\CommonMarkExtensions;
class ExtensionRef { class ExtensionRef {
} }

View File

@ -1,5 +1,7 @@
<?php <?php
namespace SukWs\Bookshelf\Utils\Markdown;
use League\CommonMark\ConverterInterface; use League\CommonMark\ConverterInterface;
use League\CommonMark\Environment\Environment; use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\Attributes\AttributesExtension; use League\CommonMark\Extension\Attributes\AttributesExtension;

View File

@ -1,6 +1,6 @@
<?php <?php
require_once "./src/Utils/StringUtils.php"; namespace SukWs\Bookshelf\Utils;
class PageParse { class PageParse {

View File

@ -1,5 +1,7 @@
<?php <?php
namespace SukWs\Bookshelf\Utils;
use Erusev\Parsedown\Parsedown; use Erusev\Parsedown\Parsedown;
class ParsedownExtend extends Parsedown { class ParsedownExtend extends Parsedown {

View File

@ -1,5 +1,9 @@
<?php <?php
namespace SukWs\Bookshelf\Utils;
use Exception;
use Throwable;
class RequestNotExistException extends Exception { class RequestNotExistException extends Exception {

View File

@ -1,16 +0,0 @@
<?php
class StringUtils {
/**
* 删除所给字符串的换行符
*
* @param string $str 所给字符串
*
* @return string 剔除换行符的版本
*/
public static function rmEOL(string $str): string {
return str_replace(array("\r\n", "\r", "\n"), "", $str);
}
}

View File

@ -1,5 +1,5 @@
<?php require_once "./src/Data/SiteMeta.php" ?> <?php use SukWs\Bookshelf\Data\SiteMeta; ?>
<?php require_once "./src/Data/PageMeta.php" ?> <?php use SukWs\Bookshelf\Data\PageMeta; ?>
<!-- Assets(js) --><?php <!-- Assets(js) --><?php
foreach (SiteMeta::getJavascriptList() as $item) { foreach (SiteMeta::getJavascriptList() as $item) {

View File

@ -1,5 +1,5 @@
<?php require_once "./src/Data/SiteMeta.php" ?> <?php use SukWs\Bookshelf\Data\SiteMeta; ?>
<?php require_once "./src/Data/PageMeta.php" ?> <?php use SukWs\Bookshelf\Data\PageMeta; ?>
<!DOCTYPE HTML> <!DOCTYPE HTML>
<html lang="<?= "" // TODO Page language ?>"> <html lang="<?= "" // TODO Page language ?>">
<head> <head>

View File

@ -1,5 +1,5 @@
<?php require_once "./src/Data/SiteMeta.php" ?> <?php use SukWs\Bookshelf\Data\SiteMeta; ?>
<?php require_once "./src/Data/PageMeta.php" ?> <?php use SukWs\Bookshelf\Data\PageMeta; ?>
<div id="nav-container" class="prevent-animation"><nav id="sidebar"> <div id="nav-container" class="prevent-animation"><nav id="sidebar">
<noscript id="noscript-warn">For now, javascript must be enabled to view this site!!</noscript> <noscript id="noscript-warn">For now, javascript must be enabled to view this site!!</noscript>
<a id="site-title" class="no-style" href="/"><?= SiteMeta::getBookshelf()->getSiteName() ?></a> <a id="site-title" class="no-style" href="/"><?= SiteMeta::getBookshelf()->getSiteName() ?></a>

View File

@ -1,7 +1,7 @@
<?php <?php
require_once "./src/Data/PageMeta.php"; use SukWs\Bookshelf\Data\PageMeta;
require_once "./src/Utils/Markdown/Parser.php"; use SukWs\Bookshelf\Utils\Markdown\Parser;
$pageMarkdownContent = PageMeta::$page->getMarkdownContent(); $pageMarkdownContent = PageMeta::$page->getMarkdownContent();

View File

@ -1,5 +1,5 @@
<?php <?php
require_once "./src/Data/PageMeta.php"; use SukWs\Bookshelf\Data\PageMeta;
PageMeta::$page->getSummaryHtml(); PageMeta::$page->getSummaryHtml();