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 规则转换为你所使用的网站配置并写进你的网站配置当中
- 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) <small>(这是由于最开始写代码极不上心导致很多地方都会有可能报出 warn输出在屏幕上会导致很糟糕的使用体验)</small>

View File

@ -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"
}
}

View File

@ -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";

View File

@ -1,13 +1,12 @@
<?php
require_once "./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";
require "./constant.php";
require "./vendor/autoload.php";
use SukWs\Bookshelf\Data\PageMeta;
use SukWs\Bookshelf\Data\SiteMeta;
use SukWs\Bookshelf\Utils\PageParse;
use SukWs\Bookshelf\Utils\RequestNotExistException;
try {
@ -40,12 +39,12 @@ try {
if ($tmp == null) throw new RequestNotExistException("Page required \"$uri[1]\" not found on book \"$uri[0]\"!");
PageMeta::$page = $tmp;
} else {
PageMeta::$page = PageMeta::$book->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;
}

View File

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

View File

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

View File

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

View File

@ -1,7 +1,10 @@
<?php
require_once "./src/Data/PageMeta.php";
require_once "./src/Element/Book.php";
namespace SukWs\Bookshelf\Element;
use SukWs\Bookshelf\Data\PageMeta;
use DOMNode;
use Exception;
class BookCollection {
@ -39,15 +42,16 @@ class BookCollection {
for ($child = $root->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;
}

View File

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

View File

@ -1,19 +1,23 @@
<?php
require_once "./src/Element/BookContent/Page.php";
namespace SukWs\Bookshelf\Element\BookContent;
use SukWs\Bookshelf\Data\PageMeta;
use DOMNode;
use Exception;
class Chapter {
private string $name;
/** @var Chapter[]|Page[] */
private array $childs;
private array $children;
private ?Chapter $parent;
private function __construct (string $name, array $array, ?Chapter $parent) {
$this->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 .= "</div></div>";
@ -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) {

View File

@ -1,8 +1,11 @@
<?php
require_once "./src/Data/PageMeta.php";
require_once "./src/Element/Bookshelf.php";
require_once "./src/Element/BookContent/Chapter.php";
namespace SukWs\Bookshelf\Element\BookContent;
use SukWs\Bookshelf\Data\PageMeta;
use DOMNode;
use SukWs\Bookshelf\Element\Bookshelf;
use Exception;
class Page {
@ -39,7 +42,7 @@ class Page {
Bookshelf::parseConfigurationAttr($xmlData->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:

View File

@ -1,8 +1,12 @@
<?php
require_once "./src/Element/BookCollection.php";
require_once "./src/Element/LinkCollection.php";
require_once "./src/Element/BookContent/BookContented.php";
namespace SukWs\Bookshelf\Element;
use DOMDocument;
use DOMNamedNodeMap;
use DOMNode;
use SukWs\Bookshelf\Element\BookContent\BookContented;
use Exception;
class Bookshelf {
@ -51,6 +55,7 @@ class Bookshelf {
break;
case "#text":
if (empty(trim($rc->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;

View File

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

View File

@ -1,6 +1,9 @@
<?php
require_once "./src/Element/Link.php";
namespace SukWs\Bookshelf\Element;
use DOMNode;
use Exception;
class LinkCollection {
@ -38,15 +41,16 @@ class LinkCollection {
for ($child = $root->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;
}

View File

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

View File

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

View File

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

View File

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

View File

@ -1,5 +1,9 @@
<?php
namespace SukWs\Bookshelf\Utils;
use Exception;
use Throwable;
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 require_once "./src/Data/PageMeta.php" ?>
<?php use SukWs\Bookshelf\Data\SiteMeta; ?>
<?php use SukWs\Bookshelf\Data\PageMeta; ?>
<!-- Assets(js) --><?php
foreach (SiteMeta::getJavascriptList() as $item) {

View File

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

View File

@ -1,5 +1,5 @@
<?php require_once "./src/Data/SiteMeta.php" ?>
<?php require_once "./src/Data/PageMeta.php" ?>
<?php use SukWs\Bookshelf\Data\SiteMeta; ?>
<?php use SukWs\Bookshelf\Data\PageMeta; ?>
<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>
<a id="site-title" class="no-style" href="/"><?= SiteMeta::getBookshelf()->getSiteName() ?></a>

View File

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

View File

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