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. *