eee
This commit is contained in:
@@ -3,7 +3,6 @@
|
||||
namespace Kiri\Server\Abstracts;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Exception\NotFindClassException;
|
||||
use Kiri\Server\Config as SConfig;
|
||||
use Kiri\Server\Constant;
|
||||
@@ -12,11 +11,6 @@ use Kiri\Server\Events\OnShutdown;
|
||||
use Kiri\Server\Handler\OnServer;
|
||||
use Kiri\Server\ServerInterface;
|
||||
use Kiri\Server\Task\Task;
|
||||
use Kiri\Di\Inject\Container;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use ReflectionException;
|
||||
use Swoole\Server;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
|
||||
@@ -4,14 +4,12 @@ namespace Kiri\Server\Abstracts;
|
||||
|
||||
use Exception;
|
||||
use Kiri;
|
||||
use Kiri\Di\Inject\Container;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Swoole\Http\Server as HServer;
|
||||
use Swoole\Process;
|
||||
use Swoole\Server;
|
||||
use Kiri\Server\Processes\BaseProcess;
|
||||
use Kiri\Server\Constant;
|
||||
use Kiri\Server\Config;
|
||||
use Kiri\Error\StdoutLogger;
|
||||
use Swoole\WebSocket\Server as WServer;
|
||||
|
||||
trait TraitServer
|
||||
|
||||
@@ -2,9 +2,7 @@
|
||||
|
||||
namespace Kiri\Server\Events;
|
||||
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Swoole\Server;
|
||||
use Kiri;
|
||||
|
||||
/**
|
||||
*
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace Kiri\Server\Handler;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Server\Abstracts\Server;
|
||||
use Kiri\Server\Contract\OnPipeMessageInterface;
|
||||
|
||||
|
||||
@@ -1,112 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Server\Handler;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Di\Inject\Container;
|
||||
use Kiri\Di\Context;
|
||||
use Kiri\Di\Interface\ResponseEmitterInterface;
|
||||
use Kiri\Router\Base\ExceptionHandlerDispatcher;
|
||||
use Kiri\Router\Constrict\ConstrictRequest as CQ;
|
||||
use Kiri\Router\Constrict\ConstrictResponse;
|
||||
use Kiri\Router\DataGrip;
|
||||
use Kiri\Router\Interface\ExceptionHandlerInterface;
|
||||
use Kiri\Router\Interface\OnRequestInterface;
|
||||
use Kiri\Router\RouterCollector;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use ReflectionException;
|
||||
use Swoole\Http\Request;
|
||||
use Swoole\Http\Response;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* OnRequest event
|
||||
*/
|
||||
class OnRequest implements OnRequestInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var RouterCollector
|
||||
*/
|
||||
public RouterCollector $router;
|
||||
|
||||
|
||||
/**
|
||||
* @var ExceptionHandlerInterface
|
||||
*/
|
||||
public ExceptionHandlerInterface $exception;
|
||||
|
||||
|
||||
/**
|
||||
* @var ResponseEmitterInterface
|
||||
*/
|
||||
public ResponseEmitterInterface $responseEmitter;
|
||||
|
||||
|
||||
/**
|
||||
* @var ConstrictResponse
|
||||
*/
|
||||
#[Container(ConstrictResponse::class)]
|
||||
public ConstrictResponse $constrictResponse;
|
||||
|
||||
|
||||
/**
|
||||
* @param ResponseInterface $response
|
||||
* @param ContainerInterface $container
|
||||
* @param DataGrip $dataGrip
|
||||
* @throws
|
||||
*/
|
||||
public function __construct(public ResponseInterface $response, DataGrip $dataGrip)
|
||||
{
|
||||
$this->responseEmitter = $this->response->emmit;
|
||||
$exception = \config('exception.http');
|
||||
if (!in_array(ExceptionHandlerInterface::class, class_implements($exception))) {
|
||||
$exception = ExceptionHandlerDispatcher::class;
|
||||
}
|
||||
$this->exception = \Kiri::getDi()->get($exception);
|
||||
$this->router = $dataGrip->get(ROUTER_TYPE_HTTP);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @throws
|
||||
*/
|
||||
public function onRequest(Request $request, Response $response): void
|
||||
{
|
||||
/** @var CQ $PsrRequest */
|
||||
try {
|
||||
$PsrRequest = $this->initRequestAndResponse($request);
|
||||
|
||||
$PsrResponse = $this->router->query($request->server['path_info'], $request->getMethod())
|
||||
->run($PsrRequest);
|
||||
} catch (Throwable $throwable) {
|
||||
$PsrResponse = $this->exception->emit($throwable, $this->constrictResponse);
|
||||
} finally {
|
||||
$this->responseEmitter->response($PsrResponse, $response, $PsrRequest);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @return ServerRequestInterface
|
||||
*/
|
||||
public function initRequestAndResponse(Request $request): ServerRequestInterface
|
||||
{
|
||||
$response = new ConstrictResponse($this->response->contentType);
|
||||
|
||||
Context::set(ResponseInterface::class, $response);
|
||||
|
||||
return Context::set(RequestInterface::class, CQ::builder($request));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,7 +6,6 @@ use Kiri;
|
||||
use Kiri\Di\Inject\Container;
|
||||
use Kiri\Error\StdoutLogger;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use ReflectionException;
|
||||
use Kiri\Server\Abstracts\Server;
|
||||
use Kiri\Server\Events\OnBeforeShutdown;
|
||||
use Kiri\Server\Events\OnShutdown;
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
namespace Kiri\Server\Handler;
|
||||
|
||||
use Kiri;
|
||||
use ReflectionException;
|
||||
use Kiri\Server\Abstracts\Server;
|
||||
use Kiri\Server\Events\OnManagerStart;
|
||||
use Kiri\Server\Events\OnManagerStop;
|
||||
|
||||
@@ -13,7 +13,6 @@ use Kiri\Server\Events\OnWorkerExit;
|
||||
use Kiri\Server\Events\OnWorkerStart;
|
||||
use Kiri\Server\Events\OnWorkerStop;
|
||||
use Swoole\Server;
|
||||
use Kiri\Di\Inject\Container;
|
||||
use Swoole\Timer;
|
||||
use Throwable;
|
||||
use function config;
|
||||
|
||||
+1
-1
@@ -5,8 +5,8 @@ namespace Kiri\Server;
|
||||
|
||||
use Kiri\Di\Context;
|
||||
use Kiri\Router\Router;
|
||||
use Kiri\Server\Abstracts\BaseProcess;
|
||||
use Kiri\Server\Events\OnWorkerStart;
|
||||
use Kiri\Server\Processes\BaseProcess;
|
||||
use Swoole\Coroutine;
|
||||
use Swoole\Event;
|
||||
use Swoole\Process;
|
||||
|
||||
@@ -1,137 +1,132 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Server\Abstracts;
|
||||
|
||||
|
||||
use Kiri\Di\Context;
|
||||
use Kiri\Di\Inject\Container;
|
||||
use Kiri\Error\StdoutLogger;
|
||||
use Kiri\Server\Contract\OnProcessInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Swoole\Coroutine;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
abstract class BaseProcess implements OnProcessInterface
|
||||
{
|
||||
|
||||
private bool $stop = false;
|
||||
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected bool $redirect_stdin_and_stdout = FALSE;
|
||||
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected int $pipe_type = SOCK_DGRAM;
|
||||
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected bool $enable_coroutine = false;
|
||||
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected bool $enable_queue = false;
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public string $name = '';
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isEnableQueue(): bool
|
||||
{
|
||||
return $this->enable_queue;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
if (empty($this->name)) {
|
||||
$this->name = uniqid('p.');
|
||||
}
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isStop(): bool
|
||||
{
|
||||
return $this->stop;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getRedirectStdinAndStdout(): bool
|
||||
{
|
||||
return $this->redirect_stdin_and_stdout;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getPipeType(): int
|
||||
{
|
||||
return $this->pipe_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isEnableCoroutine(): bool
|
||||
{
|
||||
return $this->enable_coroutine;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function stop(): void
|
||||
{
|
||||
$this->stop = true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
abstract public function onSigterm(): static;
|
||||
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return void
|
||||
*/
|
||||
protected function onShutdown($data): void
|
||||
{
|
||||
$this->stop = true;
|
||||
$value = Context::get('waite:process:message');
|
||||
\Kiri::getLogger()->alert('Process ' . $this->getName() . ' stop');
|
||||
if (!is_null($value) && Coroutine::exists((int)$value)) {
|
||||
Coroutine::cancel((int)$value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace Kiri\Server\Processes;
|
||||
|
||||
|
||||
use Kiri\Di\Context;
|
||||
use Swoole\Coroutine;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
abstract class BaseProcess implements OnProcessInterface
|
||||
{
|
||||
|
||||
private bool $stop = false;
|
||||
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected bool $redirect_stdin_and_stdout = FALSE;
|
||||
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected int $pipe_type = SOCK_DGRAM;
|
||||
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected bool $enable_coroutine = false;
|
||||
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected bool $enable_queue = false;
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public string $name = '';
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isEnableQueue(): bool
|
||||
{
|
||||
return $this->enable_queue;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
if (empty($this->name)) {
|
||||
$this->name = uniqid('p.');
|
||||
}
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isStop(): bool
|
||||
{
|
||||
return $this->stop;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getRedirectStdinAndStdout(): bool
|
||||
{
|
||||
return $this->redirect_stdin_and_stdout;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getPipeType(): int
|
||||
{
|
||||
return $this->pipe_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isEnableCoroutine(): bool
|
||||
{
|
||||
return $this->enable_coroutine;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function stop(): void
|
||||
{
|
||||
$this->stop = true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
abstract public function onSigterm(): static;
|
||||
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return void
|
||||
*/
|
||||
protected function onShutdown($data): void
|
||||
{
|
||||
$this->stop = true;
|
||||
$value = Context::get('waite:process:message');
|
||||
\Kiri::getLogger()->alert('Process ' . $this->getName() . ' stop');
|
||||
if (!is_null($value) && Coroutine::exists((int)$value)) {
|
||||
Coroutine::cancel((int)$value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,25 +1,25 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Kiri\Server\Contract;
|
||||
|
||||
|
||||
use Swoole\Process;
|
||||
|
||||
|
||||
/**
|
||||
* Interface BaseProcess
|
||||
* @package Contract
|
||||
*/
|
||||
interface OnProcessInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param ?Process $process
|
||||
*/
|
||||
public function process(?Process $process): void;
|
||||
|
||||
|
||||
|
||||
}
|
||||
<?php
|
||||
|
||||
|
||||
namespace Kiri\Server\Processes;
|
||||
|
||||
|
||||
use Swoole\Process;
|
||||
|
||||
|
||||
/**
|
||||
* Interface BaseProcess
|
||||
* @package Contract
|
||||
*/
|
||||
interface OnProcessInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param ?Process $process
|
||||
*/
|
||||
public function process(?Process $process): void;
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -10,7 +10,6 @@ use Kiri\Events\EventDispatch;
|
||||
use Kiri\Router\Router;
|
||||
use Kiri\Server\Abstracts\AsyncServer;
|
||||
use Kiri\Server\Events\OnShutdown;
|
||||
use ReflectionException;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
|
||||
@@ -5,13 +5,8 @@ namespace Kiri\Server\Task;
|
||||
|
||||
use Kiri;
|
||||
use Kiri\Router\Base\ExceptionHandlerDispatcher;
|
||||
use Kiri\Router\DataGrip;
|
||||
use Kiri\Router\Interface\ExceptionHandlerInterface;
|
||||
use Kiri\Server\Constant;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Swoole\Server;
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace Kiri\Server\Task;
|
||||
|
||||
use Kiri\Di\Inject\Container;
|
||||
use Kiri\Server\ServerInterface;
|
||||
|
||||
class TaskExecute implements TaskInterface
|
||||
|
||||
Reference in New Issue
Block a user