Added stuff for attribute based registration.

This commit is contained in:
flash 2024-08-15 18:38:01 +00:00
parent 00c330342d
commit b503c9edb9
7 changed files with 165 additions and 1 deletions

18
src/IRpcActionHandler.php Normal file
View file

@ -0,0 +1,18 @@
<?php
// IRpcActionHandler.php
// Created: 2024-08-15
// Updated: 2024-08-15
namespace Aiwass;
/**
* Provides the interface for RpcServer::register().
*/
interface IRpcActionHandler {
/**
* Registers axctions on a given RpcServer instance.
*
* @param RpcServer $server Target RPC server.
*/
public function registerRpcActions(RpcServer $server): void;
}

View file

@ -0,0 +1,67 @@
<?php
// RpcActionAttribute.php
// Created: 2024-08-15
// Updated: 2024-08-15
namespace Aiwass;
use Attribute;
use ReflectionAttribute;
use ReflectionObject;
/**
* Provides base for attributes that mark methods in a class as RPC action handlers.
*/
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
class RpcActionAttribute {
/**
* @param bool $isProcedure true if the action is a procedure, false if query.
* @param string $name Action name.
*/
public function __construct(
private bool $isProcedure,
private string $name
) {}
/**
* Returns whether the action is a procedure.
*
* @return bool
*/
public function isProcedure(): bool {
return $this->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)
);
}
}
}
}

14
src/RpcActionHandler.php Normal file
View file

@ -0,0 +1,14 @@
<?php
// RpcActionHandler.php
// Created: 2024-08-15
// Updated: 2024-08-15
namespace Aiwass;
/**
* Provides an abstract class version of IRpcActionHandler that already includes the trait as well,
* letting you only have to use one use statement rather than two!
*/
abstract class RpcActionHandler implements IRpcActionHandler {
use RpcActionHandlerTrait;
}

View file

@ -0,0 +1,16 @@
<?php
// RpcActionHandlerTrait.php
// Created: 2024-08-15
// Updated: 2024-08-15
namespace Aiwass;
/**
* Provides an implementation of IRpcActionHandler::registerRpcActions that uses the attributes.
* For more advanced use, everything can be use'd separately and RpcActionAttribute::register called manually.
*/
trait RpcActionHandlerTrait {
public function registerRoutes(RpcServer $server): void {
RpcActionAttribute::register($server, $this);
}
}

View file

@ -0,0 +1,20 @@
<?php
// RpcProcedureAttribute.php
// Created: 2024-08-15
// Updated: 2024-08-15
namespace Aiwass;
use Attribute;
/**
* Provides an attribute for marking methods in a class as an RPC Query action.
*/
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
class RpcProcedureAttribute extends RpcActionAttribute {
/**
* @param string $name Action name.
*/
public function __construct(string $name) {
parent::__construct(true, $name);
}
}

20
src/RpcQueryAttribute.php Normal file
View file

@ -0,0 +1,20 @@
<?php
// RpcQueryAttribute.php
// Created: 2024-08-15
// Updated: 2024-08-15
namespace Aiwass;
use Attribute;
/**
* Provides an attribute for marking methods in a class as an RPC Query action.
*/
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
class RpcQueryAttribute extends RpcActionAttribute {
/**
* @param string $name Action name.
*/
public function __construct(string $name) {
parent::__construct(false, $name);
}
}

View file

@ -1,7 +1,7 @@
<?php
// RpcServer.php
// Created: 2024-08-13
// Updated: 2024-08-13
// Updated: 2024-08-15
namespace Aiwass;
@ -40,6 +40,15 @@ class RpcServer {
return new RpcRouteHandler($this);
}
/**
* Registers a handler class.
*
* @param IRpcActionHandler $handler Handler to register.
*/
public function register(IRpcActionHandler $handler): void {
$handler->registerRpcActions($this);
}
/**
* Registers an action.
*