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 NCallback extends NObject
25: {
26:
27: private $cb;
28:
29:
30: 31: 32: 33: 34: 35:
36: public static function create($callback, $m = NULL)
37: {
38: return new self($callback, $m);
39: }
40:
41:
42: 43: 44: 45:
46: public function __construct($cb, $m = NULL)
47: {
48: if ($m !== NULL) {
49: $cb = array($cb, $m);
50:
51: } elseif ($cb instanceof self) {
52: $this->cb = $cb->cb;
53: return;
54: }
55:
56: if (PHP_VERSION_ID < 50202 && is_string($cb) && strpos($cb, '::')) {
57: $cb = explode('::', $cb, 2);
58: } elseif (is_object($cb) && !$cb instanceof NClosure) {
59: $cb = array($cb, '__invoke');
60: }
61:
62:
63: if (is_string($this->cb) && $a = strrpos($this->cb, '\\')) {
64: $this->cb = substr($this->cb, $a + 1);
65:
66: } elseif (is_array($this->cb) && is_string($this->cb[0]) && $a = strrpos($this->cb[0], '\\')) {
67: $this->cb[0] = substr($this->cb[0], $a + 1);
68: }
69: if (!is_callable($cb, TRUE)) {
70: throw new InvalidArgumentException("Invalid callback.");
71: }
72: $this->cb = $cb;
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(); return call_user_func_array($this->cb, $_args);
86: }
87:
88:
89: 90: 91: 92:
93: public function invoke()
94: {
95: if (!is_callable($this->cb)) {
96: throw new InvalidStateException("Callback '$this' is not callable.");
97: }
98: $_args=func_get_args(); return call_user_func_array($this->cb, $_args);
99: }
100:
101:
102: 103: 104: 105: 106:
107: public function invokeArgs(array $args)
108: {
109: if (!is_callable($this->cb)) {
110: throw new InvalidStateException("Callback '$this' is not callable.");
111: }
112: return call_user_func_array($this->cb, $args);
113: }
114:
115:
116: 117: 118: 119:
120: public function isCallable()
121: {
122: return is_callable($this->cb);
123: }
124:
125:
126: 127: 128: 129:
130: public function getNative()
131: {
132: return $this->cb;
133: }
134:
135:
136: 137: 138: 139:
140: public function toReflection()
141: {
142: if (is_string($this->cb) && strpos($this->cb, '::')) {
143: return new NMethodReflection($this->cb);
144: } elseif (is_array($this->cb)) {
145: return new NMethodReflection($this->cb[0], $this->cb[1]);
146: } elseif (is_object($this->cb) && !$this->cb instanceof Closure) {
147: return new NMethodReflection($this->cb, '__invoke');
148: } else {
149: return new NFunctionReflection($this->cb);
150: }
151: }
152:
153:
154: 155: 156:
157: public function isStatic()
158: {
159: return is_array($this->cb) ? is_string($this->cb[0]) : is_string($this->cb);
160: }
161:
162:
163: 164: 165:
166: public function __toString()
167: {
168: if ($this->cb instanceof Closure) {
169: return '{closure}';
170: } elseif (is_string($this->cb) && $this->cb[0] === "\0") {
171: return '{lambda}';
172: } else {
173: is_callable($this->cb, TRUE, $textual);
174: return $textual;
175: }
176: }
177:
178: }
179: