4
4
5
5
class Handler
6
6
{
7
- protected $ closure ;
7
+ protected $ callable ;
8
8
protected $ requiredArguments ;
9
9
10
- public function __construct (callable $ closure )
10
+ public function __construct (callable $ callable )
11
11
{
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
+
14
29
$ this ->requiredArguments = $ reflection ->getNumberOfRequiredParameters ();
15
30
}
16
31
@@ -20,8 +35,8 @@ public function handle(array $arguments)
20
35
if ($ argc < $ this ->requiredArguments ) {
21
36
throw new \Exception ("Expected " . $ this ->requiredArguments . " arguments, got " . $ argc );
22
37
}
23
- $ closure = $ this ->closure ;
24
- $ closure (...$ arguments );
38
+ $ callable = $ this ->callable ;
39
+ $ callable (...$ arguments );
25
40
return true ;
26
41
}
27
42
}
0 commit comments