From b503c9edb9adec822927a4f962366263f34baf3c Mon Sep 17 00:00:00 2001 From: flashwave Date: Thu, 15 Aug 2024 18:38:01 +0000 Subject: [PATCH] Added stuff for attribute based registration. --- src/IRpcActionHandler.php | 18 ++++++++++ src/RpcActionAttribute.php | 67 +++++++++++++++++++++++++++++++++++ src/RpcActionHandler.php | 14 ++++++++ src/RpcActionHandlerTrait.php | 16 +++++++++ src/RpcProcedureAttribute.php | 20 +++++++++++ src/RpcQueryAttribute.php | 20 +++++++++++ src/RpcServer.php | 11 +++++- 7 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 src/IRpcActionHandler.php create mode 100644 src/RpcActionAttribute.php create mode 100644 src/RpcActionHandler.php create mode 100644 src/RpcActionHandlerTrait.php create mode 100644 src/RpcProcedureAttribute.php create mode 100644 src/RpcQueryAttribute.php diff --git a/src/IRpcActionHandler.php b/src/IRpcActionHandler.php new file mode 100644 index 0000000..1b97e97 --- /dev/null +++ b/src/IRpcActionHandler.php @@ -0,0 +1,18 @@ +isProcedure; + } + + /** + * Returns the action name. + * + * @return string + */ + public function getName(): string { + return $this->name; + } + + /** + * Reads attributes from methods in a IRpcActionHandler instance and registers them to a given RpcServer instance. + * + * @param RpcServer $server RPC server instance. + * @param IRpcActionHandler $handler Handler instance. + */ + public static function register(RpcServer $server, IRpcActionHandler $handler): void { + $objectInfo = new ReflectionObject($handler); + $methodInfos = $objectInfo->getMethods(); + + foreach($methodInfos as $methodInfo) { + $attrInfos = $methodInfo->getAttributes(RpcActionAttribute::class, ReflectionAttribute::IS_INSTANCEOF); + + foreach($attrInfos as $attrInfo) { + $handlerInfo = $attrInfo->newInstance(); + $server->registerAction( + $handlerInfo->isProcedure(), + $handlerInfo->getName(), + $methodInfo->getClosure($methodInfo->isStatic() ? null : $handler) + ); + } + } + } +} diff --git a/src/RpcActionHandler.php b/src/RpcActionHandler.php new file mode 100644 index 0000000..cfbc3b5 --- /dev/null +++ b/src/RpcActionHandler.php @@ -0,0 +1,14 @@ +registerRpcActions($this); + } + /** * Registers an action. *