Added methods registerMethod() and reorderMethods() to \KimchiAPI > KimchiAPI

This commit is contained in:
Zi Xing 2021-12-15 14:11:40 -05:00
parent b48187c6e1
commit 56447575a5
3 changed files with 35 additions and 1 deletions

View File

@ -3,7 +3,9 @@
namespace KimchiAPI;
// Define server information for response headers
use KimchiAPI\Exceptions\MethodAlreadyRegisteredException;
use KimchiAPI\Exceptions\MissingComponentsException;
use KimchiAPI\Interfaces\MethodInterface;
use KimchiAPI\Utilities\Converter;
use RuntimeException;
@ -42,4 +44,35 @@
$this->server_name = Converter::functionNameSafe($server_name);
}
/**
* @param MethodInterface $method
* @throws MethodAlreadyRegisteredException
*/
public function registerMethod(MethodInterface $method)
{
if(isset($this->methods[$method->getVersion() . ':' . $method->getMethod()]))
throw new MethodAlreadyRegisteredException('The method ' . $method->getMethod() . ' (' . $method->getVersion() . ') is already registered');
$this->methods[$method->getVersion() . ':' . $method->getMethod()] = $method;
$this->reorderMethods();
}
/**
* Reorders the methods into alphabetical order
*/
private function reorderMethods()
{
$method_reordered = array_keys($this->methods);
sort($method_reordered);
$methods_clean = [];
foreach($method_reordered as $method_name)
{
if(is_int($method_name) == false)
$methods_clean[$method_name] = $this->methods[$method_name];
}
$this->methods = $methods_clean;
}
}

View File

@ -5,7 +5,6 @@
namespace KimchiAPI\Objects;
use Exception;
use KimchiRPC\Abstracts\Types\ProtocolType;
class Response
{

View File

@ -37,4 +37,6 @@
{
return (strlen($input) > $length) ? substr($input,0, $length).'...' : $input;
}
public static function methodResigtratio
}