Skip to content

Commit 4ced297

Browse files
committed
Allow handlers to be not only closures
1 parent 3bbb45f commit 4ced297

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "sowe/websocketio",
33
"description": "SocketIO implementation using RFC6455-compliant websockets only. Built on top of Swoole Websocket Server.",
4-
"version": "1.0.1",
4+
"version": "1.0.2",
55
"type": "library",
66
"license": "MIT",
77
"keywords": [

src/Handler.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,28 @@
44

55
class Handler
66
{
7-
protected $closure;
7+
protected $callable;
88
protected $requiredArguments;
99

10-
public function __construct(callable $closure)
10+
public function __construct(callable $callable)
1111
{
12-
$this->closure = $closure;
13-
$reflection = new \ReflectionFunction($closure);
12+
$this->callable = $callable;
13+
14+
if ($callable instanceof \Closure) {
15+
$reflection = new \ReflectionFunction($callable);
16+
} elseif (is_string($callable)) {
17+
$parts = explode('::', $callable);
18+
if (sizeof($parts) > 1) {
19+
$reflection = new \ReflectionMethod(...$parts);
20+
} else {
21+
$reflection = new \ReflectionFunction($callable);
22+
}
23+
} elseif (!is_array($callable)) {
24+
$reflection = new ReflectionMethod($callable, '__invoke');
25+
} else {
26+
$reflection = new ReflectionMethod(...$callable);
27+
}
28+
1429
$this->requiredArguments = $reflection->getNumberOfRequiredParameters();
1530
}
1631

@@ -20,8 +35,8 @@ public function handle(array $arguments)
2035
if ($argc < $this->requiredArguments) {
2136
throw new \Exception("Expected " . $this->requiredArguments . " arguments, got " . $argc);
2237
}
23-
$closure = $this->closure;
24-
$closure(...$arguments);
38+
$callable = $this->callable;
39+
$callable(...$arguments);
2540
return true;
2641
}
2742
}

0 commit comments

Comments
 (0)