1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette;
13:
14: use Nette;
15:
16:
17:
18: 19: 20: 21: 22: 23: 24: 25:
26: final class Callback extends Object
27: {
28:
29: private $cb;
30:
31:
32:
33: 34: 35: 36: 37:
38: public function __construct($t, $m = NULL)
39: {
40: if ($m === NULL) {
41: if (is_string($t)) {
42: $t = explode('::', $t, 2);
43: $this->cb = isset($t[1]) ? $t : $t[0];
44: } elseif (is_object($t)) {
45: $this->cb = $t instanceof \Closure ? $t : array($t, '__invoke');
46: } else {
47: $this->cb = $t;
48: }
49:
50: } else {
51: $this->cb = array($t, $m);
52: }
53:
54: if (!is_callable($this->cb, TRUE)) {
55: throw new InvalidArgumentException("Invalid callback.");
56: }
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_array($this->cb)) {
135: return new Nette\Reflection\Method($this->cb[0], $this->cb[1]);
136: } else {
137: return new Nette\Reflection\GlobalFunction($this->cb);
138: }
139: }
140:
141:
142:
143: 144: 145:
146: public function isStatic()
147: {
148: return is_array($this->cb) ? is_string($this->cb[0]) : is_string($this->cb);
149: }
150:
151:
152:
153: 154: 155:
156: public function __toString()
157: {
158: if ($this->cb instanceof \Closure) {
159: return '{closure}';
160: } elseif (is_string($this->cb) && $this->cb[0] === "\0") {
161: return '{lambda}';
162: } else {
163: is_callable($this->cb, TRUE, $textual);
164: return $textual;
165: }
166: }
167:
168: }
169: