From 97cd1a0ebf4a84dd67047ccd0dfc75fa849b6aab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Sat, 8 Jan 2022 18:49:08 +0800 Subject: [PATCH] =?UTF-8?q?Revert=20"=E6=94=B9=E5=90=8D"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit fdf58326 --- composer.json | 7 +-- function.php | 20 ++++---- .../Annotation.php | 8 ++-- {kiri-note => kiri-annotation}/Aspect.php | 4 +- {kiri-note => kiri-annotation}/Attribute.php | 6 +-- {kiri-note => kiri-annotation}/Event.php | 4 +- .../IAnnotation.php | 4 +- {kiri-note => kiri-annotation}/Inject.php | 5 +- {kiri-note => kiri-annotation}/Loader.php | 22 +-------- {kiri-note => kiri-annotation}/Mapping.php | 2 +- .../Route/Document.php | 6 +-- .../Route/Middleware.php | 6 +-- .../Route/Route.php | 4 +- .../Route/Socket.php | 6 +-- {kiri-note => kiri-annotation}/Target.php | 4 +- {kiri-note => kiri-annotation}/Task.php | 4 +- kiri-engine/Abstracts/BaseApplication.php | 27 ++++------- kiri-engine/Abstracts/TraitApplication.php | 9 +--- kiri-engine/Application.php | 25 ++++------ kiri-engine/Cache/Redis.php | 2 +- kiri-engine/Di/Container.php | 12 ++--- kiri-engine/Di/NoteManager.php | 44 +++++++++--------- kiri-engine/Error/Logger.php | 2 +- kiri-engine/FileListen/HotReload.php | 2 +- kiri-engine/Kiri.php | 8 ++-- kiri-engine/Runtime.php | 2 +- kiri-gii/GiiController.php | 6 +-- kiri-gii/GiiJsonRpc.php | 8 ++-- kiri-gii/GiiModel.php | 14 +++--- kiri-gii/GiiRpcClient.php | 6 +-- kiri-gii/GiiRpcService.php | 4 +- kiri-websocket-server/Sender.php | 12 ++++- kiri-websocket-server/Server.php | 46 +++++++++++++++---- 33 files changed, 169 insertions(+), 172 deletions(-) rename kiri-note/Note.php => kiri-annotation/Annotation.php (92%) rename {kiri-note => kiri-annotation}/Aspect.php (90%) rename {kiri-note => kiri-annotation}/Attribute.php (76%) rename {kiri-note => kiri-annotation}/Event.php (94%) rename kiri-note/INote.php => kiri-annotation/IAnnotation.php (78%) rename {kiri-note => kiri-annotation}/Inject.php (97%) rename {kiri-note => kiri-annotation}/Loader.php (91%) rename {kiri-note => kiri-annotation}/Mapping.php (94%) rename {kiri-note => kiri-annotation}/Route/Document.php (82%) rename {kiri-note => kiri-annotation}/Route/Middleware.php (91%) rename {kiri-note => kiri-annotation}/Route/Route.php (92%) rename {kiri-note => kiri-annotation}/Route/Socket.php (82%) rename {kiri-note => kiri-annotation}/Target.php (85%) rename {kiri-note => kiri-annotation}/Task.php (92%) diff --git a/composer.json b/composer.json index 920784c9..a076eca1 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ ], "license": "MIT", "require": { - "php": ">=8.0", + "php": ">=8.1", "ext-json": "*", "ext-fileinfo": "*", "ext-pdo": "*", @@ -27,7 +27,6 @@ "ext-pcntl": "*", "ext-posix": "*", "composer-runtime-api": "^2.0", - "swiftmailer/swiftmailer": "v6.3.*", "psr/container": "^2.0", "psr/http-server-middleware": "1.0.1", "game-worker/kiri-event": "^v1.0", @@ -37,8 +36,10 @@ "psr-4": { "Kiri\\": "kiri-engine/", "Kiri\\Gateway\\": "kiri-gateway/", + "Kiri\\Websocket\\": "kiri-websocket-server/", "Gii\\": "kiri-gii/", - "Note\\": "kiri-note/" + "Kiri\\Annotation\\": "kiri-annotation/", + "Kiri\\Task\\": "kiri-task/" }, "files": [ "error.php", diff --git a/function.php b/function.php index eb91cbbe..e1f304c2 100644 --- a/function.php +++ b/function.php @@ -8,14 +8,14 @@ use JetBrains\PhpStorm\Pure; use Kiri\Abstracts\Config; use Kiri\Application; use Kiri\Core\ArrayAccess; -use Kiri\Di\NoteManager; +use Kiri\Di\AnnotationManager; use Kiri\Error\Logger; use Kiri\Events\EventDispatch; use Kiri\Events\EventProvider; use Kiri\Exception\ConfigException; use Kiri\Kiri; -use Note\Note; -use Note\Route\Route; +use Kiri\Annotation\Annotation; +use Kiri\Annotation\Route\Route; use Psr\Log\LoggerInterface; use Swoole\Process; use Swoole\WebSocket\Server; @@ -192,16 +192,16 @@ if (!function_exists('workerName')) { } -if (!function_exists('note')) { +if (!function_exists('Annotation')) { /** - * @return Note + * @return Annotation * @throws Exception */ - function annotation(): Note + function annotation(): Annotation { - return Kiri::getNote(); + return Kiri::getAnnotation(); } @@ -220,7 +220,7 @@ if (!function_exists('scan_directory')) { */ function scan_directory($dir, $namespace, array $exclude = []) { - $annotation = Kiri::app()->getNote(); + $annotation = Kiri::app()->getAnnotation(); $annotation->read($dir, $namespace, $exclude); injectRuntime($dir, $exclude); @@ -256,12 +256,12 @@ if (!function_exists('injectRuntime')) { */ function injectRuntime(string $path, array $exclude = []) { - $fileLists = Kiri::getNote()->runtime($path, $exclude); + $fileLists = Kiri::getAnnotation()->runtime($path, $exclude); $di = Kiri::getDi(); $router = []; foreach ($fileLists as $class) { - foreach (NoteManager::getTargetNote($class) as $value) { + foreach (AnnotationManager::getTargetAnnotation($class) as $value) { if (!method_exists($value, 'execute')) { continue; } diff --git a/kiri-note/Note.php b/kiri-annotation/Annotation.php similarity index 92% rename from kiri-note/Note.php rename to kiri-annotation/Annotation.php index 47fc9249..a8697c31 100644 --- a/kiri-note/Note.php +++ b/kiri-annotation/Annotation.php @@ -1,7 +1,7 @@ _scanDir(new DirectoryIterator($path), $namespace); } - /** - * @param string $class - * @param string $property - * @return \ReflectionProperty|array|null - * @throws ReflectionException - */ - public function getProperty(string $class, string $property = ''): \ReflectionProperty|array|null - { - return Kiri::getDi()->getClassReflectionProperty($class, $property); - } - /** * @param string $class @@ -147,7 +130,6 @@ class Loader extends Component * @param DirectoryIterator $path * @param string $namespace * @return ReflectionClass|null - * @throws ReflectionException */ private function getReflect(DirectoryIterator $path, string $namespace): ?ReflectionClass { diff --git a/kiri-note/Mapping.php b/kiri-annotation/Mapping.php similarity index 94% rename from kiri-note/Mapping.php rename to kiri-annotation/Mapping.php index 77ef0de4..25d8a4c1 100644 --- a/kiri-note/Mapping.php +++ b/kiri-annotation/Mapping.php @@ -1,6 +1,6 @@ get('note'); + return $this->get('Annotation'); } @@ -411,7 +403,6 @@ abstract class BaseApplication extends Component } - /** * @param $array */ @@ -450,7 +441,7 @@ abstract class BaseApplication extends Component 'error' => ['class' => ErrorHandler::class], 'config' => ['class' => Config::class], 'logger' => ['class' => Logger::class], - 'note' => ['class' => SNote::class], + 'Annotation' => ['class' => SAnnotation::class], 'databases' => ['class' => Connection::class], 'jwt' => ['class' => Jwt::class], 'async' => ['class' => Async::class], diff --git a/kiri-engine/Abstracts/TraitApplication.php b/kiri-engine/Abstracts/TraitApplication.php index a551e951..c743f2ab 100644 --- a/kiri-engine/Abstracts/TraitApplication.php +++ b/kiri-engine/Abstracts/TraitApplication.php @@ -4,14 +4,11 @@ namespace Kiri\Abstracts; -use Note\Note as SNote; +use Kiri\Annotation\Annotation as SAnnotation; use Database\Connection; use Database\DatabasesProviders; -use Http\Handler\Client\Client; -use Http\Handler\Client\Curl; use Http\Handler\Router; use Server\Server; -use Kiri\Crontab\Producer; use Kiri\Async; use Kiri\Error\Logger; use Kiri\Jwt\JWTAuth; @@ -25,11 +22,9 @@ use Kiri\Jwt\JWTAuth; * @property Async $async * @property Logger $logger * @property JWTAuth $jwt - * @property SNote $annotation + * @property SAnnotation $annotation * @property BaseGoto $goto - * @property Client $client * @property Connection $databases - * @property Curl $curl */ trait TraitApplication { diff --git a/kiri-engine/Application.php b/kiri-engine/Application.php index 63cd1acc..99b52c40 100644 --- a/kiri-engine/Application.php +++ b/kiri-engine/Application.php @@ -13,25 +13,22 @@ namespace Kiri; use Closure; use Database\DatabasesProviders; use Exception; -use Kiri\Abstracts\BaseApplication; -use Kiri\Abstracts\Config; -use Kiri\Abstracts\Kernel; +use Kiri\Abstracts\{BaseApplication, Config, Kernel}; use Kiri\Crontab\CrontabProviders; -use Kiri\Events\OnAfterCommandExecute; -use Kiri\Events\OnBeforeCommandExecute; -use Kiri\Exception\NotFindClassException; +use Kiri\Events\{OnAfterCommandExecute, OnBeforeCommandExecute}; use Kiri\FileListen\HotReload; use ReflectionException; use Server\ServerProviders; use stdClass; use Swoole\Process; use Swoole\Timer; -use Symfony\Component\Console\Application as ConsoleApplication; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\ArgvInput; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\ConsoleOutput; -use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\{Application as ConsoleApplication, + Command\Command, + Input\ArgvInput, + Input\InputInterface, + Output\ConsoleOutput, + Output\OutputInterface +}; /** * Class Init @@ -213,7 +210,6 @@ class Application extends BaseApplication } - /** * @param $argv * @return array @@ -224,7 +220,6 @@ class Application extends BaseApplication } - /** * @throws ReflectionException * @throws Exception @@ -244,8 +239,6 @@ class Application extends BaseApplication } - - /** * @param $className * @param null $abstracts diff --git a/kiri-engine/Cache/Redis.php b/kiri-engine/Cache/Redis.php index cc7a602e..53e2b8e5 100644 --- a/kiri-engine/Cache/Redis.php +++ b/kiri-engine/Cache/Redis.php @@ -17,7 +17,7 @@ use Kiri\Events\EventProvider; use Kiri\Exception\ConfigException; use Kiri\Kiri; use Kiri\Pool\Redis as PoolRedis; -use Note\Inject; +use Kiri\Annotation\Inject; use Server\Events\OnWorkerExit; use Swoole\Timer; diff --git a/kiri-engine/Di/Container.php b/kiri-engine/Di/Container.php index 8f8b01e4..e7cc1eeb 100644 --- a/kiri-engine/Di/Container.php +++ b/kiri-engine/Di/Container.php @@ -9,7 +9,7 @@ declare(strict_types=1); namespace Kiri\Di; -use Note\Inject; +use Kiri\Annotation\Inject; use Closure; use Exception; use Kiri\Abstracts\Logger; @@ -197,7 +197,7 @@ class Container implements ContainerInterface */ public function propertyInject(ReflectionClass $reflect, $object): mixed { - foreach (NoteManager::getPropertyNote($reflect) as $property => $inject) { + foreach (AnnotationManager::getPropertyAnnotation($reflect) as $property => $inject) { /** @var Inject $inject */ $inject->execute($object, $property); } @@ -212,7 +212,7 @@ class Container implements ContainerInterface */ public function getMethodAttribute($className, $method = null): array { - $methods = NoteManager::getMethodNote($this->getReflect($className)); + $methods = AnnotationManager::getMethodAnnotation($this->getReflect($className)); if (!empty($method)) { return $methods[$method] ?? []; } @@ -227,7 +227,7 @@ class Container implements ContainerInterface */ public function getClassReflectionProperty(string $class, string $property = null): ReflectionProperty|null|array { - $lists = NoteManager::getProperty($this->getReflect($class)); + $lists = AnnotationManager::getProperty($this->getReflect($class)); if (empty($lists)) { return null; } @@ -266,7 +266,7 @@ class Container implements ContainerInterface if ($reflect->isAbstract() || $reflect->isTrait() || $reflect->isInterface()) { return $this->_reflection[$class] = $reflect; } - $construct = NoteManager::resolveTarget($reflect); + $construct = AnnotationManager::resolveTarget($reflect); if (!empty($construct) && $construct->getNumberOfParameters() > 0) { $this->_constructs[$class] = $construct; } @@ -284,7 +284,7 @@ class Container implements ContainerInterface if (is_string($class)) { $class = $this->getReflect($class); } - return NoteManager::getMethods($class); + return AnnotationManager::getMethods($class); } diff --git a/kiri-engine/Di/NoteManager.php b/kiri-engine/Di/NoteManager.php index f44613bd..8ea24694 100644 --- a/kiri-engine/Di/NoteManager.php +++ b/kiri-engine/Di/NoteManager.php @@ -7,14 +7,14 @@ use ReflectionAttribute; use ReflectionClass; use ReflectionProperty; -class NoteManager +class AnnotationManager { private static array $_classTarget = []; - private static array $_classMethodNote = []; + private static array $_classMethodAnnotation = []; private static array $_classMethod = []; - private static array $_classPropertyNote = []; + private static array $_classPropertyAnnotation = []; private static array $_classProperty = []; private static array $_mapping = []; @@ -25,9 +25,9 @@ class NoteManager public static function clear() { static::$_classTarget = []; - static::$_classMethodNote = []; + static::$_classMethodAnnotation = []; static::$_classMethod = []; - static::$_classPropertyNote = []; + static::$_classPropertyAnnotation = []; static::$_classProperty = []; static::$_mapping = []; } @@ -36,7 +36,7 @@ class NoteManager /** * @param ReflectionClass $class */ - public static function setTargetNote(ReflectionClass $class) + public static function setTargetAnnotation(ReflectionClass $class) { $className = $class->getName(); if (!isset(static::$_classTarget[$className])) { @@ -111,7 +111,7 @@ class NoteManager * @param mixed $class * @return array */ - public static function getTargetNote(mixed $class): array + public static function getTargetAnnotation(mixed $class): array { if (!is_string($class)) { $class = $class::class; @@ -123,20 +123,20 @@ class NoteManager /** * @param ReflectionClass $class */ - public static function setMethodNote(ReflectionClass $class) + public static function setMethodAnnotation(ReflectionClass $class) { $className = $class->getName(); - static::$_classMethodNote[$className] = static::$_classMethod[$className] = []; + static::$_classMethodAnnotation[$className] = static::$_classMethod[$className] = []; foreach ($class->getMethods() as $ReflectionMethod) { static::$_classMethod[$className][$ReflectionMethod->getName()] = $ReflectionMethod; - static::$_classMethodNote[$className][$ReflectionMethod->getName()] = []; + static::$_classMethodAnnotation[$className][$ReflectionMethod->getName()] = []; foreach ($ReflectionMethod->getAttributes() as $attribute) { if (!class_exists($attribute->getName())) { continue; } $instance = $attribute->newInstance(); - static::$_classMethodNote[$className][$ReflectionMethod->getName()][] = $instance; + static::$_classMethodAnnotation[$className][$ReflectionMethod->getName()][] = $instance; self::setMappingMethod($attribute, $className, $ReflectionMethod->getName(), $instance); } @@ -159,9 +159,9 @@ class NoteManager * @param ReflectionClass $class * @return array */ - #[Pure] public static function getMethodNote(ReflectionClass $class): array + #[Pure] public static function getMethodAnnotation(ReflectionClass $class): array { - return static::$_classMethodNote[$class->getName()] ?? []; + return static::$_classMethodAnnotation[$class->getName()] ?? []; } @@ -171,9 +171,9 @@ class NoteManager */ public static function resolveTarget(ReflectionClass $reflect): ?\ReflectionMethod { - NoteManager::setPropertyNote($reflect); - NoteManager::setTargetNote($reflect); - NoteManager::setMethodNote($reflect); + AnnotationManager::setPropertyAnnotation($reflect); + AnnotationManager::setTargetAnnotation($reflect); + AnnotationManager::setMethodAnnotation($reflect); return $reflect->getConstructor(); } @@ -182,10 +182,10 @@ class NoteManager /** * @param ReflectionClass $class */ - public static function setPropertyNote(ReflectionClass $class) + public static function setPropertyAnnotation(ReflectionClass $class) { $className = $class->getName(); - static::$_classProperty[$className] = static::$_classPropertyNote[$className] = []; + static::$_classProperty[$className] = static::$_classPropertyAnnotation[$className] = []; foreach ($class->getProperties(ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED) as $ReflectionMethod) { static::$_classProperty[$className][$ReflectionMethod->getName()] = $ReflectionMethod; @@ -196,7 +196,7 @@ class NoteManager $instance = $attribute->newInstance(); - static::$_classPropertyNote[$className][$ReflectionMethod->getName()] = $instance; + static::$_classPropertyAnnotation[$className][$ReflectionMethod->getName()] = $instance; self::setMappingProperty($attribute, $className, $ReflectionMethod->getName(), $instance); } @@ -250,7 +250,7 @@ class NoteManager * @param string $method * @return mixed */ - public static function getPropertyByNote(string $attribute, string $class, string $method): mixed + public static function getPropertyByAnnotation(string $attribute, string $class, string $method): mixed { $class = self::getAttributeTrees($attribute, $class); if (empty($class) || !isset($class['property'])) { @@ -294,9 +294,9 @@ class NoteManager * @param ReflectionClass $class * @return array */ - #[Pure] public static function getPropertyNote(ReflectionClass $class): array + #[Pure] public static function getPropertyAnnotation(ReflectionClass $class): array { - return static::$_classPropertyNote[$class->getName()] ?? []; + return static::$_classPropertyAnnotation[$class->getName()] ?? []; } diff --git a/kiri-engine/Error/Logger.php b/kiri-engine/Error/Logger.php index a72d8fa3..409c13a1 100644 --- a/kiri-engine/Error/Logger.php +++ b/kiri-engine/Error/Logger.php @@ -13,7 +13,7 @@ use Exception; use Kiri\Abstracts\Component; use Kiri\Core\Json; use Kiri\Kiri; -use Note\Inject; +use Kiri\Annotation\Inject; use Psr\Log\LoggerInterface; use Throwable; diff --git a/kiri-engine/FileListen/HotReload.php b/kiri-engine/FileListen/HotReload.php index 9d5da32e..2bd6b35e 100644 --- a/kiri-engine/FileListen/HotReload.php +++ b/kiri-engine/FileListen/HotReload.php @@ -8,7 +8,7 @@ use Kiri\Core\Json; use Kiri\Error\Logger; use Kiri\Exception\ConfigException; use Kiri\Kiri; -use Note\Inject; +use Kiri\Annotation\Inject; use Swoole\Coroutine; use Swoole\Process; use Swoole\Timer; diff --git a/kiri-engine/Kiri.php b/kiri-engine/Kiri.php index e7f82e03..aaaff43a 100644 --- a/kiri-engine/Kiri.php +++ b/kiri-engine/Kiri.php @@ -7,7 +7,7 @@ namespace Kiri; error_reporting(0); -use Note\Note; +use Kiri\Annotation\Annotation; use Database\Collection; use Database\ModelInterface; use Exception; @@ -159,12 +159,12 @@ class Kiri /** - * @return Note + * @return Annotation * @throws Exception */ - public static function getNote(): Note + public static function getAnnotation(): Annotation { - return static::app()->getNote(); + return static::app()->getAnnotation(); } diff --git a/kiri-engine/Runtime.php b/kiri-engine/Runtime.php index 43d781db..041b2fc3 100644 --- a/kiri-engine/Runtime.php +++ b/kiri-engine/Runtime.php @@ -44,7 +44,7 @@ class Runtime extends Command public function execute(InputInterface $input, OutputInterface $output): int { // TODO: Implement onHandler() method. - $annotation = Kiri::app()->getNote(); + $annotation = Kiri::app()->getAnnotation(); $runtime = storage(static::CACHE_NAME); $config = storage(static::CONFIG_NAME); diff --git a/kiri-gii/GiiController.php b/kiri-gii/GiiController.php index eda2f801..f3cff8f0 100644 --- a/kiri-gii/GiiController.php +++ b/kiri-gii/GiiController.php @@ -65,9 +65,9 @@ namespace {$namespace}; } else { $import = "use Kiri; use Exception; -use Note\Target; -use Note\Route\Middleware; -use Note\Route\Route; +use Kiri\Annotation\Target; +use Kiri\Annotation\Route\Middleware; +use Kiri\Annotation\Route\Route; use Kiri\Core\Str; use Kiri\Core\Json; use Http\Context\Request; diff --git a/kiri-gii/GiiJsonRpc.php b/kiri-gii/GiiJsonRpc.php index 7d4be190..ba6c6a42 100644 --- a/kiri-gii/GiiJsonRpc.php +++ b/kiri-gii/GiiJsonRpc.php @@ -51,8 +51,8 @@ interface ' . ucfirst($name) . 'RpcInterface namespace Rpc\Producers; -use Note\Target; -use Note\Mapping; +use Kiri\Annotation\Target; +use Kiri\Annotation\Mapping; use Rpc\\' . ucfirst($name) . 'RpcInterface; use Exception; use Kiri\Rpc\JsonRpcConsumers; @@ -86,8 +86,8 @@ class ' . ucfirst($name) . 'RpcService extends JsonRpcConsumers implements ' . u namespace Rpc\Consumers; -use Note\Target; -use Kiri\Rpc\Note\JsonRpc; +use Kiri\Annotation\Target; +use Kiri\Rpc\Annotation\JsonRpc; use Http\Handler\Controller; use Rpc\\' . ucfirst($name) . 'RpcInterface; diff --git a/kiri-gii/GiiModel.php b/kiri-gii/GiiModel.php index ab10a6ee..2e68ecba 100644 --- a/kiri-gii/GiiModel.php +++ b/kiri-gii/GiiModel.php @@ -66,11 +66,11 @@ namespace ' . $namespace . '; $html .= $imports . PHP_EOL; } - if (!str_contains($imports, 'Database\Note\Set')) { - $html .= 'use Database\Note\Set;' . PHP_EOL; + if (!str_contains($imports, 'Database\Annotation\Set')) { + $html .= 'use Database\Annotation\Set;' . PHP_EOL; } - if (!str_contains($imports, 'Database\Note\Get')) { - $html .= 'use Database\Note\Get;' . PHP_EOL; + if (!str_contains($imports, 'Database\Annotation\Get')) { + $html .= 'use Database\Annotation\Get;' . PHP_EOL; } } catch (\Throwable $e) { logger()->addError($e, 'throwable'); @@ -84,11 +84,11 @@ namespace ' . $namespace . '; use Exception; -use Note\Target; +use Kiri\Annotation\Target; use Kiri\Core\Json; use Database\Connection; -use Database\Note\Get; -use Database\Note\Set; +use Database\Annotation\Get; +use Database\Annotation\Set; use Database\Relation; use Database\Model; ' . PHP_EOL; diff --git a/kiri-gii/GiiRpcClient.php b/kiri-gii/GiiRpcClient.php index 9952ad7c..d938a2a7 100644 --- a/kiri-gii/GiiRpcClient.php +++ b/kiri-gii/GiiRpcClient.php @@ -36,9 +36,9 @@ class GiiRpcClient extends GiiBase namespace App\Client\Rpc; -use Note\Rpc\Consumer; -use Note\Rpc\RpcClient; -use Note\Target; +use Kiri\Annotation\Rpc\Consumer; +use Kiri\Annotation\Rpc\RpcClient; +use Kiri\Annotation\Target; use Exception; use Rpc\Client; use Kiri\Core\Json; diff --git a/kiri-gii/GiiRpcService.php b/kiri-gii/GiiRpcService.php index afb3aa02..3853343d 100644 --- a/kiri-gii/GiiRpcService.php +++ b/kiri-gii/GiiRpcService.php @@ -33,8 +33,8 @@ class GiiRpcService extends GiiBase namespace App\Rpc; -use Note\Route\RpcProducer; -use Note\Target; +use Kiri\Annotation\Route\RpcProducer; +use Kiri\Annotation\Target; use Exception; use Http\Controller; use Kiri\Core\Json; diff --git a/kiri-websocket-server/Sender.php b/kiri-websocket-server/Sender.php index b21af285..4a897cff 100644 --- a/kiri-websocket-server/Sender.php +++ b/kiri-websocket-server/Sender.php @@ -44,6 +44,17 @@ class Sender implements WebSocketInterface } + /** + * @param $fd + * @param $reactor_id + * @return array|null + */ + public function connection_info($fd, $reactor_id = null): ?array + { + return $this->server->getClientInfo($fd, $reactor_id); + } + + /** * @param int $fd * @param int $code @@ -53,7 +64,6 @@ class Sender implements WebSocketInterface public function disconnect(int $fd, int $code = SWOOLE_WEBSOCKET_CLOSE_NORMAL, string $reason = ''): bool { if ($this->isEstablished($fd)) { - // TODO: Implement disconnect() method. return $this->server->disconnect($fd, $code, $reason); } return false; diff --git a/kiri-websocket-server/Server.php b/kiri-websocket-server/Server.php index 4a8d11f6..3c3a8c0d 100644 --- a/kiri-websocket-server/Server.php +++ b/kiri-websocket-server/Server.php @@ -6,7 +6,7 @@ use Exception; use Http\Handler\DataGrip; use Http\Handler\Router; use Kiri\Abstracts\AbstractServer; -use Note\Inject; +use Kiri\Annotation\Inject; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use Server\Contract\OnCloseInterface; @@ -23,7 +23,7 @@ use Swoole\WebSocket\Server as WebSocketServer; /** * websocket server */ -class Server extends AbstractServer implements OnHandshakeInterface, OnMessageInterface, OnCloseInterface +class Server extends AbstractServer { public Router $router; @@ -36,10 +36,11 @@ class Server extends AbstractServer implements OnHandshakeInterface, OnMessageIn /** - * @var WebSocketServer + * @var WebSocketInterface */ - #[Inject(SwooleServerInterface::class)] - public WebSocketServer $server; + #[Inject(WebSocketInterface::class)] + public WebSocketInterface $server; + /** @@ -116,8 +117,25 @@ class Server extends AbstractServer implements OnHandshakeInterface, OnMessageIn $response->setStatusCode(101, 'connection success.'); $response->end(); } - if ($this->callback instanceof OnOpenInterface) { - $this->callback->onOpen($this->server, $request); +// if ($this->server instanceof \Swoole\Coroutine\Http\Server) { +// $response->upgrade(); +// $this->deferOpen($request); +// while (true) { +// $receive = $response->recv(); +// if ($receive === '' || $receive instanceof CloseFrame) { +// $response->close(); +// if ($this->callback instanceof OnCloseInterface) { +// $this->callback->onClose($this->server, $response->fd); +// } +// break; +// } +// $this->callback->onMessage($this->server, $receive); +// } +// } else { +// $this->deferOpen($request); +// } + if ($response->isWritable()) { + $this->deferOpen($request); } } catch (\Throwable $throwable) { $response->status(4000 + $throwable->getCode(), $throwable->getMessage()); @@ -126,14 +144,22 @@ class Server extends AbstractServer implements OnHandshakeInterface, OnMessageIn } + private function deferOpen($request) + { + if ($this->callback instanceof OnOpenInterface) { + $this->callback->onOpen($request); + } + } + + /** - * @param \Swoole\Server $server + * @param $server * @param Frame $frame */ - public function onMessage(\Swoole\Server $server, Frame $frame): void + public function onMessage($server, Frame $frame): void { if ($this->callback instanceof OnMessageInterface) { - $this->callback->onMessage($server, $frame); + $this->callback->onMessage($frame); } } }