This commit is contained in:
2021-08-24 18:24:46 +08:00
parent 575f4b8520
commit 24c93edf2b
110 changed files with 5 additions and 6 deletions
+81
View File
@@ -0,0 +1,81 @@
<?php
namespace Server\Constrict;
use Kiri\Exception\NotFindClassException;
use ReflectionException;
use Server\ResponseInterface;
/**
*
*/
class DownloadEmitter implements Emitter
{
const IMAGES = [
'png' => 'image/png',
'jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'ico' => 'image/vnd.microsoft.icon',
'tiff' => 'image/tiff',
'svg' => 'image/svg+xml',
];
/**
* @param mixed $response
* @param ResponseInterface $emitter
* @throws NotFindClassException
* @throws ReflectionException
*/
public function sender(mixed $response, ResponseInterface $emitter): void
{
$content = $emitter->getContent()->getData();
$explode = explode('/', $content['path']);
$response->header('Pragma', 'public');
$response->header('Expires', '0');
$response->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
$response->header('Content-Disposition', 'attachment;filename=' . end($explode));
$response->header('Content-Type', $type = get_file_extension($content['path']));
if (!in_array($type, self::IMAGES)) {
$response->header('Content-Transfer-Encoding', 'binary');
} else {
$response->end(file_get_contents($content['path']));
return;
}
if ($content['isChunk'] === false) {
$response->sendfile($content['path']);
} else {
$this->chunk($content, $response);
}
}
/**
* @param $content
* @param $response
*/
private function chunk($content, $response): void
{
$resource = fopen($content['path'], 'r');
$state = fstat($resource);
$offset = $content['offset'];
$response->header('Content-length', $state['size']);
while ($file = fread($resource, $content['limit'])) {
$response->write($file);
fseek($resource, $offset);
if ($offset >= $state['size']) {
break;
}
$offset += $content['limit'];
}
$response->end();
}
}
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace Server\Constrict;
use Server\ResponseInterface;
interface Emitter
{
/**
* @param mixed $response
* @param ResponseInterface $emitter
* @return mixed
*/
public function sender(mixed $response, ResponseInterface $emitter): void;
}
+77
View File
@@ -0,0 +1,77 @@
<?php
namespace Server\Constrict;
use Http\Context\Context;
use Http\Context\Request as HttpResponse;
use Http\Context\Response;
use ReflectionException;
use Server\RequestInterface;
use Kiri\Exception\NotFindClassException;
use Kiri\Kiri;
/**
* @mixin HttpResponse
*/
class Request implements RequestInterface
{
/**
* @param $name
* @param $args
* @return mixed
*/
public function __call($name, $args)
{
if (!Context::hasContext(HttpResponse::class)) {
$request = Context::setContext(HttpResponse::class, new HttpResponse());
} else {
$request = Context::getContext(HttpResponse::class);
}
if (property_exists($request, $name)) {
return $request->{$name};
}
return $request->{$name}(...$args);
}
/**
* @param $name
* @return mixed
*/
public function __get($name): mixed
{
// TODO: Change the autogenerated stub
return Context::getContext(HttpResponse::class)->{$name};
}
/**
* @param \Swoole\Http\Request $request
* @return Request
* @throws ReflectionException
* @throws NotFindClassException
*/
public static function create(\Swoole\Http\Request $request): RequestInterface
{
Context::setContext(Response::class, new Response());
$sRequest = new HttpResponse();
$sRequest->setHeaders(array_merge($request->header, $request->server));
$sRequest->setUri($sRequest->getRequestUri());
$sRequest->setClientId($request->fd);
$sRequest->setRawContent($request->rawContent(), $sRequest->getContentType());
$sRequest->setFiles($request->files ?? []);
$sRequest->setPosts($request->post ?? []);
$sRequest->setGets($request->get ?? []);
Context::setContext(HttpResponse::class, $sRequest);
return Kiri::getDi()->get(Request::class);
}
}
+40
View File
@@ -0,0 +1,40 @@
<?php
namespace Server\Constrict;
use Http\Context\Context;
use Http\Context\Response as HttpResponse;
use Server\ResponseInterface;
/**
* Class Response
* @package Server
* @mixin HttpResponse
*/
class Response implements ResponseInterface
{
const JSON = 'json';
const XML = 'xml';
const HTML = 'html';
const FILE = 'file';
/**
* @param $name
* @param $args
* @return mixed
*/
public function __call($name, $args)
{
if (!Context::hasContext(HttpResponse::class)) {
$context = Context::setContext(HttpResponse::class, new HttpResponse());
} else {
$context = Context::getContext(HttpResponse::class);
}
return $context->{$name}(...$args);
}
}
+38
View File
@@ -0,0 +1,38 @@
<?php
namespace Server\Constrict;
use Exception;
use Http\Context\Formatter\FileFormatter;
use Kiri\Exception\NotFindClassException;
use ReflectionException;
use Server\ResponseInterface;
use Swoole\Server;
/**
*
*/
class ResponseEmitter implements Emitter
{
/**
* @param \Swoole\Http\Response|\Swoole\Http2\Response $response
* @param ResponseInterface $emitter
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
public function sender(mixed $response, ResponseInterface $emitter): void
{
$content = $emitter->configure($response)->getContent();
if ($content instanceof FileFormatter) {
di(DownloadEmitter::class)->sender($response, $emitter);
return;
}
$response->header('Content-Type', $emitter->getResponseFormat());
$response->end($content->getData());
}
}
+34
View File
@@ -0,0 +1,34 @@
<?php
namespace Server\Constrict;
use Http\Context\Formatter\FileFormatter;
use Kiri\Exception\NotFindClassException;
use Server\ResponseInterface;
use Swoole\Server;
/**
*
*/
class TcpEmitter implements Emitter
{
/**
* @param Server $response
* @param ResponseInterface $emitter
* @throws NotFindClassException
* @throws \ReflectionException
* @throws \Exception
*/
public function sender(mixed $response, ResponseInterface $emitter): void
{
$formatter = $emitter->getContent();
if ($formatter instanceof FileFormatter) {
$response->sendfile($emitter->getClientId(), $formatter->getData());
} else {
$response->send($emitter->getClientId(), $formatter->getData());
}
}
}
+31
View File
@@ -0,0 +1,31 @@
<?php
namespace Server\Constrict;
use Kiri\Exception\NotFindClassException;
use Server\ResponseInterface;
use Swoole\Server;
/**
*
*/
class UdpEmitter implements Emitter
{
/**
* @param Server $response
* @param ResponseInterface $emitter
* @throws NotFindClassException
* @throws \ReflectionException
* @throws \Exception
*/
public function sender(mixed $response, ResponseInterface $emitter): void
{
$clientInfo = $emitter->getClientInfo();
$response->sendto($clientInfo['host'], $clientInfo['port'],
$emitter->getContent()->getData()
);
}
}
@@ -0,0 +1,27 @@
<?php
namespace Server\Constrict;
use Kiri\Exception\NotFindClassException;
use Server\ResponseInterface;
/**
*
*/
class WebSocketEmitter implements Emitter
{
/**
* @param mixed $response
* @param ResponseInterface $emitter
* @throws NotFindClassException
* @throws \ReflectionException
* @throws \Exception
*/
public function sender(mixed $response, ResponseInterface $emitter): void
{
$response->push($emitter->getClientId(), $emitter->getContent()->getData());
}
}