Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for all callables and PHP 8 #4

Merged
merged 1 commit into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.project
.idea
.vscode
39 changes: 13 additions & 26 deletions src/Server/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down