改名
This commit is contained in:
@@ -16,32 +16,13 @@ namespace HttpServer\Http;
|
||||
class HttpHeaders
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private array $headers = [];
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private array $response = [];
|
||||
|
||||
/**
|
||||
* HttpHeaders constructor.
|
||||
* @param $headers
|
||||
*/
|
||||
public function __construct($headers)
|
||||
{
|
||||
$this->headers = $headers;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $uri
|
||||
*/
|
||||
public function setRequestUri(string $uri)
|
||||
{
|
||||
$this->headers['request_uri'] = $uri;
|
||||
$this->replace('request_uri', $uri);
|
||||
}
|
||||
|
||||
|
||||
@@ -50,36 +31,17 @@ class HttpHeaders
|
||||
*/
|
||||
public function setRequestMethod(string $method)
|
||||
{
|
||||
$this->headers['request_method'] = $method;
|
||||
$this->replace('request_method', $method);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $value
|
||||
*/
|
||||
public function setHeader($name, $value)
|
||||
{
|
||||
$this->response[$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $headers
|
||||
*/
|
||||
public function setHeaders(array $headers)
|
||||
{
|
||||
foreach ($headers as $key => $val) {
|
||||
$this->response[$key] = $val;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $value
|
||||
*/
|
||||
public function replace($name, $value)
|
||||
{
|
||||
$this->headers[$name] = $value;
|
||||
$this->addHeaders([$name => $value]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,7 +50,7 @@ class HttpHeaders
|
||||
*/
|
||||
public function addHeader($name, $value)
|
||||
{
|
||||
$this->headers[$name] = $value;
|
||||
$this->addHeaders([$name => $value]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -100,27 +62,20 @@ class HttpHeaders
|
||||
if (empty($headers)) {
|
||||
return $this;
|
||||
}
|
||||
if (!empty($this->headers)) {
|
||||
$headers = array_merge($this->headers, $headers);
|
||||
$request = Context::getContext('request');
|
||||
if (!empty($request->headers)) {
|
||||
$headers = array_merge($request->headers, $headers);
|
||||
}
|
||||
$this->headers = $headers;
|
||||
$request->headers = $headers;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getResponseHeaders(): array
|
||||
{
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return $this->headers;
|
||||
return $this->___call();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,10 +84,11 @@ class HttpHeaders
|
||||
*/
|
||||
public function getHeader($name): ?string
|
||||
{
|
||||
if (!isset($this->headers[$name])) {
|
||||
$headers = $this->___call();
|
||||
if (!isset($headers[$name])) {
|
||||
return null;
|
||||
}
|
||||
return $this->headers[$name];
|
||||
return $headers[$name];
|
||||
}
|
||||
|
||||
|
||||
@@ -156,7 +112,8 @@ class HttpHeaders
|
||||
*/
|
||||
public function exists($name): bool
|
||||
{
|
||||
return isset($this->headers[$name]) && $this->headers[$name] != null;
|
||||
$headers = $this->___call();
|
||||
return isset($headers[$name]) && $headers[$name] != null;
|
||||
}
|
||||
|
||||
|
||||
@@ -165,7 +122,17 @@ class HttpHeaders
|
||||
*/
|
||||
public function getHeaders(): array
|
||||
{
|
||||
return $this->headers;
|
||||
return $this->___call();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
private function ___call(): array
|
||||
{
|
||||
return Context::getContext('request')->header ?? [];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ class HttpParams
|
||||
|
||||
private string|array|null $body = [];
|
||||
|
||||
|
||||
/** @var array */
|
||||
private array $gets = [];
|
||||
|
||||
@@ -33,19 +34,13 @@ class HttpParams
|
||||
private array $files = [];
|
||||
private array $socket = [];
|
||||
|
||||
|
||||
/**
|
||||
* HttpParams constructor.
|
||||
* @param mixed $body
|
||||
* @param array|null $get
|
||||
* @param array|null $files
|
||||
* @param array $socket
|
||||
* @return mixed
|
||||
*/
|
||||
public function __construct(mixed $body, ?array $get, ?array $files, array $socket = [])
|
||||
public function getRawContent(): mixed
|
||||
{
|
||||
$this->gets = $get ?? [];
|
||||
$this->files = $files ?? [];
|
||||
$this->socket = $socket ?? [];
|
||||
$this->body = $body ?? '';
|
||||
return $this->getRequest()->rawContent();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -152,7 +147,7 @@ class HttpParams
|
||||
*/
|
||||
public function get($name, $defaultValue = null, $call = null): mixed
|
||||
{
|
||||
return $this->gets[$name] ?? $defaultValue;
|
||||
return $this->getRequest()->get[$name] ?? $defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -163,7 +158,7 @@ class HttpParams
|
||||
*/
|
||||
public function post($name, $defaultValue = null, $call = null): mixed
|
||||
{
|
||||
$data = $this->body[$name] ?? $defaultValue;
|
||||
$data = $this->getRequest()->post[$name] ?? $defaultValue;
|
||||
if ($call !== null) {
|
||||
$data = call_user_func($call, $data);
|
||||
}
|
||||
@@ -191,7 +186,7 @@ class HttpParams
|
||||
*/
|
||||
public function gets(): array
|
||||
{
|
||||
return $this->gets;
|
||||
return $this->getRequest()->get;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -199,7 +194,9 @@ class HttpParams
|
||||
*/
|
||||
#[Pure] public function params(): array
|
||||
{
|
||||
return array_merge($this->body ?? [], $this->files ?? []);
|
||||
$request = $this->getRequest();
|
||||
|
||||
return array_merge($request->post ?? [], $request->files ?? []);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -207,7 +204,8 @@ class HttpParams
|
||||
*/
|
||||
#[Pure] public function load(): array
|
||||
{
|
||||
return array_merge($this->files ?? [], $this->body ?? [], $this->gets ?? [], $this->socket ?? []);
|
||||
$request = $this->getRequest();
|
||||
return array_merge($request->files ?? [], $request->post ?? [], $request->gets ?? []);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -217,7 +215,7 @@ class HttpParams
|
||||
*/
|
||||
public function array($name, array $defaultValue = []): mixed
|
||||
{
|
||||
return $this->body[$name] ?? $defaultValue;
|
||||
return $this->getRequest()->post[$name] ?? $defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -228,10 +226,10 @@ class HttpParams
|
||||
*/
|
||||
public function file($name): File|null
|
||||
{
|
||||
if (!isset($this->files[$name])) {
|
||||
$param = $this->getRequest()->files[$name] ?? null;
|
||||
if (empty($param)) {
|
||||
return null;
|
||||
}
|
||||
$param = $this->files[$name];
|
||||
$param['class'] = File::class;
|
||||
return Snowflake::createObject($param);
|
||||
}
|
||||
@@ -244,9 +242,7 @@ class HttpParams
|
||||
*/
|
||||
private function required(string $name, bool $isNeed = false): mixed
|
||||
{
|
||||
$body = array_merge($this->body ?? [], $this->socket ?? []);
|
||||
|
||||
$int = $body[$name] ?? NULL;
|
||||
$int = $this->getRequest()->post[$name] ?? NULL;
|
||||
if (is_null($int) && $isNeed === true) {
|
||||
throw new RequestException("You need to add request parameter $name");
|
||||
}
|
||||
@@ -275,7 +271,7 @@ class HttpParams
|
||||
*/
|
||||
public function float(string $name, bool $isNeed = FALSE, int $round = 0): ?float
|
||||
{
|
||||
return (float)$this->required($name, $isNeed);
|
||||
return (float)$this->required($name, $isNeed);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -422,4 +418,13 @@ class HttpParams
|
||||
return $load[$name] ?? null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
private function getRequest(): \Swoole\Http\Request
|
||||
{
|
||||
return Context::getContext('request');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+10
-12
@@ -7,8 +7,9 @@ use Exception;
|
||||
use HttpServer\Abstracts\HttpService;
|
||||
use HttpServer\IInterface\AuthIdentity;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Snowflake\Core\Help;
|
||||
use ReflectionException;
|
||||
use Snowflake\Core\Json;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
use function router;
|
||||
|
||||
@@ -445,22 +446,19 @@ class Request extends HttpService
|
||||
|
||||
/**
|
||||
* @param \Swoole\Http\Request $request
|
||||
* @return mixed
|
||||
* @return Request
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
public static function create(\Swoole\Http\Request $request): Request
|
||||
{
|
||||
$request->header = array_merge($request->header, $request->server);
|
||||
Context::setContext('request', $request);
|
||||
/** @var Request $sRequest */
|
||||
$sRequest = Context::setContext('request', new Request());
|
||||
$sRequest = Snowflake::getDi()->get(Request::class);
|
||||
|
||||
$sRequest->fd = $request->fd;
|
||||
$sRequest->startTime = microtime(true);
|
||||
|
||||
$sRequest->params = new HttpParams(Help::toArray($request->rawContent()), $request->get, $request->files);
|
||||
if (!empty($request->post)) {
|
||||
$sRequest->params->setPosts($request->post ?? []);
|
||||
}
|
||||
|
||||
$sRequest->headers = new HttpHeaders(array_merge($request->server, $request->header));
|
||||
$sRequest->params = di(HttpParams::class);
|
||||
$sRequest->headers = di(HttpHeaders::class);
|
||||
|
||||
$sRequest->uri = $sRequest->headers->get('request_uri');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user