|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace N1215\CakeCandle\Reflection; |
|
4
|
|
|
|
|
5
|
|
|
use ReflectionException; |
|
6
|
|
|
use ReflectionFunction; |
|
7
|
|
|
use ReflectionFunctionAbstract; |
|
8
|
|
|
use ReflectionMethod; |
|
9
|
|
|
use ReflectionParameter; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class ReflectionCallable |
|
13
|
|
|
* @package N1215\CakeCandle\Reflection |
|
14
|
|
|
*/ |
|
15
|
|
|
final class ReflectionCallable |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var callable |
|
19
|
|
|
*/ |
|
20
|
|
|
private $callable; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var ReflectionFunctionAbstract |
|
24
|
|
|
*/ |
|
25
|
|
|
private $reflectionFunctionAbstract; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param callable $callable |
|
29
|
|
|
* @throws ReflectionException |
|
30
|
|
|
*/ |
|
31
|
17 |
|
public function __construct(callable $callable) |
|
32
|
|
|
{ |
|
33
|
17 |
|
$this->callable = $callable; |
|
34
|
17 |
|
$this->reflectionFunctionAbstract = $this->createReflectionFunctionAbstract($callable); |
|
35
|
15 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param callable $callable |
|
39
|
|
|
* @return ReflectionFunctionAbstract |
|
40
|
|
|
* @throws ReflectionException |
|
41
|
|
|
*/ |
|
42
|
17 |
|
private function createReflectionFunctionAbstract(callable $callable) |
|
43
|
|
|
{ |
|
44
|
|
|
// array |
|
45
|
17 |
|
if (\is_array($callable)) { |
|
46
|
6 |
|
list($class, $method) = $callable; |
|
47
|
6 |
|
return new ReflectionMethod($class, $method); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// closure |
|
51
|
11 |
|
if ($callable instanceof \Closure) { |
|
52
|
5 |
|
return new ReflectionFunction($callable); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// callable object |
|
56
|
6 |
|
if (\is_object($callable) && \method_exists($callable, '__invoke')) { |
|
57
|
2 |
|
return new ReflectionMethod($callable, '__invoke'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
4 |
|
if (\is_string($callable)) { |
|
61
|
|
|
// standard function |
|
62
|
4 |
|
if (\function_exists($callable)) { |
|
63
|
2 |
|
return new ReflectionFunction($callable); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// static method |
|
67
|
2 |
|
$parts = explode('::', $callable); |
|
68
|
2 |
|
if (count($parts) === 2) { |
|
69
|
2 |
|
return new ReflectionMethod($parts[0], $parts[1]); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
throw new ReflectionException('failed to reflect the callable.'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return ReflectionParameter[] |
|
78
|
|
|
*/ |
|
79
|
9 |
|
public function getParameters() |
|
80
|
|
|
{ |
|
81
|
9 |
|
return $this->reflectionFunctionAbstract->getParameters(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param array $args |
|
86
|
|
|
* @return mixed |
|
87
|
|
|
*/ |
|
88
|
7 |
|
public function invoke(array $args = []) |
|
89
|
|
|
{ |
|
90
|
7 |
|
$callable = $this->callable; |
|
91
|
7 |
|
return call_user_func_array($callable, $args); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|