1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20:
21: final class Callback extends Object
22: {
23:
24: private $cb;
25:
26:
27:
28: 29: 30: 31: 32:
33: public function __construct($t, $m = NULL)
34: {
35: if ($m === NULL) {
36: if (is_string($t)) {
37: $t = explode('::', $t, 2);
38: $this->cb = isset($t[1]) ? $t : $t[0];
39: } elseif (is_object($t)) {
40: $this->cb = $t instanceof Closure ? $t : array($t, '__invoke');
41: } else {
42: $this->cb = $t;
43: }
44:
45: } else {
46: $this->cb = array($t, $m);
47: }
48:
49:
50: if (is_string($this->cb) && $a = strrpos($this->cb, '\\')) {
51: $this->cb = substr($this->cb, $a + 1);
52:
53: } elseif (is_array($this->cb) && is_string($this->cb[0]) && $a = strrpos($this->cb[0], '\\')) {
54: $this->cb[0] = substr($this->cb[0], $a + 1);
55: }
56:
57: if (!is_callable($this->cb, TRUE)) {
58: throw new InvalidArgumentException("Invalid callback.");
59: }
60: }
61:
62:
63:
64: 65: 66: 67:
68: public function __invoke()
69: {
70: if (!is_callable($this->cb)) {
71: throw new InvalidStateException("Callback '$this' is not callable.");
72: }
73: $args = func_get_args();
74: return call_user_func_array($this->cb, $args);
75: }
76:
77:
78:
79: 80: 81: 82:
83: public function invoke()
84: {
85: if (!is_callable($this->cb)) {
86: throw new InvalidStateException("Callback '$this' is not callable.");
87: }
88: $args = func_get_args();
89: return call_user_func_array($this->cb, $args);
90: }
91:
92:
93:
94: 95: 96: 97: 98:
99: public function invokeArgs(array $args)
100: {
101: if (!is_callable($this->cb)) {
102: throw new InvalidStateException("Callback '$this' is not callable.");
103: }
104: return call_user_func_array($this->cb, $args);
105: }
106:
107:
108:
109: 110: 111: 112: 113:
114: public function invokeNamedArgs(array $args)
115: {
116: $ref = $this->toReflection();
117: if (is_array($this->cb)) {
118: return $ref->invokeNamedArgs(is_object($this->cb[0]) ? $this->cb[0] : NULL, $args);
119: } else {
120: return $ref->invokeNamedArgs($args);
121: }
122: }
123:
124:
125:
126: 127: 128: 129:
130: public function isCallable()
131: {
132: return is_callable($this->cb);
133: }
134:
135:
136:
137: 138: 139: 140:
141: public function getNative()
142: {
143: return $this->cb;
144: }
145:
146:
147:
148: 149: 150: 151:
152: public function toReflection()
153: {
154: if (is_array($this->cb)) {
155: return new MethodReflection($this->cb[0], $this->cb[1]);
156: } else {
157: return new FunctionReflection($this->cb);
158: }
159: }
160:
161:
162:
163: 164: 165:
166: public function isStatic()
167: {
168: return is_array($this->cb) ? is_string($this->cb[0]) : is_string($this->cb);
169: }
170:
171:
172:
173: 174: 175:
176: public function __toString()
177: {
178: if ($this->cb instanceof Closure) {
179: return '{closure}';
180: } elseif (is_string($this->cb) && $this->cb[0] === "\0") {
181: return '{lambda}';
182: } else {
183: is_callable($this->cb, TRUE, $textual);
184: return $textual;
185: }
186: }
187:
188: }
189: