改名
This commit is contained in:
@@ -15,7 +15,6 @@ use Database\Connection;
|
||||
use Exception;
|
||||
use Http\Context\HttpHeaders;
|
||||
use Http\Context\HttpParams;
|
||||
use Http\Context\Response;
|
||||
use Http\Route\Router;
|
||||
use Server\Server;
|
||||
use Http\Shutdown;
|
||||
@@ -431,8 +430,6 @@ abstract class BaseApplication extends Component
|
||||
|
||||
/**
|
||||
* @param $array
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
private function setComponents($array): void
|
||||
{
|
||||
@@ -443,8 +440,6 @@ abstract class BaseApplication extends Component
|
||||
/**
|
||||
* @param $id
|
||||
* @param $definition
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
public function set($id, $definition): void
|
||||
{
|
||||
@@ -455,8 +450,6 @@ abstract class BaseApplication extends Component
|
||||
/**
|
||||
* @param $id
|
||||
* @return bool
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function has($id): bool
|
||||
{
|
||||
@@ -483,7 +476,6 @@ abstract class BaseApplication extends Component
|
||||
'jwt' => ['class' => Jwt::class],
|
||||
'async' => ['class' => Async::class],
|
||||
'kafka-container' => ['class' => KafkaProvider::class],
|
||||
'response' => ['class' => Response::class],
|
||||
'shutdown' => ['class' => Shutdown::class],
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -5,10 +5,10 @@ namespace Kiri\Abstracts;
|
||||
|
||||
|
||||
use Annotation\Annotation as SAnnotation;
|
||||
use Database\Connection;
|
||||
use Database\DatabasesProviders;
|
||||
use Http\Client\Client;
|
||||
use Http\Client\Curl;
|
||||
use Http\Context\Response;
|
||||
use Http\HttpFilter;
|
||||
use Http\Route\Router;
|
||||
use Server\Server;
|
||||
@@ -25,7 +25,6 @@ use Kiri\Jwt\Jwt;
|
||||
* @property Router $router
|
||||
* @property \Redis|Redis $redis
|
||||
* @property Server $server
|
||||
* @property Response $response
|
||||
* @property DatabasesProviders $db
|
||||
* @property Async $async
|
||||
* @property Logger $logger
|
||||
@@ -33,7 +32,7 @@ use Kiri\Jwt\Jwt;
|
||||
* @property SAnnotation $annotation
|
||||
* @property BaseGoto $goto
|
||||
* @property Client $client
|
||||
* @property \Database\Connection $databases
|
||||
* @property Connection $databases
|
||||
* @property Curl $curl
|
||||
* @property Producer $crontab
|
||||
* @property HttpFilter $filter
|
||||
|
||||
@@ -13,7 +13,6 @@ namespace Kiri;
|
||||
use Closure;
|
||||
use Database\DatabasesProviders;
|
||||
use Exception;
|
||||
use Http\Context\Response;
|
||||
use Kiri\Abstracts\BaseApplication;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Abstracts\Kernel;
|
||||
@@ -22,7 +21,6 @@ use Kiri\Exception\NotFindClassException;
|
||||
use Kiri\FileListen\FileChangeCustomProcess;
|
||||
use ReflectionException;
|
||||
use Server\Events\OnBeforeCommandExecute;
|
||||
use Server\ResponseInterface;
|
||||
use Server\ServerCommand;
|
||||
use Server\ServerProviders;
|
||||
use stdClass;
|
||||
@@ -57,7 +55,6 @@ class Application extends BaseApplication
|
||||
|
||||
|
||||
/**
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
@@ -246,19 +243,6 @@ class Application extends BaseApplication
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return Response|ResponseInterface
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
private function getBuilder($data): Response|ResponseInterface
|
||||
{
|
||||
return di(Response::class)->getBuilder($data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $className
|
||||
* @param null $abstracts
|
||||
|
||||
+5
-8
@@ -5,7 +5,6 @@ defined('APP_PATH') or define('APP_PATH', realpath(__DIR__ . '/../../'));
|
||||
|
||||
use Annotation\Annotation;
|
||||
use Http\Context\Context;
|
||||
use Http\Context\Response as HttpResponse;
|
||||
use Http\Route\Router;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Kiri\Abstracts\Config;
|
||||
@@ -22,8 +21,10 @@ use Kiri\Kiri;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Server\Constrict\Request;
|
||||
use Server\Constrict\Response;
|
||||
use Server\ResponseInterface;
|
||||
use Server\ServerManager;
|
||||
use Swoole\WebSocket\Server;
|
||||
use Server\Message\Response as Par7Response;
|
||||
|
||||
if (!function_exists('make')) {
|
||||
|
||||
@@ -841,10 +842,10 @@ if (!function_exists('response')) {
|
||||
*/
|
||||
function response(): Response
|
||||
{
|
||||
if (!Context::hasContext(HttpResponse::class)) {
|
||||
Context::setContext(HttpResponse::class, new HttpResponse());
|
||||
if (!Context::hasContext(ResponseInterface::class)) {
|
||||
Context::setContext(ResponseInterface::class, new Par7Response());
|
||||
}
|
||||
return di(Response::class);
|
||||
return di(ResponseInterface::class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -898,8 +899,6 @@ if (!function_exists('di')) {
|
||||
/**
|
||||
* @param string $className
|
||||
* @return mixed
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
function di(string $className): mixed
|
||||
{
|
||||
@@ -914,8 +913,6 @@ if (!function_exists('duplicate')) {
|
||||
/**
|
||||
* @param string $className
|
||||
* @return mixed
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
function duplicate(string $className): mixed
|
||||
{
|
||||
|
||||
@@ -1,372 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/4/24 0024
|
||||
* Time: 19:39
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Http\Context;
|
||||
|
||||
use Exception;
|
||||
use Http\Abstracts\HttpService;
|
||||
use Http\Context\Formatter\FileFormatter;
|
||||
use Http\Context\Formatter\HtmlFormatter;
|
||||
use Http\Context\Formatter\JsonFormatter;
|
||||
use Http\Context\Formatter\XmlFormatter;
|
||||
use Http\IInterface\IFormatter;
|
||||
use Kiri\Exception\NotFindClassException;
|
||||
use Kiri\Kiri;
|
||||
use Server\ResponseInterface;
|
||||
use Server\ServerManager;
|
||||
use Swoole\Http\Response as SResponse;
|
||||
|
||||
/**
|
||||
* Class Response
|
||||
* @package Kiri\Kiri\Http
|
||||
*/
|
||||
class Response extends HttpService implements ResponseInterface
|
||||
{
|
||||
|
||||
const JSON = 'json';
|
||||
const XML = 'xml';
|
||||
const HTML = 'html';
|
||||
const FILE = 'file';
|
||||
|
||||
/** @var ?string */
|
||||
private ?string $format = null;
|
||||
|
||||
/** @var int */
|
||||
public int $statusCode = 200;
|
||||
|
||||
public array $headers = [];
|
||||
public array $cookies = [];
|
||||
|
||||
private float $startTime = 0;
|
||||
|
||||
private mixed $endData;
|
||||
|
||||
private array $_clientInfo = [];
|
||||
|
||||
const FORMAT_MAPS = [
|
||||
self::JSON => JsonFormatter::class,
|
||||
self::XML => XmlFormatter::class,
|
||||
self::HTML => HtmlFormatter::class,
|
||||
self::FILE => FileFormatter::class,
|
||||
];
|
||||
|
||||
public int $fd = 0;
|
||||
private int $clientId = 0;
|
||||
private int $reactorId = 0;
|
||||
|
||||
|
||||
/**
|
||||
* @param int $int
|
||||
* @param int $reID
|
||||
*/
|
||||
public function setClientId(int $int, int $reID = null): static
|
||||
{
|
||||
$this->clientId = $int;
|
||||
$manager = Kiri::getDi()->get(ServerManager::class);
|
||||
$this->_clientInfo = $manager->getServer()->getClientInfo($int);
|
||||
if (!empty($this->reactorId)) {
|
||||
$this->reactorId = $reID;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @return $this
|
||||
*/
|
||||
public function withAccessControlAllowHeaders($value): static
|
||||
{
|
||||
$this->headers['Access-Control-Allow-Headers'] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @return $this
|
||||
*/
|
||||
public function withAccessControlRequestMethod($value): static
|
||||
{
|
||||
$this->headers['Access-Control-Request-Method'] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @return $this
|
||||
*/
|
||||
public function withAccessControlAllowOrigin($value): static
|
||||
{
|
||||
$this->headers['Access-Control-Allow-Origin'] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $clientInfo
|
||||
*/
|
||||
public function setClientInfo(array $clientInfo)
|
||||
{
|
||||
$this->_clientInfo = $clientInfo;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @throws
|
||||
*/
|
||||
public function getClientInfo(): mixed
|
||||
{
|
||||
if (!empty($this->_clientInfo)) {
|
||||
return $this->_clientInfo;
|
||||
}
|
||||
$server = di(ServerManager::class)->getServer();
|
||||
return $server->getClientInfo($this->clientId, $this->reactorId);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getFormat(): string
|
||||
{
|
||||
return $this->format;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getClientId(): int
|
||||
{
|
||||
return $this->clientId;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $format
|
||||
* @return $this
|
||||
*/
|
||||
public function setFormat($format): static
|
||||
{
|
||||
if (empty($format)) {
|
||||
return $this;
|
||||
}
|
||||
$this->format = $format;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $content
|
||||
* @return Response
|
||||
*/
|
||||
public function toHtml($content): static
|
||||
{
|
||||
$this->format = self::HTML;
|
||||
$this->endData = (string)$content;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $content
|
||||
* @return Response
|
||||
*/
|
||||
public function toJson($content): static
|
||||
{
|
||||
$this->format = self::JSON;
|
||||
$this->endData = json_encode($content, JSON_UNESCAPED_UNICODE);
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $content
|
||||
* @return mixed
|
||||
*/
|
||||
public function toXml($content): static
|
||||
{
|
||||
$this->format = self::XML;
|
||||
$this->endData = $content;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param bool $isChunk
|
||||
* @param int $offset
|
||||
* @param int $limit
|
||||
* @return static
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendFile(string $path, bool $isChunk = false, int $offset = 0, int $limit = 10240): static
|
||||
{
|
||||
$this->format = self::FILE;
|
||||
if (!file_exists($path)) {
|
||||
throw new Exception('File `' . $path . '` not exists.');
|
||||
}
|
||||
$this->endData = ['path' => $path, 'isChunk' => $isChunk, 'limit' => $limit, 'offset' => $offset];
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $value
|
||||
* @return Response
|
||||
*/
|
||||
public function addHeader($key, $value): static
|
||||
{
|
||||
$this->headers[$key] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param null $value
|
||||
* @param null $expires
|
||||
* @param null $path
|
||||
* @param null $domain
|
||||
* @param null $secure
|
||||
* @param null $httponly
|
||||
* @param null $samesite
|
||||
* @param null $priority
|
||||
* @return Response
|
||||
*/
|
||||
public function addCookie($name, $value = null, $expires = null, $path = null, $domain = null, $secure = null, $httponly = null, $samesite = null, $priority = null): static
|
||||
{
|
||||
$this->cookies[] = func_get_args();
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $statusCode
|
||||
* @return Response
|
||||
*/
|
||||
public function setStatusCode($statusCode): static
|
||||
{
|
||||
$this->statusCode = $statusCode;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getResponseFormat(): string
|
||||
{
|
||||
return match ($this->format) {
|
||||
Response::HTML => 'text/html;charset=utf-8',
|
||||
Response::FILE => 'application/octet-stream',
|
||||
Response::XML => 'application/xml;charset=utf-8',
|
||||
default => 'application/json;charset=utf-8',
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
* @param SResponse|null $response
|
||||
* @return Response
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getBuilder(mixed $data, SResponse $response = null): ResponseInterface
|
||||
{
|
||||
if ($response != null) {
|
||||
$this->configure($response);
|
||||
}
|
||||
return $this->setContent($data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param SResponse $response
|
||||
* @return Response
|
||||
* @throws Exception
|
||||
*/
|
||||
public function configure(SResponse $response): static
|
||||
{
|
||||
$response->setStatusCode($this->statusCode);
|
||||
$response->header('Run-Time', $this->getRuntime());
|
||||
if (!empty($this->headers)) {
|
||||
foreach ($this->headers as $name => $header) {
|
||||
$response->header($name, $header);
|
||||
}
|
||||
}
|
||||
if (!empty($this->cookies)) {
|
||||
foreach ($this->cookies as $header) {
|
||||
$response->setCookie(...$header);
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $content
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function setContent(mixed $content): ResponseInterface
|
||||
{
|
||||
$this->endData = $content;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return IFormatter
|
||||
* @throws NotFindClassException
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function getContent(): IFormatter
|
||||
{
|
||||
$class = Response::FORMAT_MAPS[$this->format] ?? HtmlFormatter::class;
|
||||
return \di($class)->send($this->endData);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @param array $param
|
||||
* @return int
|
||||
*/
|
||||
public function redirect($url, array $param = []): mixed
|
||||
{
|
||||
if (!empty($param)) {
|
||||
$url .= '?' . http_build_query($param);
|
||||
}
|
||||
$url = ltrim($url, '/');
|
||||
if (!preg_match('/^http/', $url)) {
|
||||
$url = '/' . $url;
|
||||
}
|
||||
/** @var SResponse $response */
|
||||
$response = Context::getContext('response');
|
||||
if (!empty($response)) {
|
||||
return $response->redirect($url, 302);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getRuntime(): string
|
||||
{
|
||||
return sprintf('%.5f', microtime(TRUE) - request()->getStartTime());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,7 +7,6 @@ namespace Http\Route;
|
||||
|
||||
use Closure;
|
||||
use Exception;
|
||||
use Http\Context\Response;
|
||||
use Server\RequestInterface;
|
||||
|
||||
/**
|
||||
@@ -23,16 +22,13 @@ class CoreMiddleware extends MiddlewareAbstracts
|
||||
* @param RequestInterface $request
|
||||
* @param Closure $next
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandler(RequestInterface $request, Closure $next): mixed
|
||||
{
|
||||
/** @var Response $response */
|
||||
$response = \response();
|
||||
$response->withAccessControlAllowOrigin('*')
|
||||
->withAccessControlRequestMethod($request->getAccessControlRequestMethod())
|
||||
->withAccessControlAllowHeaders($request->getAccessControlAllowHeaders());
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@ use Annotation\Inject;
|
||||
use Closure;
|
||||
use Exception;
|
||||
use Http\Abstracts\HttpService;
|
||||
use Http\Context\Response;
|
||||
use Http\Controller;
|
||||
use Http\IInterface\MiddlewareInterface;
|
||||
use Http\IInterface\RouterInterface;
|
||||
@@ -42,8 +41,6 @@ class Router extends HttpService implements RouterInterface
|
||||
|
||||
public int $useTree = ROUTER_TREE;
|
||||
|
||||
public ?Response $response = null;
|
||||
|
||||
|
||||
/**
|
||||
* @var RequestInterface
|
||||
@@ -69,8 +66,6 @@ class Router extends HttpService implements RouterInterface
|
||||
public function init()
|
||||
{
|
||||
$this->namespace = Config::get('http.namespace', $this->namespace);
|
||||
|
||||
$this->response = Kiri::app()->get('response');
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -3,10 +3,11 @@
|
||||
namespace Server\Constrict;
|
||||
|
||||
use Http\Context\Context;
|
||||
use Http\Context\Response;
|
||||
use Kiri\Kiri;
|
||||
use Server\Message\Request as RequestMessage;
|
||||
use Server\RequestInterface;
|
||||
use Server\Message\Response;
|
||||
use Server\ResponseInterface;
|
||||
|
||||
|
||||
/**
|
||||
@@ -53,7 +54,7 @@ class Request implements RequestInterface
|
||||
*/
|
||||
public static function create(\Swoole\Http\Request $request): RequestInterface
|
||||
{
|
||||
Context::setContext(Response::class, new Response());
|
||||
Context::setContext(ResponseInterface::class, new Response());
|
||||
|
||||
Context::setContext(RequestMessage::class, RequestMessage::parseRequest($request));
|
||||
|
||||
|
||||
@@ -29,10 +29,7 @@ class Response implements ResponseInterface
|
||||
*/
|
||||
public function __call($name, $args)
|
||||
{
|
||||
if (!method_exists($this, $name)) {
|
||||
return $this->__call__()->{$name}(...$args);
|
||||
}
|
||||
return $this->{$name}(...$args);
|
||||
return $this->__call__()->{$name}(...$args);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -50,6 +47,6 @@ class Response implements ResponseInterface
|
||||
*/
|
||||
public function __call__(): Psr7Response
|
||||
{
|
||||
return Context::getContext(Psr7Response::class, new Psr7Response());
|
||||
return Context::getContext(ResponseInterface::class, new Psr7Response());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,9 +87,9 @@ class Response implements ResponseInterface, \Server\ResponseInterface
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @return ResponseInterface
|
||||
* @return Response
|
||||
*/
|
||||
public function withAccessControlAllowHeaders($value): ResponseInterface
|
||||
public function withAccessControlAllowHeaders($value): static
|
||||
{
|
||||
return $this->withHeader('Access-Control-Allow-Headers', $value);
|
||||
}
|
||||
@@ -97,9 +97,9 @@ class Response implements ResponseInterface, \Server\ResponseInterface
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @return ResponseInterface
|
||||
* @return Response
|
||||
*/
|
||||
public function withAccessControlRequestMethod($value): ResponseInterface
|
||||
public function withAccessControlRequestMethod($value): static
|
||||
{
|
||||
return $this->withHeader('Access-Control-Request-Method', $value);
|
||||
}
|
||||
@@ -107,9 +107,9 @@ class Response implements ResponseInterface, \Server\ResponseInterface
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @return ResponseInterface
|
||||
* @return Response
|
||||
*/
|
||||
public function withAccessControlAllowOrigin($value): ResponseInterface
|
||||
public function withAccessControlAllowOrigin($value): static
|
||||
{
|
||||
return $this->withHeader('Access-Control-Allow-Origin', $value);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user