add cs fixer and PHPStan and activate it (build 436)

This commit is contained in:
crazywhalecc
2022-03-15 18:05:33 +08:00
parent d01bd69aa5
commit 1706afbcd0
163 changed files with 4572 additions and 3588 deletions

View File

@@ -1,9 +1,9 @@
<?php /** @noinspection PhpMissingReturnTypeInspection */
<?php
declare(strict_types=1);
namespace ZM\Utils;
use Swoole\Coroutine;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
@@ -18,7 +18,8 @@ use ZM\Utils\Manager\RouteManager;
class HttpUtil
{
/** @noinspection PhpMissingReturnTypeInspection */
public static function parseUri($request, $response, $uri, &$node, &$params) {
public static function parseUri($request, $response, $uri, &$node, &$params)
{
$context = new RequestContext();
$context->setMethod($request->server['request_method']);
@@ -26,8 +27,8 @@ class HttpUtil
$matcher = new UrlMatcher(RouteManager::$routes ?? new RouteCollection(), $context);
$matched = $matcher->match($uri);
} catch (ResourceNotFoundException $e) {
if (ZMConfig::get("global", "static_file_server")["status"]) {
HttpUtil::handleStaticPage($request->server["request_uri"], $response);
if (ZMConfig::get('global', 'static_file_server')['status']) {
HttpUtil::handleStaticPage($request->server['request_uri'], $response);
return null;
}
$matched = null;
@@ -36,72 +37,76 @@ class HttpUtil
}
if ($matched !== null) {
$node = [
"route" => RouteManager::$routes->get($matched["_route"])->getPath(),
"class" => $matched["_class"],
"method" => $matched["_method"],
"request_method" => $request->server['request_method']
'route' => RouteManager::$routes->get($matched['_route'])->getPath(),
'class' => $matched['_class'],
'method' => $matched['_method'],
'request_method' => $request->server['request_method'],
];
unset($matched["_class"], $matched["_method"]);
unset($matched['_class'], $matched['_method']);
$params = $matched;
return true;
} else {
return false;
}
return false;
}
public static function getHttpCodePage(int $http_code) {
if (isset(ZMConfig::get("global", "http_default_code_page")[$http_code])) {
return Coroutine::readFile(DataProvider::getResourceFolder() . "html/" . ZMConfig::get("global", "http_default_code_page")[$http_code]);
} else return null;
public static function getHttpCodePage(int $http_code)
{
if (isset(ZMConfig::get('global', 'http_default_code_page')[$http_code])) {
return Coroutine::readFile(DataProvider::getResourceFolder() . 'html/' . ZMConfig::get('global', 'http_default_code_page')[$http_code]);
}
return null;
}
/**
* @param $uri
* @param Response|\Swoole\Http\Response $response
* @param array $settings
* @param Response|\Swoole\Http\Response $response
* @return bool
*/
public static function handleStaticPage($uri, $response, $settings = []) {
$base_dir = $settings["document_root"] ?? ZMConfig::get("global", "static_file_server")["document_root"];
$base_index = $settings["document_index"] ?? ZMConfig::get("global", "static_file_server")["document_index"];
public static function handleStaticPage($uri, $response, array $settings = [])
{
$base_dir = $settings['document_root'] ?? ZMConfig::get('global', 'static_file_server')['document_root'];
$base_index = $settings['document_index'] ?? ZMConfig::get('global', 'static_file_server')['document_index'];
$path = realpath($base_dir . urldecode($uri));
if ($path !== false) {
if (is_dir($path)) $path = $path . '/';
if (is_dir($path)) {
$path = $path . '/';
}
$work = realpath($base_dir) . '/';
if (strpos($path, $work) !== 0) {
Console::info("[403] " . $uri);
Console::info('[403] ' . $uri);
self::responseCodePage($response, 403);
return true;
}
if (is_dir($path)) {
if (mb_substr($uri, -1, 1) != "/") {
Console::info("[302] " . $uri);
$response->redirect($uri . "/", 302);
if (mb_substr($uri, -1, 1) != '/') {
Console::info('[302] ' . $uri);
$response->redirect($uri . '/', 302);
return true;
}
foreach ($base_index as $vp) {
if (is_file($path . "/" . $vp)) {
Console::info("[200] " . $uri);
$exp = strtolower(pathinfo($path . $vp)['extension'] ?? "unknown");
$response->setHeader("Content-Type", ZMConfig::get("file_header")[$exp] ?? "application/octet-stream");
if (is_file($path . '/' . $vp)) {
Console::info('[200] ' . $uri);
$exp = strtolower(pathinfo($path . $vp)['extension'] ?? 'unknown');
$response->setHeader('Content-Type', ZMConfig::get('file_header')[$exp] ?? 'application/octet-stream');
$response->end(file_get_contents($path . $vp));
return true;
}
}
} elseif (is_file($path)) {
Console::info("[200] " . $uri);
$exp = strtolower(pathinfo($path)['extension'] ?? "unknown");
$response->setHeader("Content-Type", ZMConfig::get("file_header")[$exp] ?? "application/octet-stream");
Console::info('[200] ' . $uri);
$exp = strtolower(pathinfo($path)['extension'] ?? 'unknown');
$response->setHeader('Content-Type', ZMConfig::get('file_header')[$exp] ?? 'application/octet-stream');
$response->end(file_get_contents($path));
return true;
}
}
Console::info("[404] " . $uri);
Console::info('[404] ' . $uri);
self::responseCodePage($response, 404);
return true;
}
public static function responseCodePage($response, $code) {
public static function responseCodePage($response, $code)
{
$response->status($code);
$response->end(self::getHttpCodePage($code));
}