1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silk\Support; |
4
|
|
|
|
5
|
|
|
class Callback |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* The normalized callable. |
9
|
|
|
* @var mixed |
10
|
|
|
*/ |
11
|
|
|
protected $target; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Create a new Callback instance |
15
|
|
|
* |
16
|
|
|
* @param callable $target The callback to wrap |
17
|
|
|
*/ |
18
|
|
|
public function __construct(callable $target) |
19
|
|
|
{ |
20
|
|
|
$this->target = static::normalizeSyntax($target); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Normalize the callable syntax |
25
|
|
|
* |
26
|
|
|
* Converts string static class method to standard callable array. |
27
|
|
|
* |
28
|
|
|
* @param mixed $callback Target callback |
29
|
|
|
* |
30
|
|
|
* @return mixed Closure Anonymous function |
31
|
|
|
* array Class method |
32
|
|
|
* string Function |
33
|
|
|
*/ |
34
|
|
|
public static function normalizeSyntax(callable $callback) |
35
|
|
|
{ |
36
|
|
|
if (is_string($callback) && false !== strpos($callback, '::')) { |
37
|
|
|
$callback = explode('::', $callback); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $callback; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Call the target callable |
45
|
|
|
* |
46
|
|
|
* @return mixed Returns the return value of the callback, or FALSE on error. |
47
|
|
|
*/ |
48
|
|
|
public function call() |
49
|
|
|
{ |
50
|
|
|
return $this->callArray(func_get_args()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Call the target callable, with an array of arguments |
55
|
|
|
* |
56
|
|
|
* @param array $arguments The parameters to be passed to the callback, as an indexed array. |
57
|
|
|
* @return mixed Returns the return value of the callback, or FALSE on error. |
58
|
|
|
*/ |
59
|
|
|
public function callArray(array $arguments = []) |
60
|
|
|
{ |
61
|
|
|
return call_user_func_array($this->target, $arguments); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the target callable |
66
|
|
|
* |
67
|
|
|
* @return mixed The normalized callable |
68
|
|
|
*/ |
69
|
|
|
public function get() |
70
|
|
|
{ |
71
|
|
|
return $this->target; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the number of parameters from the callback's signature |
76
|
|
|
* |
77
|
|
|
* @return int |
78
|
|
|
*/ |
79
|
|
|
public function parameterCount() |
80
|
|
|
{ |
81
|
|
|
return $this->reflect()->getNumberOfParameters(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the corresponding Reflection instance for the target callable |
86
|
|
|
* |
87
|
|
|
* @return \ReflectionFunctionAbstract |
88
|
|
|
*/ |
89
|
|
|
public function reflect() |
90
|
|
|
{ |
91
|
|
|
if ($this->target instanceof \Closure |
92
|
|
|
|| (is_string($this->target) && function_exists($this->target)) |
93
|
|
|
) { |
94
|
|
|
return new \ReflectionFunction($this->target); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
list($class, $method) = $this->target; |
98
|
|
|
|
99
|
|
|
return new \ReflectionMethod($class, $method); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|