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:
39: public static function create($callback, $m = NULL)
40: {
41: return new self($callback, $m);
42: }
43:
44:
45:
46: 47: 48: 49:
50: public function __construct($cb, $m = NULL)
51: {
52: if ($m !== NULL) {
53: $cb = array($cb, $m);
54:
55: } elseif ($cb instanceof self) {
56: $this->cb = $cb->cb;
57: return;
58: }
59:
60: if (!is_callable($cb, TRUE)) {
61: throw new InvalidArgumentException("Invalid callback.");
62: }
63: $this->cb = $cb;
64: }
65:
66:
67:
68: 69: 70: 71:
72: public function __invoke()
73: {
74: if (!is_callable($this->cb)) {
75: throw new InvalidStateException("Callback '$this' is not callable.");
76: }
77: $args = func_get_args();
78: return call_user_func_array($this->cb, $args);
79: }
80:
81:
82:
83: 84: 85: 86:
87: public function invoke()
88: {
89: if (!is_callable($this->cb)) {
90: throw new InvalidStateException("Callback '$this' is not callable.");
91: }
92: $args = func_get_args();
93: return call_user_func_array($this->cb, $args);
94: }
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:
117: public function isCallable()
118: {
119: return is_callable($this->cb);
120: }
121:
122:
123:
124: 125: 126: 127:
128: public function getNative()
129: {
130: return $this->cb;
131: }
132:
133:
134:
135: 136: 137: 138:
139: public function toReflection()
140: {
141: if (is_string($this->cb) && strpos($this->cb, '::')) {
142: return new Nette\Reflection\Method($this->cb);
143: } elseif (is_array($this->cb)) {
144: return new Nette\Reflection\Method($this->cb[0], $this->cb[1]);
145: } elseif (is_object($this->cb) && !$this->cb instanceof \Closure) {
146: return new Nette\Reflection\Method($this->cb, '__invoke');
147: } else {
148: return new Nette\Reflection\GlobalFunction($this->cb);
149: }
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:
167: public function __toString()
168: {
169: if ($this->cb instanceof \Closure) {
170: return '{closure}';
171: } elseif (is_string($this->cb) && $this->cb[0] === "\0") {
172: return '{lambda}';
173: } else {
174: is_callable($this->cb, TRUE, $textual);
175: return $textual;
176: }
177: }
178:
179: }
180: