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