|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Cubiche package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) Cubiche |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Cubiche\Core\Delegate; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Delegate class. |
|
16
|
|
|
* |
|
17
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class Delegate extends AbstractCallable |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var \ReflectionFunction|\ReflectionMethod |
|
23
|
|
|
*/ |
|
24
|
|
|
private $reflection = null; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var callable |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $callable; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param Closure $closure |
|
33
|
|
|
* |
|
34
|
|
|
* @return static |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function fromClosure(\Closure $closure) |
|
37
|
|
|
{ |
|
38
|
|
|
return new static($closure); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param object $object |
|
43
|
|
|
* @param string $method |
|
44
|
|
|
* |
|
45
|
|
|
* @return static |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function fromMethod($object, $method) |
|
48
|
|
|
{ |
|
49
|
|
|
return new static(array($object, $method)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param string $class |
|
54
|
|
|
* @param string $method |
|
55
|
|
|
* |
|
56
|
|
|
* @return static |
|
57
|
|
|
*/ |
|
58
|
|
|
public static function fromStaticMethod($class, $method) |
|
59
|
|
|
{ |
|
60
|
|
|
return new static(array($class, $method)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param string $function |
|
65
|
|
|
* |
|
66
|
|
|
* @return static |
|
67
|
|
|
*/ |
|
68
|
|
|
public static function fromFunction($function) |
|
69
|
|
|
{ |
|
70
|
|
|
return new static($function); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param callable $callable |
|
75
|
|
|
*/ |
|
76
|
|
|
public function __construct($callable) |
|
77
|
|
|
{ |
|
78
|
|
|
if (!\is_callable($callable)) { |
|
79
|
|
|
throw new \InvalidArgumentException('The $callable argument must be a callable.'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if (\is_string($callable) && \strpos($callable, '::')) { |
|
83
|
|
|
$callable = explode('::', $callable); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$this->callable = $callable; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return \ReflectionFunction|\ReflectionMethod |
|
91
|
|
|
*/ |
|
92
|
|
|
public function reflection() |
|
93
|
|
|
{ |
|
94
|
|
|
if ($this->reflection === null) { |
|
95
|
|
|
if (\is_array($this->callable)) { |
|
96
|
|
|
$this->reflection = new \ReflectionMethod($this->callable[0], $this->callable[1]); |
|
97
|
|
|
} elseif (\is_object($this->callable) && !$this->callable instanceof \Closure) { |
|
98
|
|
|
$this->reflection = new \ReflectionMethod($this->callable, '__invoke'); |
|
99
|
|
|
} else { |
|
100
|
|
|
$this->reflection = new \ReflectionFunction($this->callable); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $this->reflection; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return callable |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getCallable() |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->callable; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* {@inheritdoc} |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function innerCallable() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->getCallable(); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|