refactor change self:: to static::

This commit is contained in:
小喵 2023-08-27 15:27:42 +08:00
parent cc92d67c05
commit 0393ab8b89
Signed by: mt
GPG key ID: CD321C269EC63BB6
4 changed files with 36 additions and 20 deletions

16
.idea/php.xml generated
View file

@ -1,5 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="MessDetectorOptionsConfiguration">
<option name="transferred" value="true" />
</component>
<component name="PHPCSFixerOptionsConfiguration">
<option name="transferred" value="true" />
</component>
<component name="PHPCodeSnifferOptionsConfiguration">
<option name="highlightLevel" value="WARNING" />
<option name="transferred" value="true" />
</component>
<component name="PhpIncludePathManager"> <component name="PhpIncludePathManager">
<include_path> <include_path>
<path value="$PROJECT_DIR$/vendor/composer" /> <path value="$PROJECT_DIR$/vendor/composer" />
@ -17,9 +27,15 @@
</include_path> </include_path>
</component> </component>
<component name="PhpProjectSharedConfiguration" php_language_level="8.1" /> <component name="PhpProjectSharedConfiguration" php_language_level="8.1" />
<component name="PhpStanOptionsConfiguration">
<option name="transferred" value="true" />
</component>
<component name="PhpUnit"> <component name="PhpUnit">
<phpunit_settings> <phpunit_settings>
<PhpUnitSettings custom_loader_path="$PROJECT_DIR$/vendor/autoload.php" /> <PhpUnitSettings custom_loader_path="$PROJECT_DIR$/vendor/autoload.php" />
</phpunit_settings> </phpunit_settings>
</component> </component>
<component name="PsalmOptionsConfiguration">
<option name="transferred" value="true" />
</component>
</project> </project>

View file

