Skip to content

Commit 830c978

Browse files
author
子旭 苏
committed
Change some CRLF file to LF
1 parent f184707 commit 830c978

18 files changed

+519
-512
lines changed

src/Authorization.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
<?php
2-
namespace Swango\HttpServer;
3-
abstract class Authorization {
4-
public const AUTH_NONE = 0;
5-
private static $func = null;
6-
public static function getUidWithRole(): string {
7-
if (null === self::$func) {
8-
$user_id = \session::getUid();
9-
if (null === $user_id) {
10-
return '';
11-
}
12-
return $user_id;
13-
} else {
14-
return (self::$func)();
15-
}
16-
}
17-
public static function registerGetUidWithRoleFunc(callable $func): void {
18-
self::$func = $func;
19-
}
1+
<?php
2+
namespace Swango\HttpServer;
3+
abstract class Authorization {
4+
public const AUTH_NONE = 0;
5+
private static $func = null;
6+
public static function getUidWithRole(): string {
7+
if (null === self::$func) {
8+
$user_id = \session::getUid();
9+
if (null === $user_id) {
10+
return '';
11+
}
12+
return $user_id;
13+
} else {
14+
return (self::$func)();
15+
}
16+
}
17+
public static function registerGetUidWithRoleFunc(callable $func): void {
18+
self::$func = $func;
19+
}
2020
}
Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,58 @@
1-
<?php
2-
namespace Swango\HttpServer\Controller;
3-
class StaticResourceController extends \Swango\HttpServer\Controller {
4-
private static $file_exists_cache = [];
5-
protected static function file_exists(string $file): bool {
6-
if (array_key_exists($file, self::$file_exists_cache))
7-
return self::$file_exists_cache[$file];
8-
$result = file_exists($file);
9-
if (count(self::$file_exists_cache) > 2048) {
10-
array_shift(self::$file_exists_cache);
11-
array_shift(self::$file_exists_cache);
12-
array_shift(self::$file_exists_cache);
13-
}
14-
self::$file_exists_cache[$file] = $result;
15-
return $result;
16-
}
17-
protected function handle(): void {
18-
$uri = $this->swoole_http_request->server['request_uri'];
19-
$type = substr($uri, strrpos($uri, '.') + 1);
20-
if (! array_key_exists($type, \Swlib\Http\ContentType::MAP)) {
21-
$this->E_404();
22-
return;
23-
}
24-
static $appdir;
25-
if (! isset($appdir))
26-
$appdir = substr(\Swango\Environment::getDir()->app, 0, - 1);
27-
$file = $appdir . $uri;
28-
29-
if (array_key_exists('accept-encoding', $this->swoole_http_request->header) &&
30-
strpos($this->swoole_http_request->header['accept-encoding'], 'gzip') !== false) {
31-
$gz_file = "$file.gz";
32-
if (self::file_exists($gz_file)) {
33-
$this->swoole_http_response->header('Cache-Control', 'max-age=315360000');
34-
$this->swoole_http_response->header('Content-Encoding', 'gzip');
35-
$this->swoole_http_response->header('Content-Type', \Swlib\Http\ContentType::MAP[$type]);
36-
if ($this->method === 'GET')
37-
$this->swoole_http_response->sendfile($gz_file);
38-
else
39-
$this->swoole_http_response->end();
40-
41-
$this->json_response_code = 200;
42-
$this->response_finished = true;
43-
return;
44-
}
45-
}
46-
if (! self::file_exists($file)) {
47-
self::E_404();
48-
return;
49-
}
50-
$this->swoole_http_response->header('Content-Type', \Swlib\Http\ContentType::MAP[$type]);
51-
if ($this->method === 'GET')
52-
$this->swoole_http_response->sendfile($file);
53-
else
54-
$this->swoole_http_response->end();
55-
$this->json_response_code = 200;
56-
$this->response_finished = true;
57-
}
1+
<?php
2+
namespace Swango\HttpServer\Controller;
3+
class StaticResourceController extends \Swango\HttpServer\Controller {
4+
private static $file_exists_cache = [];
5+
protected static function file_exists(string $file): bool {
6+
if (array_key_exists($file, self::$file_exists_cache))
7+
return self::$file_exists_cache[$file];
8+
$result = file_exists($file);
9+
if (count(self::$file_exists_cache) > 2048) {
10+
array_shift(self::$file_exists_cache);
11+
array_shift(self::$file_exists_cache);
12+
array_shift(self::$file_exists_cache);
13+
}
14+
self::$file_exists_cache[$file] = $result;
15+
return $result;
16+
}
17+
protected function handle(): void {
18+
$uri = $this->swoole_http_request->server['request_uri'];
19+
$type = substr($uri, strrpos($uri, '.') + 1);
20+
if (! array_key_exists($type, \Swlib\Http\ContentType::MAP)) {
21+
$this->E_404();
22+
return;
23+
}
24+
static $appdir;
25+
if (! isset($appdir))
26+
$appdir = substr(\Swango\Environment::getDir()->app, 0, - 1);
27+
$file = $appdir . $uri;
28+
29+
if (array_key_exists('accept-encoding', $this->swoole_http_request->header) &&
30+
strpos($this->swoole_http_request->header['accept-encoding'], 'gzip') !== false) {
31+
$gz_file = "$file.gz";
32+
if (self::file_exists($gz_file)) {
33+
$this->swoole_http_response->header('Cache-Control', 'max-age=315360000');
34+
$this->swoole_http_response->header('Content-Encoding', 'gzip');
35+
$this->swoole_http_response->header('Content-Type', \Swlib\Http\ContentType::MAP[$type]);
36+
if ($this->method === 'GET')
37+
$this->swoole_http_response->sendfile($gz_file);
38+
else
39+
$this->swoole_http_response->end();
40+
41+
$this->json_response_code = 200;
42+
$this->response_finished = true;
43+
return;
44+
}
45+
}
46+
if (! self::file_exists($file)) {
47+
self::E_404();
48+
return;
49+
}
50+
$this->swoole_http_response->header('Content-Type', \Swlib\Http\ContentType::MAP[$type]);
51+
if ($this->method === 'GET')
52+
$this->swoole_http_response->sendfile($file);
53+
else
54+
$this->swoole_http_response->end();
55+
$this->json_response_code = 200;
56+
$this->response_finished = true;
57+
}
5858
}

src/ExceptionToResponse.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
<?php
2-
class ExceptionToResponse extends Exception implements ExceptionToResponse\ExceptionToResponseInterface {
3-
const HEADER_STR = '200 OK';
4-
private ?string $cnmsg;
5-
private $data;
6-
public function __construct(?string $enmsg = null, ?string $cnmsg = null, int $code = 200, $data = null, $retry_time = null) {
7-
$this->cnmsg = $cnmsg;
8-
$this->data = $data;
9-
if (isset($retry_time)) {
10-
$this->retryTime = $retry_time;
11-
}
12-
parent::__construct($enmsg, $code);
13-
}
14-
public function getCnMsg() {
15-
return $this->cnmsg;
16-
}
17-
public function getData() {
18-
return $this->data;
19-
}
1+
<?php
2+
class ExceptionToResponse extends Exception implements ExceptionToResponse\ExceptionToResponseInterface {
3+
const HEADER_STR = '200 OK';
4+
private ?string $cnmsg;
5+
private $data;
6+
public function __construct(?string $enmsg = null, ?string $cnmsg = null, int $code = 200, $data = null, $retry_time = null) {
7+
$this->cnmsg = $cnmsg;
8+
$this->data = $data;
9+
if (isset($retry_time)) {
10+
$this->retryTime = $retry_time;
11+
}
12+
parent::__construct($enmsg, $code);
13+
}
14+
public function getCnMsg() {
15+
return $this->cnmsg;
16+
}
17+
public function getData() {
18+
return $this->data;
19+
}
2020
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
<?php
2-
namespace ExceptionToResponse;
3-
class BadRequestException extends \ExceptionToResponse {
4-
const HEADER_STR = '400 Bad Request';
5-
public function __construct($enmsg = null, $cnmsg = null, $code = 200) {
6-
parent::__construct('Bad request', '非法的请求', 400);
7-
}
1+
<?php
2+
namespace ExceptionToResponse;
3+
class BadRequestException extends \ExceptionToResponse {
4+
const HEADER_STR = '400 Bad Request';
5+
public function __construct($enmsg = null, $cnmsg = null, $code = 200) {
6+
parent::__construct('Bad request', '非法的请求', 400);
7+
}
88
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
<?php
2-
namespace ExceptionToResponse;
3-
class InsufficientPermissionsException extends \ExceptionToResponse {
4-
const HEADER_STR = '403 Forbidden';
5-
public function __construct($enmsg = null, $cnmsg = null, $code = 200) {
6-
parent::__construct('Insufficient permissions', '登录状态失效,请重新授权', 401);
7-
}
1+
<?php
2+
namespace ExceptionToResponse;
3+
class InsufficientPermissionsException extends \ExceptionToResponse {
4+
const HEADER_STR = '403 Forbidden';
5+
public function __construct($enmsg = null, $cnmsg = null, $code = 200) {
6+
parent::__construct('Insufficient permissions', '登录状态失效,请重新授权', 401);
7+
}
88
}
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
<?php
2-
namespace ExceptionToResponse;
3-
class TimeTooDifferentException extends \ExceptionToResponse {
4-
const HEADER_STR = '450 Time Too Different';
5-
public function __construct($enmsg = null, $cnmsg = null, $code = 200) {
6-
parent::__construct('Time too different', '本地时间与服务器时间相差太多,请调整本地时间', 450,
7-
[
8-
'servertime' => (int)(microtime(true) * 1000)
9-
]);
10-
}
11-
public function getTraceToRecord() {
12-
return '';
13-
}
1+
<?php
2+
namespace ExceptionToResponse;
3+
class TimeTooDifferentException extends \ExceptionToResponse {
4+
const HEADER_STR = '450 Time Too Different';
5+
public function __construct($enmsg = null, $cnmsg = null, $code = 200) {
6+
parent::__construct('Time too different', '本地时间与服务器时间相差太多,请调整本地时间', 450,
7+
[
8+
'servertime' => (int)(microtime(true) * 1000)
9+
]);
10+
}
11+
public function getTraceToRecord() {
12+
return '';
13+
}
1414
}

src/Validator.php

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,68 @@
1-
<?php
2-
namespace Swango\HttpServer;
3-
abstract class Validator {
4-
private bool $optional = false, $could_be_null = false;
5-
protected string $cnkey;
6-
protected ?string $cnmsg;
7-
protected \SplQueue $validate_function;
8-
public function __construct(?string $cnkey) {
9-
$this->cnkey = $cnkey ?? '';
10-
$this->validate_function = new \SplQueue();
11-
}
12-
/**
13-
* 设置为非必须项
14-
*/
15-
public function optional(): self {
16-
$this->optional = true;
17-
return $this;
18-
}
19-
public function isOptional(): bool {
20-
return $this->optional;
21-
}
22-
/**
23-
* 表示可能为null,默认为不能为null
24-
*/
25-
public function null(): self {
26-
$this->could_be_null = true;
27-
return $this;
28-
}
29-
public function couldBeNull(): bool {
30-
return $this->could_be_null;
31-
}
32-
public function setCnMsg($cnmsg) {
33-
$this->cnmsg = $cnmsg;
34-
return $this;
35-
}
36-
public function getCnMsg() {
37-
return $this->cnmsg;
38-
}
39-
public function getCnKey() {
40-
return $this->cnkey;
41-
}
42-
public function attachValidateFunction($function) {
43-
$this->validate_function->enqueue($function);
44-
return $this;
45-
}
46-
abstract protected function check(?string $key, &$value): void;
47-
public function validate($key, &$value) {
48-
if (! isset($value) || $value === '') {
49-
if ($this->couldBeNull()) {
50-
if ($value === '') {
51-
$value = null;
52-
}
53-
return;
54-
} else {
55-
throw new \ExceptionToResponse\InvalidParameterException('Invalid ' . $key, $this->getCnMsg());
56-
}
57-
}
58-
$this->check($key, $value);
59-
if (method_exists($this, 'validate_function')) {
60-
$this->validate_function($value);
61-
}
62-
while (! $this->validate_function->isEmpty()) {
63-
$func = $this->validate_function->dequeue();
64-
$func($value);
65-
unset($func);
66-
}
67-
}
1+
<?php
2+
namespace Swango\HttpServer;
3+
abstract class Validator {
4+
private bool $optional = false, $could_be_null = false;
5+
protected string $cnkey;
6+
protected ?string $cnmsg;
7+
protected \SplQueue $validate_function;
8+
public function __construct(?string $cnkey) {
9+
$this->cnkey = $cnkey ?? '';
10+
$this->validate_function = new \SplQueue();
11+
}
12+
/**
13+
* 设置为非必须项
14+
*/
15+
public function optional(): self {
16+
$this->optional = true;
17+
return $this;
18+
}
19+
public function isOptional(): bool {
20+
return $this->optional;
21+
}
22+
/**
23+
* 表示可能为null,默认为不能为null
24+
*/
25+
public function null(): self {
26+
$this->could_be_null = true;
27+
return $this;
28+
}
29+
public function couldBeNull(): bool {
30+
return $this->could_be_null;
31+
}
32+
public function setCnMsg($cnmsg) {
33+
$this->cnmsg = $cnmsg;
34+
return $this;
35+
}
36+
public function getCnMsg() {
37+
return $this->cnmsg;
38+
}
39+
public function getCnKey() {
40+
return $this->cnkey;
41+
}
42+
public function attachValidateFunction($function) {
43+
$this->validate_function->enqueue($function);
44+
return $this;
45+
}
46+
abstract protected function check(?string $key, &$value): void;
47+
public function validate($key, &$value) {
48+
if (! isset($value) || $value === '') {
49+
if ($this->couldBeNull()) {
50+
if ($value === '') {
51+
$value = null;
52+
}
53+
return;
54+
} else {
55+
throw new \ExceptionToResponse\InvalidParameterException('Invalid ' . $key, $this->getCnMsg());
56+
}
57+
}
58+
$this->check($key, $value);
59+
if (method_exists($this, 'validate_function')) {
60+
$this->validate_function($value);
61+
}
62+
while (! $this->validate_function->isEmpty()) {
63+
$func = $this->validate_function->dequeue();
64+
$func($value);
65+
unset($func);
66+
}
67+
}
6868
}

0 commit comments

Comments
 (0)