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