@ -21,14 +21,14 @@ class App
public static function getTemplate(): Engine public static function getTemplate(): Engine
{ {
static $templates = new Engine(__DIR__ . '/../templates'); static $templates = new Engine(__DIR__ . '/../templates');
$me = self::me(); $me = static::me();
$isLogin = self::checkU(); $isLogin = static::checkU();
$templates->addData([ $templates->addData([
'me' => $me, 'me' => $me,
'whoami' => "$me->username#$me->userDiscriminator", 'whoami' => "$me->username#$me->userDiscriminator",
'noStatus' => false, 'noStatus' => false,
'isLogin' => $isLogin, 'isLogin' => $isLogin,
'isAllowed' => $isLogin && $me !== null && self::isAllowed($me->userId), 'isAllowed' => $isLogin && $me !== null && static::isAllowed($me->userId),
]); ]);
return $templates; return $templates;
} }
@ -40,10 +40,10 @@ class App
public static function checkU(): bool public static function checkU(): bool
{ {
$token = self::getU(); $token = static::getU();
if ($token === null) return false; if ($token === null) return false;
if ($token->timestamp + $token->expires_in < time()) return false; if ($token->timestamp + $token->expires_in < time()) return false;
return self::me() !== null; return static::me() !== null;
} }
public static function setU(Token $token): void public static function setU(Token $token): void
@ -80,7 +80,7 @@ class App
public static function requireAuth(): void public static function requireAuth(): void
{ {
if (!self::checkU()) { if (!static::checkU()) {
header('location: /login.php'); header('location: /login.php');
http_response_code(302); http_response_code(302);
exit; exit;
@ -89,7 +89,7 @@ class App
public static function requireNonAuth(): void public static function requireNonAuth(): void
{ {
if (self::checkU()) { if (static::checkU()) {
header('location: /'); header('location: /');
http_response_code(302); http_response_code(302);
exit; exit;
@ -98,10 +98,10 @@ class App
public static function requireAllowed(): void public static function requireAllowed(): void
{ {
self::requireAuth(); static::requireAuth();
$me = self::me(); $me = static::me();
if ($me === null || !self::isAllowed($me->userId)) { if ($me === null || !static::isAllowed($me->userId)) {
self::template([ static::template([
'title' => '您無權限使用本系統', 'title' => '您無權限使用本系統',
'body' => <<<HTML 'body' => <<<HTML
<div class="mt-5"> <div class="mt-5">
@ -117,7 +117,7 @@ HTML,
{ {
try { try {
$token = Auth::getTokenByCode($code); $token = Auth::getTokenByCode($code);
if ($token !== null) self::setU($token); if ($token !== null) static::setU($token);
} catch (Exception) { } catch (Exception) {
error_log('Failed to getTokenByCode.'); error_log('Failed to getTokenByCode.');
} }
@ -126,7 +126,7 @@ HTML,
public static function me(): ?Me public static function me(): ?Me
{ {
try { try {
$u = self::getU(); $u = static::getU();
if ($u === null) return null; if ($u === null) return null;
return Auth::getMe($u); return Auth::getMe($u);
} catch (Exception) { } catch (Exception) {
@ -139,8 +139,8 @@ HTML,
public static function template(array $params): void public static function template(array $params): void
{ {
$status = ''; $status = '';
if (self::checkU() && !$params['no_status']) { if (static::checkU() && !$params['no_status']) {
$me = self::me(); $me = static::me();
$status = <<<HTML $status = <<<HTML
<div>您已使用 <code>$me->username#$me->userDiscriminator</code> 登入,點選此處以<a href="/logout.php">登出系統</a>。</div> <div>您已使用 <code>$me->username#$me->userDiscriminator</code> 登入,點選此處以<a href="/logout.php">登出系統</a>。</div>
HTML; HTML;
@ -181,13 +181,13 @@ HTML;
public static function isAllowed(string $userId): bool public static function isAllowed(string $userId): bool
{ {
return in_array($userId, self::allowedUsers); return in_array($userId, static::allowedUsers);
} }
#[NoReturn] #[NoReturn]
public static function render(string $name, array $data = []): void public static function render(string $name, array $data = []): void
{ {
$html = self::getTemplate()->render($name, $data); $html = static::getTemplate()->render($name, $data);
header('Content-Type: text/html; charset=utf-8'); header('Content-Type: text/html; charset=utf-8');
header('Content-Length: ' . strlen($html)); header('Content-Length: ' . strlen($html));

View file

@ -42,7 +42,7 @@ class Auth
{ {
$query_string = http_build_query([ $query_string = http_build_query([
'client_id' => $_ENV['DISCORD_CLIENT_ID'], 'client_id' => $_ENV['DISCORD_CLIENT_ID'],
'redirect_uri' => self::DISCORD_REDIRECT_URI, 'redirect_uri' => static::DISCORD_REDIRECT_URI,
'response_type' => 'code', 'response_type' => 'code',
'scope' => 'identify', 'scope' => 'identify',
]); ]);
@ -60,7 +60,7 @@ class Auth
'client_secret' => $_ENV['DISCORD_CLIENT_SECRET'], 'client_secret' => $_ENV['DISCORD_CLIENT_SECRET'],
'grant_type' => 'authorization_code', 'grant_type' => 'authorization_code',
'code' => $code, 'code' => $code,
'redirect_uri' => self::DISCORD_REDIRECT_URI, 'redirect_uri' => static::DISCORD_REDIRECT_URI,
]), ]),
], ],
]))); ])));

View file

@ -29,7 +29,7 @@ abstract class FileViewer
$directory = static::directory(); $directory = static::directory();
$filename = "$directory/$file"; $filename = "$directory/$file";
if (!in_array($file, self::list()) || !file_exists($filename) || !is_file($filename)) return null; if (!in_array($file, static::list()) || !file_exists($filename) || !is_file($filename)) return null;
$extension = pathinfo($filename, PATHINFO_EXTENSION); $extension = pathinfo($filename, PATHINFO_EXTENSION);
$content = file_get_contents($filename); $content = file_get_contents($filename);