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: public function __construct($cb, $m = NULL)
38: {
39: if ($m !== NULL) {
40: $cb = array($cb, $m);
41: }
42: if (!is_callable($cb, TRUE)) {
43: throw new InvalidArgumentException("Invalid callback.");
44: }
45: $this->cb = $cb;
46: }
47:
48:
49:
50: 51: 52: 53:
54: public function __invoke()
55: {
56: if (!is_callable($this->cb)) {
57: throw new InvalidStateException("Callback '$this' is not callable.");
58: }
59: $args = func_get_args();
60: return call_user_func_array($this->cb, $args);
61: }
62:
63:
64:
65: 66: 67: 68:
69: public function invoke()
70: {
71: if (!is_callable($this->cb)) {
72: throw new InvalidStateException("Callback '$this' is not callable.");
73: }
74: $args = func_get_args();
75: return call_user_func_array($this->cb, $args);
76: }
77:
78:
79:
80: 81: 82: 83: 84:
85: public function invokeArgs(array $args)
86: {
87: if (!is_callable($this->cb)) {
88: throw new InvalidStateException("Callback '$this' is not callable.");
89: }
90: return call_user_func_array($this->cb, $args);
91: }
92:
93:
94:
95: 96: 97: 98:
99: public function isCallable()
100: {
101: return is_callable($this->cb);
102: }
103:
104:
105:
106: 107: 108: 109:
110: public function getNative()
111: {
112: return $this->cb;
113: }
114:
115:
116:
117: 118: 119: 120:
121: public function toReflection()
122: {
123: if (is_string($this->cb) && strpos($this->cb, '::')) {
124: return new Nette\Reflection\Method($this->cb);
125: } elseif (is_array($this->cb)) {
126: return new Nette\Reflection\Method($this->cb[0], $this->cb[1]);
127: } elseif (is_object($this->cb) && !$this->cb instanceof \Closure) {
128: return new Nette\Reflection\Method($this->cb, '__invoke');
129: } else {
130: return new Nette\Reflection\GlobalFunction($this->cb);
131: }
132: }
133:
134:
135:
136: 137: 138:
139: public function isStatic()
140: {
141: return is_array($this->cb) ? is_string($this->cb[0]) : is_string($this->cb);
142: }
143:
144:
145:
146: 147: 148:
149: public function __toString()
150: {
151: if ($this->cb instanceof \Closure) {
152: return '{closure}';
153: } elseif (is_string($this->cb) && $this->cb[0] === "\0") {
154: return '{lambda}';
155: } else {
156: is_callable($this->cb, TRUE, $textual);
157: return $textual;
158: }
159: }
160:
161: }
162: