This commit is contained in:
2021-09-22 11:36:29 +08:00
parent 07e919a2d5
commit e3bf41c7c5
3 changed files with 93 additions and 80 deletions
+2 -1
View File
@@ -8,6 +8,7 @@ use Http\Handler\Context;
use Http\Message\ContentType;
use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Config;
use Kiri\Exception\ConfigException;
use Kiri\Kiri;
use Psr\Http\Message\StreamInterface;
use Http\Message\ServerRequest as RequestMessage;
@@ -30,7 +31,7 @@ class Response implements ResponseInterface
/**
* @throws \Kiri\Exception\ConfigException
* @throws ConfigException
*/
public function __construct()
{
+91 -62
View File
@@ -4,10 +4,15 @@
namespace Server\Task;
use ReflectionException;
use Server\SInterface\TaskExecute;
use Kiri\Exception\NotFindClassException;
use Kiri\Abstracts\Config;
use Kiri\Exception\ConfigException;
use Kiri\Kiri;
use ReflectionException;
use Server\Constrict\Response;
use Server\Constrict\ResponseInterface;
use Server\ExceptionHandlerDispatcher;
use Server\ExceptionHandlerInterface;
use Server\SInterface\TaskExecute;
use Swoole\Server;
@@ -19,71 +24,95 @@ class OnServerTask
{
/**
* @param Server $server
* @param int $task_id
* @param int $src_worker_id
* @param mixed $data
*/
public function onTask(Server $server, int $task_id, int $src_worker_id, mixed $data)
{
try {
$data = $this->resolve($data);
} catch (\Throwable $exception) {
$data = [$exception->getMessage()];
} finally {
$server->finish($data);
}
}
/**
* @var ExceptionHandlerInterface|null
*/
public ?ExceptionHandlerInterface $handler = null;
/**
* @param Server $server
* @param Server\Task $task
*/
public function onCoroutineTask(Server $server, Server\Task $task)
{
try {
$data = $this->resolve($task->data);
} catch (\Throwable $exception) {
$data = [$exception->getMessage()];
} finally {
$server->finish($data);
}
}
/**
* @throws ConfigException
*/
public function emit(\Throwable $exception, Response $response): ResponseInterface
{
if ($this->handler == null) {
$exceptionHandler = Config::get('exception.task', ExceptionHandlerDispatcher::class);
if (!in_array(ExceptionHandlerInterface::class, class_implements($exceptionHandler))) {
$exceptionHandler = ExceptionHandlerDispatcher::class;
}
$this->handler = Kiri::getDi()->get($exceptionHandler);
}
return $this->handler->emit($exception, $response);
}
/**
* @param $data
* @return null
* @throws ReflectionException
*/
private function resolve($data)
{
[$class, $params] = json_encode($data, true);
$reflect = Kiri::getDi()->getReflect($class);
if (!$reflect->isInstantiable()) {
return null;
}
$class = $reflect->newInstanceArgs($params);
return $class->execute();
}
/**
* @param Server $server
* @param int $task_id
* @param int $src_worker_id
* @param mixed $data
* @throws ConfigException
*/
public function onTask(Server $server, int $task_id, int $src_worker_id, mixed $data)
{
try {
$data = $this->resolve($data);
} catch (\Throwable $exception) {
$data = $this->emit($exception, new Response());
} finally {
$server->finish($data);
}
}
/**
* @param Server $server
* @param int $task_id
* @param mixed $data
*/
public function onFinish(Server $server, int $task_id, mixed $data)
{
if (!($data instanceof TaskExecute)) {
return;
}
$data->finish($server, $task_id);
}
/**
* @param Server $server
* @param Server\Task $task
* @throws ConfigException
*/
public function onCoroutineTask(Server $server, Server\Task $task)
{
try {
$data = $this->resolve($task->data);
} catch (\Throwable $exception) {
$data = $this->emit($exception, new Response());
} finally {
$server->finish($data);
}
}
/**
* @param $data
* @return null
* @throws ReflectionException
*/
private function resolve($data)
{
[$class, $params] = json_encode($data, true);
$reflect = Kiri::getDi()->getReflect($class);
if (!$reflect->isInstantiable()) {
return null;
}
$class = $reflect->newInstanceArgs($params);
return $class->execute();
}
/**
* @param Server $server
* @param int $task_id
* @param mixed $data
*/
public function onFinish(Server $server, int $task_id, mixed $data)
{
if (!($data instanceof TaskExecute)) {
return;
}
$data->finish($server, $task_id);
}
}
-17
View File
@@ -1,17 +0,0 @@
<?php
namespace Annotation;
interface Porters
{
/**
* @return mixed
*/
public function process(): mixed;
}