From 6ae5fba4d21088c3556e58daf66672e5dda2ff36 Mon Sep 17 00:00:00 2001 From: Faizan Akram Date: Thu, 29 Jul 2021 16:19:43 +0530 Subject: [PATCH] Support for all callables - simplifies call method --- .gitignore | 2 ++ src/Server/Server.php | 39 +++++++++++++-------------------------- 2 files changed, 15 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index 3a4edf6..efa3726 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ .project +.idea +.vscode diff --git a/src/Server/Server.php b/src/Server/Server.php index bc97ae3..694c48a 100644 --- a/src/Server/Server.php +++ b/src/Server/Server.php @@ -77,40 +77,27 @@ protected function call($methodname, $args) return new Error(-32601, 'server error. requested method ' . $methodname . ' does not exist.'); } $method = $this->callbacks[$methodname]; - // Perform the callback and send the response - if (count($args) == 1) { - // If only one paramater just send that instead of the whole array + + if (is_array($args) && count($args) == 1) { + // If only one parameter just send that instead of the whole array $args = $args[0]; } - // Are we dealing with a function or a method? - if (is_string($method) && substr($method, 0, 5) == 'this:') { - // It's a class method - check it exists - $method = substr($method, 5); - if (!method_exists($this, $method)) { - return new Error(-32601, 'server error. requested class method "' . $method . '" does not exist.'); - } + try { + // Are we dealing with a function or a method? + if (is_string($method) && substr($method, 0, 5) === 'this:') { + // It's a class method - check it exists + $method = substr($method, 5); - //Call the method - $result = $this->$method($args); - } else { - // It's a function - does it exist? - if (is_array($method)) { - if (!is_callable([$method[0], $method[1]])) { - return new Error(-32601, - 'server error. requested object method "' . $method[1] . '" does not exist.'); - } - } else { - if (!function_exists($method)) { - return new Error(-32601, 'server error. requested function "' . $method . '" does not exist.'); - } + return $this->$method($args); } - // Call the function - $result = call_user_func($method, $args); + return call_user_func($method, $args); + } catch (\BadFunctionCallException $exception) { + return new Error(-32601, "server error. requested callable '{$method}' does not exist."); } - return $result; + } public function error($error, $message = false)